2

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
7
  • What environment are you running it on? Have you tried using ~/.bash_profile instead per this question's answer ? Commented Oct 9, 2013 at 18:24
  • 1
    Just a side note, but you might want to change your function name as it's confusing with groovy the dynamic language for the Java platform which can be run on the command line via the groovy command. Commented Oct 9, 2013 at 18:33
  • I was able to get this to run successfully on my box. Are you sure that you have the function in the right .bashrc? Note that you're using the ~ notation, so if you're sudo'ing, you might be looking in the wrong place. Commented Oct 9, 2013 at 18:42
  • @Kasra I'm not sure to understand your question but I'll try a dumb answer : I'm using it locally on my machine as normal user. Commented Oct 9, 2013 at 18:50
  • @ychaouche It was a two sided question, my bad: 1. Are you running as sudo? 2. Is this OS X or a linux? Commented Oct 9, 2013 at 18:51

1 Answer 1

4

/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).

Sign up to request clarification or add additional context in comments.

15 Comments

I believe you also need to export a shell function for it to be available in a child shell.
@ychaouche Issuing commands in your shell directly is interactive mode. Issuing the same set of commands from within a script is non-interactive.
@chepner Yes, but that file that is so explicitly sourced contains something that forces it to return when non running interactively.
@devnull may be right I have this in my source code : if [[ $- != *i* ]] ; then # Shell is non-interactive. Be done now! return fi
@ychaouche Don't remove that. Instead move the groovy function to another file, say ~/.bashfunctions and source that file instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.