UNIX Scripting - A Quick Reference

Download UNIX Scripting - A Quick Reference

Post on 09-Apr-2018




0 download

Embed Size (px)


<ul><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 1/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Introduction to UNIX</p><p>History of UNIXGE, Bell Labs, and MIT: Network OS; Multiplex Information and Computing Services(Multics)Dennis Ritchie, Ken Thompson, and Brian KerninghamUnix code development language:Assembly Code Machine dependant and not portableB: Ken ThompsonC: Dennis RitchieOpen source development has lead to the availability of various flavors of UNIX.</p><p>Features of UNIXMulti-userThis feature is facilitated by approaches like Dumb Terminals, Terminal Emulation, andDial-up Connection</p><p>Discussion on SSH (Terminal Emulation)&gt;ssh l informat app360.sea.wamu.net&gt;ssh informat@app360.sea.wamu.net</p><p>Shells in UNIXOrganization of the Unix SystemApplications - Shell - Kernel - HardwareBourne Shell (sh), C Shell (csh), Korn Shell (ksh), and Bash Shell (bash)&gt;echo $SHELLThe default shell is set by the admin while creating the user and is given by the abovecommand.Every time a user logs in, a shell process is invoked which acts as the parent process forother processes created and serves as the command interpreter.</p><p>Login and Password resetting/bin/passwd contains details about all the users of the system./etc/passwd executable that allows us to change our passwords.</p><p>Internal and External commandsThe essential UNIX commands for general use are located in /usr/bin. All the commands,which have an independent existence in the above directory, are called externalcommands, while the commands that are built-in and are not stored as separate files arecalled internal commands.&gt;type The above command will give you the location of an external executable command.</p><p>I/O redirection and PipingCombine commands by using the semi colon</p><p>&gt;cd /informat/scripts; ls lrtls &gt; dir.list puts the output of ls into a file named dir.list. ls | sort pipes the output of lsinto the sort command. The final output on the terminal will now be sorted.UNIX I/O revolves around an integer number called a file descriptor. When a file or otherdevice is opened, a file descriptor number is assigned and used for all the I/O. Byconvention, UNIX programs write their normal output to unit 1, and expect input on unit 0.Error/diagnostic output goes to unit 2.</p><p>Unix commands and their help files.Pager pg, more, less</p><p>- 1 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 2/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>&gt;export $PAGER=moreNavigation is through f and b.Discussion on search facility, using the forward slash (/) and how to quit (:q).To get a one line description of the command, useman k, apropos, man f, whatis</p><p>Introduction to Visual Editor (vi) (Part I)</p><p>Modes of OperationIn the Command mode, all the keys pressed by the user are interpreted as commands.In the Insert mode, one is permitted to insert new text.In the ex Command mode, the commands are given at the command line.</p><p>Adding, deleting and overwriting textHands on</p><p>Create a new file.Move into the inset modeType in text.Save data from buffer into the file without quitting.Navigate through the text in the file. (h,j,k,l)</p><p>Append data (within line; before current line; after current line)Delete lines.Overwrite data.Join Lines.</p><p>Undoing changes.u/U Latest change.[1-9]p If you want to undo changes (deletions) which were done earlier.</p><p>Quitting vi. (Temporary and Permanent):wq, ZZ, :x, :q! Permanent:sh Temporary</p><p>Block commands.</p><p>All executed in the ex command mode.Line numbers needed. (se nu, se nonu)Deletion :m,n dMove :m,n mo pCopy :m,n co pAll the above commands make changes within the same file.To copy lines to another file :m,n w filename:m,n w &gt;&gt; filename</p><p>- 2 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 3/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Visual Editor (vi) (Part II): Advanced Features</p><p>SearchMechanism Command Function/pattern Searches forward?pattern Searches backwardN Repeats the last search commandN Repeats the last search command in the</p><p>backward direction.</p><p>Characters to build patterns Character Meaning* Zero or more characters</p><p>[] A set or range of characters$ End of line^ Beginning of line\&lt; Beginning of word\&gt; End of word. Matches any single character except the</p><p>newline character</p><p>How to build patterns?Pattern Meaning^part All lines which begin with the word partpart$ All lines which end with the word part\ All strings which end with the word part.e.g., depart</p><p>\ The whole word part[m-s]ore All strings which contain any character in the</p><p>range m tos and is followed by ore.[^p]art All strings which contain the characters art,</p><p>with art being preceded by any characterother than p.</p><p>Wing* Wing followed any sequence of characters</p><p>Note: If any special character is being searched then they must be preceded with abackslash (\)Search and ReplaceMechanism </p><p>Command Function:s/str1/str2 Replace first occurrence of str1 with str2 in</p><p>current line:s/str1/str2/g Replace all occurrences of str1 with str2 in</p><p>current line:m,n s/str1/str2/g Replace all occurrences of str1 with str2</p><p>from lines m to n.:1,$ s/str1/str2/g Replace all occurrences of str1 with str2</p><p>- 3 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 4/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>from 1st line to end of file.:1,. s/str1/str2/g Replace all occurrences of str1 with str2</p><p>from 1st line to current line.:.,$ s/str1/str2/g Replace all occurrences of str1 with str2</p><p>from current line to end of file.</p><p>abbrabbr command in vi stands for abbreviation. It can be used to enhance the usersconvenience.(Esc):abbr pr printf pr now becomes the shorthand for printf(Esc):abbr Display the entire list of macros you have set.(Esc):una pr The pr abbreviation is forgotten.</p><p>Set Commands and customizing the vi environment(Esc):set all Displays the list of all options available with set.</p><p>Some commonly used options Command Function:se nu Display of line numbers on:se eb Beep the speaker when an error occurs</p><p>:se ai Auto indent on:se ic Ignore case while searching a pattern:se showmode Display mode in which we are working</p><p>Whenever vi is invoked, it searches for the .exrc file in the users default workingdirectory ($HOME). This file is optional, i.e., vi does not give an error if it is not able tolocate this file. However, if the file is present any set commands or abbreviations that arestored in this file automatically become effective for all vi sessions.</p><p>Multiple file editing Command line options in vi</p><p>Command</p><p>Function Command Function</p><p>$vi file1</p><p>file2 file3</p><p>3 files are loaded into vi</p><p>buffer for editing</p><p>$vi + 10 file Loads file and places cursor</p><p>on the 10</p><p>th</p><p>line of the file:n Edit next file in buffer $vi +/pattern file Loads file and places cursoron first occurrence ofmatching pattern</p><p>:n! Edit next file withoutsaving current file</p><p>$view file Opens file in read-onlymode</p><p>:rew Edit 1st file in buffer:rew! Edit 1st file without</p><p>saving current file:args Display names of all</p><p>files in buffer, withcurrent file nameenclosed in []</p><p>:f Displays name of </p><p>current file</p><p>- 4 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 5/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Shell Scripting: Introduction &amp; Decision making statements</p><p>User defined and System defined variablesSystem variables are always accessible. The shell provides the values for these variables.These variables are usually used by the system itself and govern the environment wework under. If we so desire we can change the values of these variables as per ourpreferences and customize the system environment. For e.g.,PS1 : System Prompt 1.PATH : The path, which the shell must search in order to execute any command or file.HOME : The default working directory of the user.SHELL : Default working shell.MAIL : The file where the mail of the user is stored.</p><p>MAILCHECK : Duration after which the shell checks whether the user has received anymail.LOGNAME : Login name of the userThe value of the system variables can be displayed by typing the following command atthe prompt echo $.</p><p>User defined variables are used most extensively in shell programming. To access thevalue of any kind of variable, we need to precede the variable name with a dollar ($) sign.</p><p>1. All shell variables are string variables. We cannot carry out arithmetic calculations onthem unless we use a command called expr.</p><p>2. While using the assignment operator to assign values to variables, there should be nospaces on either side of =. During assignment, if the variable does not exist it will becreated.</p><p>3. If the variable needs to contain more than one word then the assignment should bemade using double quotes.</p><p>4. We can carry out more than one assignment in a line. So also we can echo more thanone variables value at a time.</p><p>5. All variables defined inside a script die the moment the execution of the script is over.6. A constant variable can be created by saying $&gt;readonly .7. Variables can be made to cease existing by using the unset command. $&gt;unset</p><p>. This command cannot be used on system variables.8. The entire user defined variables and system defined variables can be displayed by</p><p>using the command set at the command prompt.</p><p>Positional parameters (setting and shifting)The shell script uses positional parameters to access the command line arguments whichare passed to it. Positional parameters are 9 in number, named $1 through $9.The set command can be used to assign values to the positional parameters.$&gt;set We are now trying to understand the setting of positional parameters withoutcalling a script$&gt;echo $1 $2 $3 $4 $5 $6 $7 $8 $9$&gt;echo Total number of arguments: $#$&gt;echo All positional parameters: $@$&gt;echo After shifting by 5 positions$&gt;shift 5; echo 1 $2 $3 $4 $5 $6 $7 $8 $9</p><p>Exit status of a command</p><p>- 5 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 6/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Every UNIX command/script returns an exit status of zero, if it has been successfulexecuted, otherwise, a value greater than zero is returned.$&gt;echo $?$? stores the exit status of the last executed command.</p><p>Things to be noted while using arithmetic in shell scripts.$&gt;echo `expr $a \* $b`</p><p>1. The multiplication symbol should be preceded by a \. Otherwise, the shell treats it as awildcard character for all files in the current directory.</p><p>2. Terms of the expression provided to expr must be separated by blanks.</p><p>3. expr is only capable of carrying out integer arithmetic. To carry out arithmetic on realnumbers it is necessary to use the bc command.</p><p>Numerical Test Operators</p><p>Operator Meaning-gt Greater than-lt Less than-ge Greater than or equal</p><p>to-le Less than or equal to</p><p>-ne Not equal to-eq Equal to</p><p>File Tests</p><p>Option TRUE if -s File exists and has size greater</p><p>than zero-f File exists and is not a directory-d File exists and is a directory-r File exists and user has read</p><p>permission on it-w File exists and user has write</p><p>permission on it</p><p>-x File exists and user hasexecute permission on it</p><p>String Tests</p><p>Operator TRUE if string1 = string2 Strings are same-n string Length of string &gt; 0-z string Length of string = 0string String is not a null</p><p>string</p><p>Logical operators</p><p>Operator Meaning</p><p>-a AND-o OR! NOT</p><p>The Case Control Structure</p><p>- 6 -</p><p>case value inchoice 1)</p><p>....;;</p><p>choice 2)....;;</p><p>*)..;;</p><p>esac</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 7/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Example Problems and Solutions.A. Code Snippets </p><p> Interaction with the user Command line arguments.</p><p>B. Program Example 1. The file /etc/passwd contains information about all users. However it is difficult to</p><p>decipher the information stored in it. Write a shell script which would receive thelogname during execution, obtain information about it from /etc/passwd and displaythis information on the screen in easily understandable format.</p><p>2. If the cost price and selling price of an item is input through the keyboard, write a</p><p>program to find out whether the seller has made profit or incurred loss. Alsodetermine how much profit was made or loss incurred.</p><p>Code Snippet 1: Interaction with theuser</p><p>script_1.ksh</p><p>Code Snippet 2: Command linearguments. script_2.ksh</p><p>Program Example 1. script_3.ksh</p><p>Program Example 2. script_4.ksh</p><p>- 7 -</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 8/12</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 9/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Code Snippet 1: Interaction with theuser</p><p>script_1.ksh</p><p>Program Example 1. script_2.ksh</p><p>Shell Scripting: Miscellaneous Information</p><p>Difference between executing a script normally and with a .The preceding dot (.) while executing a script ensures that the script gets executed in thecurrent shell. A space between the . And the script name is mandatory. If it is absent theshell considers the script as a hidden file.</p><p>When do we need to export?If we want variables to be available to all sub-shells we must export them from the</p><p>current shell.$&gt;export a. A variable once exported from the parent shell becomes available to the sub shell or</p><p>any other shells launched from this shell.b. If we are to unexport a variable, we have to first unset it and then recreate it.c. A variable can be exported from the parent shell to its sub shell, but never the other</p><p>way round.d. If the sub-shell changes the value of an exported variable the value of this variable in</p><p>the parent shell remains unchanged since the sub-shell always works only the copy ofthe variable in the parent shell.</p><p>e. If an exported variable is modified in a sub shell then to make this modified valueavailable to a sub-sub-shell we must once again export the variable in the sub-shell.</p><p>Trapping signals and Terminal settings.</p><p>A few examples on trappingtrap 123 Does nothing on generation of signal numbers 1,2 or 3trap ls;who 2Displays file listing and current user listing on generation of signal number2.</p><p>Terminal Settings</p><p>- 9 -</p><p>Signal Signal NumberExit 0Ctrl d 1Del 2Kill 15</p><p>Setting Actionecho Echoes back every character typed-echo Does not echo back every character typedsane Resets all modes to some reasonable values.</p><p>By trapping the program terminating signalsshown in the side table, we can use them toachieve some other task totally unconnected withtheir original duties, and hence ignore the taskthey were initially instructed to do.</p></li><li><p>8/8/2019 UNIX Scripting - A Quick Reference</p><p> 10/12</p><p>Wipro Technologies; E-Enabling; Data Warehousing</p><p>Here documentThe HERE document signifies that the data is here instead of in a separate file. Anycommand using statndard input can also take input from a here document.Example (Executing Oracle procedures from Unix scripts)</p><p># ---------------------------------------------------------------# Calling Database procedures from a UNIX script</p><p># ---------------------------------------------------------------# Script Name call_sortbydisplayorder.sh# Usage sh call_sortbydisplayorder.sh# ---------------------------------------------------------------(sqlplus s user/password@h...</p></li></ul>


View more >