structure of scripts

Supervisor

Administrator
Apr 27, 2015
1,863
2,546
335
All I'm going to explain here is the structure. I'm not goint to explain how it works. There will be different tutorials for that.
When you call a script for execution, the interpreter will look for logic errors.
Example:
if [ $foo -gt 1 ]
then echo "foo is greater than 1"
-> error, because you forgot to close it with fi


Tell the OS what interpretor it's got to use
Define variables you need. Calling variables before declaring (chronological order) will result in an error
Functions you may use need to be read before they're called by the script

Code which the machine is going to go through line by line (with exeptions)
Exiting the script


Small example script:
#!/bin/bash
foo="1"
bar="2"

function_name(){
echo "This is the variable foo: \"$foo\""
printf "This is the variable bar: \"$bar\"\n"
}

printf "Do you want to execute function_name? Type (y)es or (n)o: "
read answer
case $answer in
y*|Y*)
function_name

exit
;;
*)
printf "You didn't want it, so I'm not going to. See ya next time!\n"

exit 1
;;
esac

exit
 
Top