170 likes | 357 Views
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.
E N D
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? • Use any editor like vi / mcedit / nanoor notepad to write shell script. • After writing your script in txt format, execute it by one of the following ways: $ bash your-script-name.txt$ shyour-script-name.txt$ ./your-script-name.txt • Note: sometimes you need to convert your script/text filefrom 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 ( NOsubstitution of variables and commands). • Back quote` • Used with commands only. • To execute command.
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: (1) System variables (SV) - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. (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 • begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character • Don't put spaces on either side of the equal sign no=10 no =10 wrongno= 10wrong no = 10wrong • Variables are case-sensitive, just like filename in Linux. • You can define NULL variable as follows: vech= vech="" • Do not use ?,* etc, to name your variable names.
Shell Arithmetic • Syntax:exprop1 math-operator op2
Shell Arithmetic (cont.) • 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 • Syntax: read variable1 variable2 variableN Example 1:#Script to read your name from key-board#echo "Your first name please“:read fnameecho "Hello $fname, Lets be friend"!
Class exercise (3) • Write a script that reads two numbers from key-board and calculate their sum