0% found this document useful (0 votes)
19 views25 pages

Lab 3

operating system

Uploaded by

Belete Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views25 pages

Lab 3

operating system

Uploaded by

Belete Siyum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Introduction to Shell programming

► Unix shell program interprets user commands


► Which are directly entered by the user or
► Can read from the file

► In order to execute shell script programs you don’t need

to compiled.
► There are different types of shells

► To check that open your terminal To check


available shells
supported by
your OS
Introduction to Shell programming

► bashwe are going to use


► To know where your bash is located -> which bash
Introduction to Shell programming (cont’d)

► First shell program


► First.sh -> touch first.sh

► When you write a shell program at the very beginning of


the program you should write
► #! /bin/bash
whi s
cate

are you
bash ch

g
usi n
In d i
Introduction to Shell programming (cont’d)

► First shell program

To execute ./first.sh
Introduction to Shell programming (cont’d)

► If there is no permission to execute Permission


► Solution: add execute permission denied
► How?
Add execute
chmod +x name.sh permission to the
script
Introduction to Shell programming (cont’d)

1 rwx: read, write, execute permission for user


► st

►2nd rwx: read, write, execute permission for group

► 3rd r-x: read, execute permission for other user


Shell programming: Variables & Comments

► Comments are lines of code, which are not executed by a


script
► You can add comment adding #
►Example : # comment section
► Variables are a container which can store data inside
► There are two types of variables
► System variables: maintained by the OS
►By convention written by Capital letters e.g. $BASH
Shell programming: Variables & Comments

System variables
Shell programming: Variables & Comments
► User defined variables: maintained by the user
► By convention written by small letters , but there is no strict rules

User variables
Shell programming: Read input from keyboard

► Use read command


Syntax: read variable_name1 variable_name2, variable_namen

Example
Shell programming: Read input from keyboard
► Accept the input the same line with instruction string
► Use –p flag before the variable name

Syntax: read –p variable_name

► Example

# Read the input on the same prompt as echo string


Shell programming: Read input from keyboard
► Accept the input Silently (e.g. Password)
► Use –s flag before the variable name

Syntax: read –s variable_name

Example

# Accept the input silently


# Output
Shell programming: Read input from keyboard

Read an array from the keyboard


► Use –a flag before the variable name
Syntax: read –a variable_name
Example

# Read an array
Shell programming: Read input from keyboard

Read without variable name


Syntax: read
Example

# Read input without variable name

# Output
Shell programming: Integer comparison
► There are two ways of integer comparison in shell
scripting
i. Using Square bracket []
► -eq : equals to
► -ne : not equal to
► -gt : grater than
► -ge : greater than or equal to
► -lt : less than
► -le : less than or equal to
Shell programming: Integer comparison

ii. Using double parenthesis (( ))


► < : less than
► <= : less than or equal to
► > : greater than
► >= : greater than or equal to
Shell programming: String comparison

ii. Both using single and double square bracket


► = : equals to
► == : equals to
► != : not equal to
► < : less than, in ASCII alphabetic order,
► > : greater than, in ASCII alphabetic order
► >= : greater than or equal to
► Note: > and < use double square bracket
Arithmetic operations

There are some ways to perform arithmetic operation in


shell scripting
►Using double parenthesis {i.e. (()) }
► Using expr command
► Using bc command
►Etc.
Arithmetic operations: Using double parenthesis

► Using double parenthesis {i.e. (()) }


Arithmetic operations: Using expr command

►Using expr command


Statements: if then statement

If [condition]
then
Syntax Statements
fi

Example

Output
Statements: if then statement

Example
Statements: if then else statement
If [condition]
then
Statements
Syntax else
statements
fi
Example

Out
Statements : if then else statement

Example
Statements: If then else statements

Example

You might also like