Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to learn shell scripting by reading linuxcommand.org. For some reason I keep getting the error "Not Found", even though it runs all the commands.

The error codes I get are:

minecraft_backup.sh: 21: run_backup: not found

minecraft_backup.sh: 23: function: not found

Here's the script:

#!/bin/bash
#MineCraft backup script by

#Variables
APP=MineCraft
SERVICE=Minecraft_Mod.jar
APPDIR=/opt/MineCraft_Server/bin
BACKUPDIR1=/opt/MineCraft_Server/backup1
BACKUPDIR2=/opt/MineCraft_Server/backup2



#First i want to check if minecraft server is running.
if(ps ax | grep -v grep | grep $SERVICE > /dev/null)
then
    echo "$APP is running"
    $(run_backup)
else
    echo "$APP is not running"
    exit 1
fi

function run_backup
{
    echo "Starting back up"

    #create backup dirs of they do not exit
    if [ ! -d "$BACKUPDIR1" ]
    then
        mkdir $BACKUPDIR1
    fi

    if [ ! -d "$BACKUPDIR2" ]
    then
        mkdir $BACKUPDIR2
        echo "test"
    fi

    #backup save1 to save2
    rsync -av --delete "$BACKUPDIR1/" "$BACKUPDIR2"

    #backup running app to save 1
    rsync -av --delete "$APPDIR/" "$BACKUPDIR1"

}
share|improve this question
    
@ev00l I've modified your post, in the future please include the contents of your code in the question, do not use pastebins for that. –  xenoterracide Jan 9 '11 at 6:21
    
I am sorry about that. I tried to include at first using the code button, but it made it look really bad with different font sizes and so. –  ev00l Jan 9 '11 at 6:24
2  
@ev00l I did it by putting it in a file, and doing this. cat test| psp4 psp4 is an alias for prepend 4 spaces. alias psp4="sed -e 's/^/ /'" –  xenoterracide Jan 9 '11 at 6:29
1  
@xeno: Using temp files and sed to add spaces is not necessary: Next time you need to include code, just paste, mark, and click the 'code' icon over the edit box. You will then witness the magic of stackexchange javascript adding the four spaces for you :) –  Janus Jan 10 '11 at 6:59
1  
@janus blah... I just did it the way i do on my system. it's habitual, though usually the files are already on my system –  xenoterracide Jan 10 '11 at 10:39

4 Answers 4

up vote 6 down vote accepted

The standard construct to define a function is

run_backup () { … }

In ksh, bash and zsh, but not in other shells such as dash, you can define a function with

function run_backup { … }

What happened when you ran the script with dash was:

  • The shell executed the $(run_backup) line, naturally resulting in the first error message since there was no command called run_backup.
  • The shell later executed the function run_backup, naturally resulting in the second error message since there was no command called function.
  • Then the shell executed the braced block, which on its own is valid shell syntax.

Note that as xenoterracide's comment indicates, since you have #!/bin/bash, the script would have been run by bash if you hadn't done something wrong.

Even if you run the script with the correct shell, or (my recommendation) change function to the standard syntax (I don't see any other non-standard construct), you still need to move the function definition to before it's used.

One more useful tip: when you're debugging a shell script, run bash -x /path/to/script (or sh -x /path/to/script, etc.). This prints a trace of each executed line. If you want to trace just part of a script, you can use set -x to enable traces and set +x to disable them. Zsh has better traces than the others, but it has incompatible syntax; you can tell zsh to expect sh syntax with the command emulate sh 2>/dev/null (which has no effect in other shells).

share|improve this answer
    
+1 for mentioning function is not standard. I wasn't sure, which is part of the reason I originally asked for bash --version I thought maybe it wasn't in all versions. –  xenoterracide Jan 9 '11 at 15:33
    
Thank you so much. I haven't had the time to be online before now. Love this site already :D –  ev00l Jan 11 '11 at 5:37
    
I hope i accepted the answer correctly. –  ev00l Jan 11 '11 at 5:39

Your functions need to be defined before you use them.

share|improve this answer

I think I solved it myself. Seems like I got owned by the fact that Ubuntu uses dash as default, and I was running the script using the sh command... "sigh"

share|improve this answer
3  
you should have just done ./script then it would use the #! line to determine what to run it as. –  xenoterracide Jan 9 '11 at 8:23
    
might consider accepting an answer –  xenoterracide Jan 10 '11 at 14:26
    
Yeah i should have done that. I just read that i could either make it executable at ./ run it, or use the sh command ;D –  ev00l Jan 11 '11 at 5:40

I'm going to make some guesses... vim doesn't like if( so try if ( that may be just a vim thing though.

I think your real issue is that you call run_backup before you've created it. put your function before your if then else block. the shell has to be aware of functions before it can use them. this is true of programming in general.

share|improve this answer
    
I use gedit for editing. I have added a space to my if ( statement, and i also swapped the positions of the function and the code. The script still runs fine, but also still produces those outputs, just with a different line number i guess: minecraft_backup.sh: 11: function: not found minecraft_backup.sh: 45: run_backup: not found –  ev00l Jan 9 '11 at 6:54
    
@ev00l :S well maybe someone else will know better. I am not an expert bash coder, and I don't get those errors. I get a whole different set of errors. –  xenoterracide Jan 9 '11 at 6:56
    
haha, i can imagine :D It just seems weird to my that it says not found, and then runs the function anyway. –  ev00l Jan 9 '11 at 7:04
    
You don't need spaces around parentheses (except when they would be interpreted as extended patterns in ksh/bash/zsh). This doesn't mean it's not a good idea to use spaces. You do need spaces around square brackets and braces. Yeah, shell syntax is inconsistent. –  Gilles Jan 9 '11 at 13:07
    
@Gilles, like I said, might just be vim's syntax highlighting, but seemed like a good thing to rule out. –  xenoterracide Jan 9 '11 at 15:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.