Bit of Bashing

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.


abc=1

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 )


read newvar

where read command will read the value of newvar variable from the user .

To see value of any variable you can use :


echo $(var_name)

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

Bonus :

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.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s