Shell scripts – part 1 Cs 302
Shell scripts What is Shell Script? “Shell Script is series of command written in plain text file. “ Why to Write Shell Script? Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. System Administration part can be also automated.
Getting started with Shell Programming (cont.) How to write shell script? 1) Use any editor like vi / mcedit / nano or notepad to write shell script. 2) After writing your script in txt format, execute it by one of the following ways: $ bash your-script-name.txt $ sh your-script-name.txt $./your-script-name.txt 1) Note: sometimes you need to convert your script/text file from DOS/MAC format to UNIX format using the following command: $ dos2unix./ your-script-name.txt
Getting started with Shell Programming (cont.) Hello world program: # # My first shell script # echo “Hello world !"
Echo command echo [OPTION] [STRING] The option -n means : do not output the trailing newline The option -e means Enable interpretation of the following backslash escaped characters in the strings: \b backspace \n new line \t horizontal tab
Echo command (cont.) "Double Quotes“ variables substitution 'Single quotes‘ protects everything enclosed between two single quote marks. It is used to turn off the special meaning of all characters ( NO substitution of variables and commands). Back quote` Used with commands only. To execute command.
commandvariable Double “ ” $echo “My working directory is pwd” The output: My working directory is pwd $echo “the home is $HOME” The output: the home is /home/nora Double “ “ Back ` ` $echo “My working directory is `pwd`” The output: My working directory is /home/nora/Desktop - Single ‘ ‘ Back ` ` $echo ‘My working directory is `pwd`’ The output: My working directory is `pwd` - Single ‘ ‘ $echo ‘My working directory is pwd’ The output: My working directory is pwd $echo ‘the home is $HOME’ The output: the home is $HOME
Comment in script # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text
Variables in shell In Linux, there are two types of variable: System variables (SV) (1) System variables (SV) - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. User defined variables (UDV) (2) User defined variables (UDV) - Created and maintained by user. How to define and print User Defined Variables? Syntax to define UDV variable name=value (without spaces !) Syntax to print or access value of UDV/SV $variablename
Variables in shell (cont.) Example: - To define variable called drink having value “tea”: drink=tea - To print it on screen : echo “$drink”
Class exercise (1) Define variable called X and give it value 10. Print it.
Rules for Naming variable name 1) begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character 2) Don't put spaces on either side of the equal sign no=10 no =10 wrong no= 10 wrong no = 10 wrong 1) Variables are case-sensitive, just like filename in Linux. 4)You can define NULL variable as follows: vech= vech="" 4)Do not use ?,* etc, to name your variable names.
Shell Arithmetic Syntax : expr op1 math-operator op2 expr expr expr 10 / 2 expr 20 % 3 expr 10 \* 3 `` echo will print 6+3 echo `expr 6 + 3` will print 9 expr 6+3 will not work because no space between number and operator
Shell Arithmetic (cont.) x=20 y=5 expr $x / $y x=20 y=5 z=`expr $x / $y` echo $z define two variable x=20, y=5 and then to print division of x and y store division of x and y to variable called z
Class exercise (2) Define two variable z=20, y=5 and then print the addition of them.
The read statement Used to get input (data from user) from keyboard and store (data) to variable read variable1 variable2 variableN Syntax: read variable1 variable2 variableN Example 1 : #Script to read your name from key-board # echo "Your first name please“: read fname echo "Hello $fname, Lets be friend" !
Class exercise (3) Write a script that reads two numbers from key-board and calculate their sum