Recently I’ve started to learn how to make bash scripts , getting them started at boot time or before shutdown . Let’s share some details .
Firstly let’s see how to create or assign a variable a certain value
In bash , it can be imagined as a dynamically typed language . i.e you do not need to specify the type of data you’re going to enter.
will set value of variable equal to 1
but what if you want to read from the user the value of a variable ( though that’ll make more sense once we’re done with conditional statements , let’s do it now itself )
where read command will read the value of newvar variable from the user .
To see value of any variable you can use :
Now , let’s go to conditional statements .
an example if statement in bash looks like :
if [ condition ] ; then #commands to be executed when condition is true else #commands to be executed when condition is false fi
As you can see , to close an if statement here , we use fi .
Now, to fire your code up at boot , you can save your file as filename.sh or possibly another extension( Need to verify though) .
crontab -e @reboot /path/to/file
Another thing I found while searching to test a successful completion of a command I found the use of && and || . It works like
command1 && echo "Command successful" || echo "Command unsuccessful"
here is a sample of echo commands . To use more commands you can seperate them by a semicolon.