unix scripting

Download unix scripting

Post on 18-Nov-2014




2 download

Embed Size (px)




Where I can use Linux?You can use Linux as Server Os or as standalone Os on your PC. (But it is best suited for Server.) As a server Os it provides different services/network resources to client. Server Os must be:

Stable Robust Secure High Performance

Linux offers all of the above characteristics plus its Open Source and Free OS. So Linux can be used as: (1) On standalone workstation/PC for word processing, graphics, software development, internet, e-mail, chatting, small personal database management system etc. (2) In network environment as: (A) File and Print or Application Server Share the data, Connect the expensive device like printer and share it, e-mail within the LAN/intranet etc are some of the application.

Linux Server with different Client Os

(B) Linux sever cab be connected to Internet, So that PC's on intranet can share the internet/email etc. You can put your web server that run your web site or transmit the information on the internet.

Linux Server can act as Proxy/Mail/WWW/Router Server etc.

So you can use Linux for:

Personal Work Web Server Software Development Workstation Workgroup Server In Data Center for various server activities such as FTP, Telnet, SSH, Web, Mail, Proxy, Proxy Cache Appliance etc

What Kernel Is?Kernel is heart of Linux Os. It manages resource of Linux Os .Resources means facilities available in Linux. For e.g. Facility to store data, print data on printer, memory, file management etc. Kernel decides who will use this resource, for how long and when. It runs your programs (or set up to execute binary files). The kernel acts as an intermediary between the computer hardware and various programs/application/shell.

It's Memory resident portion of Linux. It performance following tasks:

I/O management Process management Device management File management Memory management

What is Linux Shell ?Computer understands the language of 0's and 1's called binary language. 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 it is a valid command, it is passed to kernel. Shell is a user program or it is environment provided for user interaction. Shell is a 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. Several shells available with Linux including: Shell Name Developed by Where BASH ( Bourne-Again Brian Fox and Chet Free Software SHell ) Ramey Foundation CSH (C SHell) Bill Joy University of California (For BSD) Remark Most common shell in Linux. It's Freeware shell. The C shell's syntax and usage are very similar to the C programming language. -TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH).

KSH (Korn SHell) TCSH

David Korn See the man page. Type $ man tcsh

AT & T Bell Labs --

Tip: To find all available shells in your system type following command: $ cat /etc/shells Note that each shell does the same job, but each understand 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 (

Usually in-front of $ prompt, This prompt is depend upon your shell and Environment that you set or by your System Administrator, therefore you may get different prompt ). Tip: To find your current shell type following command $ echo $SHELL

Exit StatusBy default in Linux if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not. (1) If return value is zero (0), command is successful. (2) If return value is nonzero, command is not successful or some sort of error executing command/shell script. This value is know as Exit Status. But how to find out exit status of command or shell script? Simple, to determine this exit Status you can use $? special variable of shell. For e.g. (This example assumes that unknow1file doest not exist on your hard drive) $ rm unknow1file It will show error as follows rm: cannot remove `unkowm1file': No such file or directory and after that if you give command $ echo $? it will print nonzero value to indicate error. Now give command $ ls $ echo $? It will print 0 to indicate command is successful. Exercise Try the following commands and not down the exit status:$ expr 1 + 3 $ echo $?

$ echo Welcome $ echo $? $ wildwest canwork? $ echo $? $ date $ echo $? $ echon $? $ echo $?

How to write shell scriptFollowing steps are required to write shell script: (1) Use any editor like vi or mcedit to write shell script. (2) After writing shell script set execute permission for your script as follows syntax: chmod permission your-script-name 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). (3) Execute your script as syntax: bash your-script-name sh your-script-name ./your-script-name Examples:$ bash bar $ sh bar $ ./bar

NOTE In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows Syntax: . command-name Example:$ . foo

Now you are ready to write first shell script that will print "Knowledge is Power" on screen. See the common vi command list , if you are new to vi.$ vi first # # My first shell script # clear echo "Knowledge is Power"

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

First screen will be clear, then Knowledge is Power is printed on screen. Script Command(s) $ vi first # # My first shell script # clear 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"

echo "Knowledge is Power"

How Shell Locates the file (My own bin directory to execute script) Tip: For shell script file try to give file extension such as .sh, which can be easily identified by you as shell script. 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 ShellTo process our data/information, data must be kept in computers RAM memory. RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable (Its a named storage location that may take different values, but only one at a time). 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. You can see system variables by giving command like $ set, some of the important System variables are: System Variable BASH=/bin/bash BASH_VERSION=1.14.7(1) COLUMNS=80 HOME=/home/vivek LINES=25 LOGNAME=students OSTYPE=Linux PATH=/usr/bin:/sbin:/bin:/usr/sbin PS1=[\u@\h \W]\$ PWD=/home/students/Common SHELL=/bin/bash USERNAME=vivek Meaning Our shell name Our shell version name No. of columns for our screen Our home directory No. of columns for our screen students Our logging name Our Os type Our path settings Our prompt settings Our current working directory 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? Click here for answer.

Answer to Variables in Linux.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 cotaines.

How to define User defined variables (UDV)To define UDV use following syntax Syntax: variable name=value 'value' is assigned to given 'variable name' and Value must be on right side = sign. Example: $ no=10# this is ok $ 10=no# Error, NOT Ok, Value must be on right side of = sign. To define variable called 'vech' having value Bus$ vech=Bus

To define variable called n having value 10$ n=10

Rules for Naming variable name (Both UDV and System Variable)(1) Variable name must begi


View more >