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
ychaouche
  • 4,922
  • 2
  • 44
  • 52
  • What environment are you running it on? Have you tried using ~/.bash_profile instead [per this question's answer](http://stackoverflow.com/questions/4477328/bashrc-not-read-when-shell-script-is-invoked-from-desktop-shortcut) ? – Kasra Rahjerdi Oct 09 '13 at 18:24
  • 1
    Just a side note, but you might want to change your function name as it's confusing with [groovy](http://groovy.codehaus.org/Running) the dynamic language for the Java platform which can be run on the command line via the groovy command. – FuriousGeorge Oct 09 '13 at 18:33
  • @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. – ychaouche Oct 09 '13 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? – Kasra Rahjerdi Oct 09 '13 at 18:51
  • oh sorry yes linux as normal user. – ychaouche Oct 09 '13 at 19:02
  • 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. – FuriousGeorge Oct 09 '13 at 18:42
  • `~/.bashrc` will refer to the `.bashrc` of the user that runs the script. Are you sure the correct `.bashrc` is being sourced? – chepner Oct 09 '13 at 19:10

1 Answers1

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

devnull
  • 118,548
  • 33
  • 236
  • 227
  • I believe you also need to export a shell function for it to be available in a child shell. – chepner Oct 09 '13 at 18:31
  • @chepner OP says that the file containing the function is being `source`d. – devnull Oct 09 '13 at 18:36
  • Ah, missed that. But why doesn't it work when `.bashrc` is explicitly sourced in `batch.sh`? – chepner Oct 09 '13 at 18:57
  • @chepner I must say that you didn't read the answer upon which you're commenting. Sorry. – devnull Oct 09 '13 at 18:58
  • I read your answer and I don't have any clue what is interactive mode. I'm explicitly sourcing it in my script, would still interactive mode be relevant in my case ? – ychaouche Oct 09 '13 at 19:03
  • 1
    @ychaouche Issuing commands in your shell directly is interactive mode. Issuing the same set of commands from within a script is **non-interactive**. – devnull Oct 09 '13 at 19:05
  • @devnull `batch.sh` is `sourced` *directly* in `batch.sh`, not manually at a shell prompt. `groovy` should be available in that shell. – chepner Oct 09 '13 at 19:08
  • @chepner Perhaps you didn't notice that `groovy` is defined in `~/.bashrc`. – devnull Oct 09 '13 at 19:11
  • See my previous comment. This has absolutely *nothing* to do with which files are automatically sourced by the shell at start-up: the file is *explicitly* sourced by the script when it starts. – chepner Oct 09 '13 at 19:12
  • 1
    @chepner Yes, **but** that file that is *so explicitly sourced* contains something that forces it to `return` when non running interactively. – devnull Oct 09 '13 at 19:14
  • If that turns out to be the case, moving the function to `.profile` (or some other automatically sourced file) will not help, because functions defined in the current environment are not available to scripts that run in child processes unless they are exported; hence my original comment. – chepner Oct 09 '13 at 19:22
  • 1
    @devnull may be right I have this in my source code : `if [[ $- != *i* ]] ; then # Shell is non-interactive. Be done now! return fi` – ychaouche Oct 09 '13 at 19:37
  • @devnull is it safe to remove that instruction ? – ychaouche Oct 09 '13 at 19:39
  • 1
    @ychaouche Don't remove that. Instead move the `groovy` function to another file, say `~/.bashfunctions` and `source` that file instead. – devnull Oct 09 '13 at 19:41
  • If you really want it in your .bashrc for some reason, just add it before the `if [[ $- != *i* ]] ; then # Shell is non-interactive. Be done now! return fi` line. I tried it an it works. – FuriousGeorge Oct 09 '13 at 20:06