Example: air traffic controller

Linux Shell Scripting Tutorial - kau.edu.sa

Linux Shell Scripting Tutorial Ver. by Vivek G GiteI N D E XIntroductionKernelm Shellm How to use Shellm Common Linux Command Introductionm l ProcessWhy Process requiredm Linux commands related with processm l Redirection of Standard output/inputRedirectorsm Pipesm Filtersm l Shell ProgrammingVariables in Linuxm How to define User defined variablesm Rules for Naming variable namem How to print or access value of UDV (User defined variables)m How to write Shell scriptm How to Run Shell Scriptsm Quotes in Shell Scriptsm l Linux Shell Scripting (1 of 2) [17/08/2001 ] Shell Arithmeticm Command Line Processing (Command Line Arguments)m Why Command Line arguments requiredm Exit Statusm Filename Shorthand or meta Characters ( wild cards)

Linux Shell Scripting Tutorial Ver. 1.0 Written by Vivek G Gite I N D E X Introduction Kernel Shell How to use Shell Common Linux Command Introduction Process Why Process required Linux commands related with process Redirection of Standard output/input

Tags:

  Linux

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Linux Shell Scripting Tutorial - kau.edu.sa

1 Linux Shell Scripting Tutorial Ver. by Vivek G GiteI N D E XIntroductionKernelm Shellm How to use Shellm Common Linux Command Introductionm l ProcessWhy Process requiredm Linux commands related with processm l Redirection of Standard output/inputRedirectorsm Pipesm Filtersm l Shell ProgrammingVariables in Linuxm How to define User defined variablesm Rules for Naming variable namem How to print or access value of UDV (User defined variables)m How to write Shell scriptm How to Run Shell Scriptsm Quotes in Shell Scriptsm l Linux Shell Scripting (1 of 2) [17/08/2001 ] Shell Arithmeticm Command Line Processing (Command Line Arguments)m Why Command Line arguments requiredm Exit Statusm Filename Shorthand or meta Characters ( wild cards)

2 M Programming Commandsecho commandm Decision making in Shell script ( if command)m test command or [ expr ]m Loop in Shell scriptsm The case Statementm The read Statementm l More Advanced Shell Script Commands/dev/null - Use to send unwanted output of programm Local and Global Shell variable (export command)m Conditional execution && and ||m I/O Redirection and file descriptorsm Functionsm User Interface and dialog utilitym trap commandm getopts commandm More examples of Shell Script (Exercise for You :-)m l 1998-2000 (I) Pvt. Ltd. All rights Shell Scripting (2 of 2) [17/08/2001 ]IntroductionThis Tutorial is designed for beginners only and This Tutorial explains the basics ofshell programming by showing some examples of Shell programs.

3 Its not help ormanual for the Shell . While reading this Tutorial you can find manual quite useful (type man bash at $ prompt to see manual pages). Manual contains all necessaryinformation you need, but it won't have that much examples, which makes ideamore clear. For that reason, this Tutorial contains examples rather than all thefeatures of Shell . I assumes you have at least working knowledge of Linux basiccommands like how to create, copy, remove files/directories etc or how to useeditor like vi or mcedit and login to your system. Before Starting Linux Shell ScriptProgramming you must knowKernell Shelll Processl Redirectors, Pipes, Filters What's KernelKernel is hart of Linux O/S. It manages resource of Linux O/S. Resources meansfacilities available in Linux . For eg.

4 Facility to store data, print data on printer,memory, file management etc . Kernel decides who will use this resource, for howlong and when. It runs your programs (or set up to execute binary files) It'sMemory resident portion of Linux . It performance following task :-I/O managementl Process managementl Device managementl File managementl Memory managementl What's Linux ShellComputer understand the language of 0's and 1's called binary language, In earlydays of computing, instruction are provided using binary language, which is difficultfor all of us, to read and write. So in O/s there is special program called Shell . Shellaccepts your instruction or commands in English and translate it into computersnative binary is what Shell Does for USLinux Shell Script (1 of 5) [17/08/2001 ]You type Your command and Shell convert it asIt's environment provided for user interaction.

