Shell Programming
Shell Programming
In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in Os there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is pass to kernel. Shell is a user program or it's environment provided for user interaction. Shell is an command language interpreter that executes commands read from the standard input device (keyboard) or from a file. Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.
To find all available shells in your system type following command: $ cat /etc/shells Each shell does the same job, but each understand a different command syntax and provides different built-in functions. In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's not as powerful as our Linux Shells are!
Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux Os what users want. If we are giving commands from keyboard it is called command line interface. To find your current shell type following command: $ echo $SHELL
Examples: $ chmod +x your-script-name $ chmod 755 your-script-name Note: This will set read write execute(7) permission for owner, for group and other permission is read and execute only(5).
Continued
Execute your script as:
Writing first shell script that will print "Knowledge is Power" on screen.
Continued
..
After saving the above script, you can run the script as follows: $ ./first
This will not run script since we have not set execute permission for our script first; to do this type command $ chmod 755 first $ ./first
Continued
Script Command(s) $ vi first # # My first shell script # Meaning Start vi editor # followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text clear the screen To print message or value of variables on screen, we use echo command, general form of echo command is as follows syntax: echo "Message"
clear
Exercise: 1)Write following shell script, save it, execute it and note down the it's output.
$ vi ginfo # # # Script to print user information who currently login , current date & time # clear echo "Hello $USER" echo "Today is \c ";date echo "Number of user login : \c" ; who | wc -l echo "Calendar" cal exit 0
Variables in Shell
In Linux (Shell), there are two types of variable: (1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS. (2) User defined variables (UDV) - Created and maintained by user. This type of variable defined in lower letters. We can see system variables by giving command like $ set.
PATH=/usr/bin:/sbin:/bin:/usr/s Our path settings bin PS1=[\u@\h \W]\$ Our prompt settings PWD=/home/students/CommonOur current working directory SHELL=/bin/bash USERNAME=vivek Our shell name User name who is currently login to this PC
NOTE that Some of the above settings can be different in your PC/Linux environment. You can print any of the above variables contains as follows: $ echo $USERNAME $ echo $HOME Exercise: 1) If you want to print your home directory location then you give command: a)$ echo $HOME OR (b)$ echo HOME Which of the above command is correct & why?
Exercise: 1) If you want to print your home directory location then you give command: (a)$ echo $HOME OR (b)$ echo HOME Which of the above command is correct & why? Ans:(a) command is correct, since we have to print the contains of variable (HOME) and not the HOME. You must use $ followed by variable name to print variables containes.
Rules for Naming variable name (Both UDV and System Variable)
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g. Valid shell variable are as follows HOME SYSTEM_VERSION vech no
(2) Don't put spaces on either side of the equal sign when assigning value to variable. For e.g. In following variable declaration there will be no error $ no=10 But there will be problem for any of the following variable declaration: $ no =10 $ no= 10 $ no = 10
(3) Variables are case-sensitive, just like filename in Linux. For e.g. $ no=10 $ No=11 $ NO=20 $ nO=2 Above all are different variable name, so to print value 20 we have to use $ echo $NO and not any of the following $ echo $no # will print 10 but not 20 $ echo $No# will print 11 but not 20 $ echo $nO# will print 2 but not 20
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g. $ vech= $ vech="" Try to print it's value by issuing following command $ echo $vech Nothing will be shown because variable has no value i.e. NULL variable. (5) Do not use ?,* etc, to name your variable names.
Q.1.How to Define variable x with value 10 and print it on screen. $ x=10 $ echo $x Q.2.How to Define variable xn with value Rani and print it on screen For Ans. Click here $ xn=Rani $ echo $xn
Syntax: expr op1 operator op2 Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be + Addition - Subtraction / Division % Modular, to find remainder For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2, (Remember its integer calculation) \* Multiplication $ expr 6 + 3 Now It will print sum as 9 , But $ expr 6+3 will not work because space is required between number and operator
Q.4.How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y) For Ans. Click here $x=20 $ y=5 $ expr x / y
Q.5.Modify above and store division of x and y to variable called z For Ans. Click here $ x=20 $ y=5 $ z=`expr x / y` $ echo $z