Docsity

Operating System: Linux Shell Scripts, Lecture notes of Computer Science

Partial preview of the text

Download Operating System: Linux Shell Scripts and more Lecture notes Computer Science in PDF only on Docsity!

Linux

Shell

Scripts

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.

What

is

Shell

Script

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

How

to

write

and

execute

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. ƒ

A

word beginning with

causes that word and all remaining characters on that line to be ignored.

Shell

script

format

In Linux (Shell), there are two types of variable: ƒ System variables

created and maintained by Linux itself.

echo

$USER

echo

$PATH

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.

Variables

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

[

A

]

echo Your os:$myos

[

Linux

]

echo $text

[

]

Variables

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”

Read

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

1`

x=1 x=`expr $x

1`

Shell

Arithmetic

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

If

Else

If

Else

#!/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

If

Else

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.

Case

#!/bin/sh echo “Is it morning? Please answer yes or no” read timeofday case “$timeofday” in yes

y

Yes

YES

_echo “Good Morning”;; n_*

N*

*echo “Good Afternoon”;; **

echo “Sorry, answer not recognized”;; esac

Case

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

Command

Line

arguments