Top users:

1. Nadeem
120
2. Brianna
96
3. Finley
87
4. Sigmund
87
5. Bishop
84
See all...
Win $50! Every month the top Seepedia user wins $50.

Question: What are the basic shell scripting commands?

Asked by reamonn (42 points) on Aug 5, 2009  under Internet & Computers 1 answers

What are the basic shell scripting commands?


Answers
user pic
Teodoro (72 points)

on Aug 5, 2009

Use any editor like vi or mcedit to write shell script. You can also use the GNOME or KDE environment to your advantage and use the notepad available with them. After writing shell script set execute permission for your script as follows
chmod +x your-script-name
chmod 755 your-script-name



This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5). Execute your script as:
bash your-script-name
sh your-script-name
./your-script-name



Examples:
bash bar
sh bar
./bar



NOTE: In the last syntax ./ means current directory. .(dot) means execute given command file in current shell without starting the new copy of shell.
Variable Declaration:
To define a variable use the following syntax:
variable name=value
no=10
vech=Bus



To print or access the variable use the following syntax
echo $variablename
echo $vech
echo $no



Use the following syntax perform arithmetic operations.
expr op1 math-operator op2
expr 1 + 3
expr 2 - 1
expr 10 / 2
expr 20 % 3
expr 10 * 3



Note that expr 10 * 3 - Multiplication use * and not * since it’s a wild card.
Read Statement:
Use to get input (data from user) from keyboard and store (data) to variable.
read variable1, variable2,...variableN
read username
Multiple Commands:
You can use multiple commands on a line.
Command1; Command2
date;who



The if condition:
if condition
then
command
command
...
...
fi



The if...else...fi:
If given condition is true then command1 is executed otherwise command2 is executed.
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
else
if condition is not true then
execute all commands up to fi
fi



The test command:
test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
test expression OR [ expression ]
test $name = Julio



The case command:
case value in
match1 command
command

command;;
match2 command
command

command;;
esac



The for command:
for var in word1 word2 wordn
do
command
command
done



for I in 1 2 3




do
echo $i
done




The while command:
while [ condition ]
do
command1
command2
command3

done



The until command:
until command
do
command
command

done
The break statement:
Used to exit from a loop.


Your Answer

Join or Login to Submit Your Answer

Register Login
   or   




* We'll send an email with a link to activate your account.

We'll publish your answer as soon as you activate your account.