http://www.linfo.org/touch.html
As it is very new to me. I am sharing that with you all.
Posted in Bash/Shell Scripting
Syntax:
1 2 3 4 |
for var in <list> do <commands> done |
example:
1 2 3 4 5 |
named='Zaki Live .Com' for nametame in $named do echo $nametame done |
we can check at the end that loop is not working by this
1 2 3 4 5 6 |
named='Zaki Live .Com' for nametame in $named do echo $nametame done echo Voila La La |
Another examples can be c like styles:
1 2 3 4 |
for (( c=1; c<=5; c++ )) do echo "Welcome $c times" done |
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/
Take a name from user and say hello user! 😀
1 2 3 |
echo `expr "What is your name?"` read a echo `expr "Hello $a"` |
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 !"
expr “False!”
else echo
conditional with variables
t1="zaki"
t2="live"
if [ "$t1" = "t2" ]
then
echo expr “True”
expr “False”`
else
echo
fi #you have to end with this if you started with if
1 2 3 4 5 6 7 8 |
echo `expr "What's your name?"` read name if [ "$name" = "$USER" ] then echo `expr "Hello $name"` else echo `expr "You are not valid user!"` fi |
Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html#ss6.1
http://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php
Here I tried this on ubuntu
To create the file
1 |
cat>mul |
code type in the terminal:
1 2 3 4 |
read a read b mul=`expr $a "*" $b` echo $mul |
save the file and close editing mode:
1 |
ctrl+d |
change the permission of this file:
1 |
chmod +x mul |
to show the output
1 2 3 4 |
sh mul 4 5 20 |
Here I tried this on ubuntu
To create the file
1 |
cat>add |
code type in the terminal:
1 2 3 4 |
read a read b sum=`expr $a + $b` echo $sum |
don’t give space after or before equal sign
save the file and close editing mode:
1 |
ctrl+d |
change the permission of this file:
1 |
chmod +x add |
to show the output
1 2 3 4 |
sh add 4 5 9 |
Shortcut method for addition or any other:
create a file with cat then type code from the below in one line
1 |
echo `expr $1 + $2 + $3` |
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
1 |
sh shortcut 3 4 5 |
and you will get the addition of this variables
Posted in A ll Codes, Bash/Shell Scripting
Tagged add, addition, bash/shell, terminal
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
1 2 3 4 5 6 7 8 9 10 11 |
echo -n "Enter numnber : " read number remainder=$(( $number % 2 )) if [ $remainder = 0 ] then echo "$number is even number" else echo "$number is odd number" fi |
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.
Posted in A ll Codes, Bash/Shell Scripting
Tagged bash/shell, bourne, oddeven, programming, scripting, ubuntu, unix