如何使函数对bash脚本可见?

时间:2013-10-09 18:15:54

标签: bash function

我的.bashrc中有一个名为groovy的函数。

我有这个bash脚本,我想使用groovy。

它说./batch.sh: line 7: groovy: command not found

虽然我在脚本的开头提供了.bashrc。

我错过了什么?

batch.sh

#!/usr/bin/env bash
source ~/.bashrc
for file in *.html;
do
    name="${file%.html}"
    groovy "$name.html" "uncached/$name.mp3"
done;

.bashrc的一部分

function groovy {
    sed -n '/<pre>/,/<\/pre>/p' "$1" |  replace '<pre>' '' '</pre>' '' | hextomp3 - "$2"
}

function hextomp3 {
    echo "in $1"
    echo "out $2"
    echo "cut -c10-74 $1 | xxd -r -p - $2"
    cut -c10-74 "$1" | xxd -r -p - "$2"    
}

输出:

chaouche@karabeela ~/DOWNLOADS/MUSIQUE $ ./batch.sh
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found
./batch.sh: line 6: groovy: command not found

1 个答案:

答案 0 :(得分:4)

以交互模式运行时,

/etc/bashrc~/.bashrc

您可能会看到与

类似的内容
case $- in
    *i*) ;;
      *) return;;
esac

[ -z "$PS1" ] && return

~/.bashrc

考虑将您的功能添加到~/.profile~/.bash_profile(如果后者存在)。