























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























We have seen some basic shell commands
it’s time to move on to scripts. There are two ways of writing shell programs. You can type a sequence of commands and allow the shell to execute them interactively. You can store those commands in a file that you can then invoke as a program. This is known as Shell Script. We will use bash shell assuming that the shell has been installed as /bin/sh and that it is the default shell for your login.
Use any editor to write shell script. The extension is .sh
After writing shell script set execute permission for your script.
chmod +x script_name Execute your script ./script_name
Every script starts with the line #!/bin/bash This indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly.
is used as the comment character.
word beginning with
causes that word and all remaining characters on that line to be ignored.
In Linux (Shell), there are two types of variable: System variables
created and maintained by Linux itself.
echo
echo
User de fi ned variables
created and maintained by user. All variables are considered and stored as strings, even when they are assigned numeric values. Variables are case sensitive.
When assigning a value to a variable, just use the name. No spaces on either side of the equals sign. var_name=value Within the shell we can access the contents of a variable by preceding its name with a
myname=A
use quotes if the value contains spaces
myos=Linux text
echo Your name:$myname
echo Your os:$myos
Linux
echo $text
To read user input from keyboard and store it into a variable use read var1,var2,.....varn #!/bin/bash echo
n "Enter your name:” read name echo
n "Enter your student no:” read stdno echo "Your Name:$name” echo "Your Age:$stdno”
The expr command evaluates its arguments as an expression. It is commonly used for simple arithmetic operations. #!/bin/bash expr
expr
expr
expr
va r=`expr
x=1 x=`expr $x
if
conditiong
then statement elif
condition
then statement else statement fi It is must to put spaces between the
braces and the condition being checked. If you prefer putting then on the same line as if
you must add a semicolon to separate the test from the then
#!/bin/bash echo "Enter first number
read num echo "Enter second number" read num if
$num
gt $num
then echo "$num is greater than $num2" elif
$num
lt $num
then echo "$num is less than $num2" else echo "$num and $num are equal" fi
case $var in condition1) statement
condition2) statement
statement esac Notice that each pattern line is terminated with double semicolons
You can put multiple statements between each pattern and the next, so a double semicolon is needed to mark where one statement ends and the next pattern begins.
#!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes
y
Yes
_echo “Good Morning”;; n_*
*echo “Good Afternoon”;; **
echo “Sorry, answer not recognized”;; esac
Command line arguments can be passed to the shell scripts. There exists a number of built in variables
command line arguments
number of arguments $n
nth argument in
./script_name arg arg
argn