First time doing a function script and I am getting an error. Anyone knows the problem?
#!/bin/bash
hello()
{ echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"
First time doing a function script and I am getting an error. Anyone knows the problem?
#!/bin/bash
hello()
{ echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"
If you are getting "not found" error, then likely your system does not have /bin/bash. As example, on my system it is /usr/bin/bash
What error do you get?
I get normal completion.
The cat
command displays the scrtipt. the ./t.shl
command runs the script
$ cat t.shl
#!/bin/bash
hello()
{ echo "Executing function hello"
}
echo "Script has started now"
hello
echo "Script will end"
$ ./t.shl
Script has started now
Executing function hello
Script will end
As has already been stated: knowing what the error is makes it somewhat easier to tell you the reason. Save for that, you might consider putting an explicit return
-command at the end of your functions:
hello()
{
echo "Executing function hello"
return 0
}
This will give back a return code you can check in the main program and base a reaction on it. Like this:
#! /bin/bash
myfunc()
{
if [ $1 -eq 1 ] ; then
return 0
else
return 1
fi
}
# main()
echo "Script starts"
echo calling myfunc() with 1:
if myfunc 1 ; then
echo "myfunc 1 returned $?"
fi
echo calling myfunc() with 0:
if myfunc 0 ; then
echo "myfunc returned $?"
fi
I hope this helps.
bakunin