Shell script error message
Solution 1
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 calledrun_backup
. - The shell later executed the
function run_backup
, naturally resulting in the second error message since there was no command calledfunction
. - 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).
Solution 2
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"
Solution 3
Your functions need to be defined before you use them.
Solution 4
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.
Related videos on Youtube
ev00l
Updated on September 17, 2022Comments
-
ev00l over 1 year
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" }
-
xenoterracide over 13 years@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.
-
ev00l over 13 yearsI 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.
-
xenoterracide over 13 years@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/^/ /'"
-
ev00l over 13 years@xeno i will have to look up the sed command i guess :D bash --version gives me GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu)
-
xenoterracide over 13 years@ev00l all I'm doing is prepending 4 spaces into the output, which allows me to easily copy and paste into markdown.
-
ev00l over 13 yearsAnd now i realize i need to look into regex :D
-
ev00l over 13 yearsTo include code (formatted in monospace font), you can either : use `` for one line of code indent several lines of code by at least four spaces. taken from wikipedia :D Is that why you prepend 4 spaces?
-
Janus over 13 years@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 :) -
xenoterracide over 13 years@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
-
-
ev00l over 13 yearsI 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
-
xenoterracide over 13 years@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.
-
ev00l over 13 yearshaha, i can imagine :D It just seems weird to my that it says not found, and then runs the function anyway.
-
xenoterracide over 13 yearsyou should have just done
./script
then it would use the#!
line to determine what to run it as. -
Gilles 'SO- stop being evil' over 13 yearsYou 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.
-
xenoterracide over 13 years@Gilles, like I said, might just be vim's syntax highlighting, but seemed like a good thing to rule out.
-
xenoterracide over 13 years+1 for mentioning
function
is not standard. I wasn't sure, which is part of the reason I originally asked forbash --version
I thought maybe it wasn't in all versions. -
ev00l over 13 yearsThank you so much. I haven't had the time to be online before now. Love this site already :D
-
ev00l over 13 yearsYeah i should have done that. I just read that i could either make it executable at ./ run it, or use the sh command ;D
-
Yasser Sinjab about 7 yearsyou are AWESOME brother !