Shell programming

10

Transcript of Shell programming

Shell programming

• Shell is the Linux command interpreter• Often interact with users: command

• Shell reads commands from the keyboard or file

• Linux: kernel executing the command

• Shell script• Shell programs: sequence of commands

Create shell program

• Use text editors• Vi, emacs, nano, gedit

• Content: linux command

• Commands in the same line: separated by ;

• Change permissions:• chmod o + x file_name

• Execute:• bash file_name

• sh file_name

• ./ten_file

Example

• $nano first # My first shell script clear

echo "Hello $USER"

echo "Today is \c ";date

echo "Number of user login : \c" ; who | wc –l

echo "Calendar" • $ chmod 755 first

• $./first

Variable

• System variable:• Created and managed by Linux.• Variable names are uppercase

• Variable created by user:• Created and managed by users• Variable names are lowercase

• View or access variable values:• $name_ variable• echo $HOME• echo $USERNAME• Must have $ sign before variable name

System variable

Variable created by user

• How to use• name_variable=value

• Print • echo $name_variable

• Example• No=10

• Echo $No

Rules

• Variable names are case sensitive

• Variable names must begin with a character• HOME

• SYSTEM_VERSION

• no

• Vech

• No space between operator = when assigning values to variables• no=10 # correct

• no =10 # wrong

• no = 10 # wrong

Read data from keyboard and assign to variable• Read variable1