Category Archives: Bash/Shell Scripting

Bash For Loops

Syntax:

example:

we can check at the end that loop is not working by this

 

 

Another examples can be c like styles:

Bash IF Else with for loop break and continue  have to practice later time 🙂

 

Here is the video all for loop explained beautifully

References:

http://www.cyberciti.biz/faq/bash-for-loop/

Bash Programming : Say user hello !

Take a name from user and say hello user! 😀

 

Bash Programming : Check typed name is a valid $USER or not

I tried it hardly….Here you have to maintain the syntax

if[expression]
then
echo expr ""Hello Name
fi

and this conditional

if [ “foo” = “foo” ]; then
echo expression evaluated as true
fi

or the same things can be written as

if[ “foo” = “foo” ]
then
echo expr "Hello !"
fi

if..then..else in bash

if [ “zaki” = “live” ]
then
echo expr "True !"
else echo
expr “False!”

conditional with variables
t1="zaki"
t2="live"
if [ "$t1" = "t2" ]
then
echo
expr “True”
else
echo
expr “False”`
fi #you have to end with this if you started with if

Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html#ss6.1
http://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php

 

Bash Programming : Multiplication

Here I tried this on ubuntu

To create the file

code type in the terminal:

save the file and close editing mode:

change the permission of this file:

to show the output

 

Bash Programming : Addition

Here I tried this on ubuntu

To create the file

code type in the terminal:

don’t give space after or before equal sign

save the file and close editing mode:

change the permission of this file:

to show the output

Shortcut method for addition or any other:
create a file with cat then type code from the below in one line

here 1 2 and 3 is variable is taking input from user and also giving teh output so we sue $ before them….it is the shortcut method

then just type

and you will get the addition of this variables

Odd Even in Bash/Shell Scripting

I tried it so many times but it didn’t gave the solution but the code I have found on the net and I understood where my problem was

caution: Don’t forget to give space after if eg. if [ ] and symbols eg. [ $remainder = 0 ]

and don’t forget to style after then eg.

then
echo “$number is evern”
else
echo “$number is odd”

if you don’t do all of the above mentioned system will give various errors.