• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
shell scripting
 

shell scripting

on

  • 569 views

 

Statistics

Views

Total Views
569
Views on SlideShare
568
Embed Views
1

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 1

http://blackboard.ccbc.edu 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    shell scripting shell scripting Presentation Transcript

    • NATIONAL INSTITUTE OF TECHNOLOGYJAMSHEDPURComputer Science and Engineering Dept.
    • SHELL SCRIPTINGbyKUMAR SOURABH241/10
    • INDEX:• What is Shell• How Shell Works• How Shell Better• Type of Shell• What is Shell Script• Why We Write Shell Script• How We Run Shell Script• Variable in Shell• Problem Statement
    •  Shell is a agency that sits between the user and the UNIXsystem Shell is an command language interpreter that readcommands from the standard input device (keyboard) orfrom a file and execute them Shell is not part of system kernel, but uses the system kernel toexecute programs, create files etc.WHAT IS SHELL
    • WHAT IS SHELLUSER2
    • HOW SHELL WORKS?Shell is a agency that sits between the user and the UNIX systemComputer understand the language of 0s and 1s called binary language. which isdifficult for all of us, to read and write.So In OS there is special program called Shell.Shell issues the prompt and waits for us to enter a commandAfter a command is entered , the shell scans the command line for meta-character and expands abbreviation (like * in rm *)to recreate a simplifiedcommand lineIt then passes command line to kernel for executionNow the shell waits for the command to complete and normally it can’t do anywork while the command is runningAfter command execution is complete , shell returns to its waiting role to startthe next cycle . Now we are free to enter another commandHowever we can change this behavior and instruct not to wait
    • HOW SHELL WORKS?OurcommandorShell scriptLinuxShellConverted toBinary languageby ShellNow LinuxKernalUnderstadOurRequest
    • lsdatetime BASH001010001010101101010010LINUXKERNELExample:
    • Linux Shell is similar to Windows command promptbut are much more powerful.For example:On Linux it’s quite feasible to have multiple shellsinstalled, with different users able to pick the one theyprefer.:input and output can be redirected using <and >$ ls -l > lsoutput.txtHOW SHELL IS BETTER
    •  Sh – simple shellBASH – Bourne Again ShellKSH – Korne ShellCSH – C ShellSSH – Secure Shell To use a particular shell type the shell name at thecommand prompt.Eg $csh – will switch the current shell to c shell To view the available shells in the system, type cat/etc/shells at the command prompt To view the current shell that is being used, type echo$SHELL at the command promptTYPE OF SHELL-1
    • TYPE OF SHELL-2Shell NameDevelopedbyWhere RemarkBASH ( Bourne-Again SHell )Brian Foxand ChetRameyFreeSoftwareFoundationMost common shell inLinux.CSH (C SHell) Bill JoyUniversity ofCaliforniaThe C shells syntax andusage are very similar tothe C programminglanguage.KSH (Korn SHell) David KornAT & T BellLabsUsed in CommercialApplication
    •  Shell Script is series of command written in plaintext file. Shell script is just like batch file in MS-DOS buthave more power than the MS-DOS batch file.shell script is also known as shell programWHAT IS SHELL SCRIPT
    • Shell script can take input from user, file and outputthem on screen.Useful to create our own commands.Minimizes typing of repetitive commandSave lots of time.Can schedule jobs to run in the systemTo automate some task of day today life.System Administration part can be also automated.WHY TO WRITE SHELL SCRIPT
    • HOW TO WRITE SHELL SCRIPT
    • $ vi first#!/bin.sh## My first shell script#clearecho “Time nd Tide wait 4 no one"
    • Explanation:Script Command(s) Meaning$ vi firstvi editor## My first program## followed by any text is consideredcommentecho “Time nd Tide wait 4 no one” To print message or value of variableon screen, we use echo commandSyntax:echo "Message"
    • HOW TO RUN SHELL SCRIPTRun our script asSyntax: ./your-shell-program-nameFor e.g.$ ./first
    •  In Linux (Shell), there are two types of variable:System variables-: Created and maintained by Linuxitself and defined in CAPITAL LETTERS.User defined variables (UDV)-: Created andmaintained by user. This type of variable defined inlower letters.VARIABLE IN SHELLS
    • SYSTEM VARIABLES :System Variable Meaning1. BASH=/bin/bash Our shell name2. HOME=/home/kumar Our home directory3. PWD=/home/students/Common Our current working directory4. USERNAME=kumar User name who is currently login to PC5. SHELL=/bin/bash Our shell name
    • TEST OPERATORSTest Works in 3 Ways:o Numeric comparisono String comparisono File tests
    • EXAMPLE OF NUMERIC COMPARISON$ a=5; b=7; c=7.2$test $a –eq $b ; echo $?1$test $a -lt $b ; echo $?0$test $c –gt $b ; echo $?1$test $c –eq $b ; echo $?0
    • EXAMPLE OF FILE TESTS$ ls –l emp.txt-rw-rw-rw- …$ [ -f emp.txt ] ; echo $?0$ [ -x emp.txt ] ; echo $?1$ [ -d emp.txt ] || echo “file is not a directory”file is not a directory
    • STRING TESTTest True ifS1=s2 String s1=s2S1!=s2 String s1 is not equalto s2-n stg String stg is not nullstring-z stg String stg is nullstring
    • Conditions (Case)case expr inOption1) stmt ;;Option2) stmt ;;*) stmt ;;esacEvery option should be terminated with a doublesemicolon.Denotes default caseCase should be termniated with esac
    • ARRAYInitialising an arrayA[0] = 10A[1] = HiUsing an array${A[0]} : retrieves 10Here arrays can contain data belonging to differentdata typesUninitialised index in arrays will have null value bydefault
    • FUNCTIONLocal FunctionWritten at the command promptLasts for the current session aloneGlobal FunctionWritten in .bashrc fileAvailable as long as the definition is there in.bashrc fileFunction in shell scriptAvailable with in the file alone
    • ALIASAlias – Alternate name for an entityEntity here refers to commandWe can give another name or alias name for acommand either at the command prompt or in the.bashrc file.The former will be temporary and will vanish if thesession ends and the latter will be permanent aslong as the definition exists in the .bashrc file
    • Alias and Unaliasalias newname=oldnameEg alias copy=cpThen we can use copy in the same way we use cpcommandEg copy file1 file2 //copies content of file1 to file2To remove alias use unalias commandunalias copyAfter this we cannot use copy to perform copyingfunction
    • export : EXPORTING SHELL VARIABLES
    • Reference:1. A Practical Guide to Ubuntu Linuxby MARK G SOBEL2. Linux Foundation Organisation
    • Thank you!!