5 Shell is an command languageinterpreter that executes commands read from the standard input device(keyboard) or from a file. Linux may use one of the following most popular shells(In MS-DOS, Shell name is which is also used for same purpose,but it's not as powerful as our Linux Shells are!) Shell NameDeveloped byWhereRemarkBASH ( Bourne-AgainSHell )Brian Fox and ChetRameyFree SoftwareFoundationMost common Shell inLinux. It's (C Shell )Bill JoyUniversity of California(For BSD)The C Shell 's syntax andusage are very similar to the C programminglanguage. KSH (Korn Shell )David Korn AT & T Bell Labs Any of the above Shell reads command from user (via Keyboard or Mouse) and tellsLinux O/s what users want. If we are giving commands from keyboard it is calledcommand line interface ( Usually in-front of $ prompt, This prompt is depend uponyour Shell and Environment that you set or by your System Administrator,therefore you may get different prompt ).

6 NOTE: To find your Shell type following command$ echo $SHELLHow to use ShellTo use Shell (You start to use your Shell as soon as you log into your system) youhave to simply type commands. Following is the list of common Common CommandsNOTE that following commands are for New users or for Beginners only. Thepurpose is if you use this command you will be more familiar with your Shell andsecondly, you need some of these command in your Shell script. If you want to getmore information or help for this command try following commands For To seehelp or options related with date command try$ date --helpor To see help or options related with ls command (Here you will screen by screenhelp, since help of ls command is quite big that can't fit on single screen ) Linux Shell Script (2 of 5) [17/08/2001 ]$ ls --help | moreSyntax: command-name --helpSyntax: man command-nameSyntax: info command-nameSee what happened when you type following$ man ls$ info bashNOTE: In MS-DOS, you get help by using /?

7 Clue or by typing help command asC:\> dir /?C:\> date /?C:\> help timeC:\> help dateC:\> helpLinux CommandFor this PurposeUse this Command SyntaxExample ( In front of $Prompt)To see datedate$ dateTo see who's $ whoPrint working directorypwd$ pwdList name of files incurrent directoryls or dirs$ lsTo create text fileNOTE: Press and holdCTRL key and press D tostop or to end file (CTRL+D)cat > { file name }$ cat > myfile type your text when done press^D To text see filescat {file name }$ cat myfileTo display file one fullscreen at a timemore {file name }$ more myfileTo move or renamefile/directorymv {file1} {file2} $ mv sales create multiple filecopies with various this both oldfilenewfile refers to samenameln {oldfile} {newfile}$ ln Page1 Book1To remove filerm file1$ rm myfileLinux Shell Script (3 of 5)

8 [17/08/2001 ]Remove all files in it very -rf {dirname}$ rm -rf oldfilesTo change file accesspermissions u - User who owns thefile g - Group file owner o - User classified asother a - All other system user + Set permission - Remove permission r - Read permission w - Write permission x - Execute permissionchmod {u|g|o|a} {+|-} {r|w|x} {filename}$ chmod u+x,g+wx,o+x myscriptNOTE: This command setpermission for file called 'myscript' as User (Personwho creates that file ordirectory) has execute permission (u+x) Group offile owner can write to thisfile as well as execute thisfile (g+wx) Others canonly execute file but cannot modify it, Since wehave not given w (writepermission) to them. (o+x).Read your $ mailTo See more aboutcurrently login person( yourself)who am i$ who am iTo login outlogout (OR press CTRL+D)$ logout(Note.)

9 It may askyou password typeyour login password,In some case thisfeature is disabled bySystemAdministrator)Send mail to other personmail {user-name}$ mail ashishTo count lines, words andcharacters of given filewc {file-name}$wc myfileTo searches file for linethat match a {word-to-lookup} {filename}$ grep fox myfileTo sort file in followingorder-r Reverse normal order-n Sort in numeric order-nr Sort in reversenumeric ordersort -r -n -nr {filename} $sort myfileLinux Shell Script (4 of 5) [17/08/2001 ]To print last | first line ofgiven filetail - | + { linenumber } {filename}$tail +5 myfileTo Use to compare filescmp {file1} {file2} ORdiff {file1} {file2}$cmp myfile print filepr {file-name}$pr myfile 1998-2000 (I) Pvt.

10 Ltd. All rights Shell Script (5 of 5) [17/08/2001 ]What is ProcessesProcess is any kind of program or task carried out by your PC. For $ ls -lR , iscommand or a request to list files in a directory and all subdirectory in your currentdirectory. It is a process. A process is program (command given by user) toperform some Job. In Linux when you start process, it gives a number (called PIDor process-id), PID starts from 0 to Process requiredLinux is multi-user, multitasking o/s. It means you can run more than two processsimultaneously if you wish. For To find how many files do you have on yoursystem you may give command like$ ls / -R | wc -lThis command will take lot of time to search all files on your system. So you canrun such command in Background or simultaneously by giving command like$ ls / -R | wc -l &The ampersand (&) at the end of command tells shells start command (ls / -R | wc-l) and run it in background takes next command immediately.