ychaouche
ychaouche

Reputation: 5092

how to make functions visible to bash scripts?

I have a function called groovy defined in my .bashrc.

I have this bash script where I want to use groovy.

It says ./batch.sh: line 7: groovy: command not found

Although I source .bashrc in the beginning of the script.

What am I missing ?

batch.sh

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

part of .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"    
}

output :

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

Upvotes: 2

Views: 1026

Answers (1)

devnull
devnull

Reputation: 123448

/etc/bashrc, ~/.bashrc are not read when not running in an interactive mode.

You might see something similar to

case $- in
    *i*) ;;
      *) return;;
esac

or

[ -z "$PS1" ] && return

in your ~/.bashrc.

Consider adding your function to ~/.profile or to ~/.bash_profile (if the latter exists).

Upvotes: 4

Related Questions