Introduction. The UNIX Operating System is divided into three major components:

Size: px
Start display at page:

Download "Introduction. The UNIX Operating System is divided into three major components:"

Transcription

1 Introduction 11 The UNIX System The UNIX Operating System is divided into three major components: The Kernel This is the heart of the UNIX Operating System It performs the tasks that create and maintain the UNIX environment It keeps track of the disks, tapes, printers, terminals, communication lines and any other devices attached to the computer It also interfaces between the computer's hardware and the users The Shell This is a program that interfaces between the user and the UNIX Operating System It listens to the user's terminal and translates the actions requested by the user There are a number of different Shells that may be used Utilities and Application Programs Utilities are the UNIX Commands Application Programs, suchas Word Processors, Spreadsheets and Database Management Systems,may be installed alongside the UNIX Commands A user may run autility or application through the shell 12 The Shell The Shell is a program provide an interpreter and interface between the user and the UNIX Operating System It executes commands that are read either from a terminal or from a file Files containing commands may be created, allowing users to build their own commands In this manner, users may tailor UNIX to their individual requirements and style There are a number of different Shells Each provides a slightly different interface between the user and the UNIX Operating System The most important shells are: Bourne Shell - sh Although UNIX was first created as far back as 1969, it was not until 1979, with the release of Version 7, did UNIX have a well-defined shell - the Bourne shell The Bourne shell was written originally by Stephen Bourne of AT&T Bell Laboratories, with some help and ideas from John Mashey At first, the shell was very fundamental Because of the architecture of the machines it was developed for, facilities such as the use of filename metcharacters (wildcards) was a separate program - to keep down the size of the shell Over the year, the Bourne shell has expanded in its utilities While the industry standard shell, it is a fundamental and small shell, lacking the facilities of the other two shells

2 C Shell - csh This shell was written by William Joy at the University of California at Berkley It is available on machines running Berkley UNIX or those with the Berkley enhancements It was meant as a replacement for the Bourne shell The shell is a command interpreter with facilities for decision making and looping that resemble those of the C programming language It contains facilities to create aliases for long, complex commands and a history facility to recall old commands However, its syntax is quite different from the Bourne shell and is more difficult to use Korn Shell - ksh This shell is a relatively new shell It was developed by David Korn at AT&T Bell Laboratories He designed it to be upwards compatible with the Bourne shell, so that programs written for the Bourne shell also run under the Korn shell Apart from a few minor differences, the Korn shell provides all the features of the Bourne shell with many extra facilities, such as an alias facility, history mechanisms, arrays and integer arithmetic There are other shells that are less widely used and not available on many machines For example, there is the Restricted Shell - rsh This restricts the area of memory the user may access to his or her own directory, thus limiting access to all other users' files

3 The Shell Environment A command issued by a user may be run in the present shell, or the shell itself may start up another copy of itself in which to run that command In this way, a user may run several commands at the same time A secondary shell is called a subshell When a user logs onto the System, a shell is automatically started This will monitor the user's terminal, waiting for the issue of any commands The type of shell used is stored in a file called passwd in the subdirectory etc Any other shell may be run as a sub-shell by issuing it as a command For example, /bin/ksh will run a Korn shell The original shell will still be running - in background mode - until the Korn shell is terminated 21 The UNIX Process The address space for a UNIX process is organised in five sections: The Header This contains information about the command line arguments and the environment parameters The Text Segment This contains the executable code of the process and associated library functions The Data Segment This contains global definitions, static variables and constants The Stack This contains local variables, function parameters and manifest constants set up by loops, etc I/O Buffer Space This contains the I/O text and requirements of the process 22 Passing Information to a UNIX Command There are several methods of passing information to a UNIX command - or, for that matter, any applications program running in the UNIX environment:

4 Command Line Arguments The syntax of a UNIX command is generally of the form: command [options] [expression] [filenames] All options, expressions and filenames - as well as the command's name - are passed into the command's address space header Environment Parameters An environment parameter behaves very much like a program variable That is, it stores information that may be used in the present shell, or be passed into a command's address space header when run is a subshell Read Information Information may be passed to a command, if the command has any inbuilt read statements These vary, depending on the language of the command: fgetc, fgets, fscanf, fread, read, etc for a compiled C program read for a UNIX shell script 23 Shell Variables A shell variable stores information For the Bourne shell, this information is stored in character format - strings of characters, even if the information is, apparently, numeric The Korn shell allows both character and numeric information to be assigned to shell variables The syntax for the assignment of a value to a shell variable is: variable=value A variable's name: For example: Can only consist of: Alphabetic characters (a to z, A to Z) Numeric characters (0 to 9) The underscore ( _ ) Must start with an alphabetic or the underscore character Can be any length $ firstname=jon $ surname=harvey $ count=100

5 Notes: 1 There are no spaces left either before of after the equals sign 2 If the variable exists already, an assignment will overwrite its previous value 3 If the variable did not exist, an assignment creates the variable 4 A NULL value may be assigned to a variable This may be done in three ways: variable= variable="" variable='' 24 Types of Variable There are a number of different types of variables available in the Bourne and Korn shells Variables are referred to as parameters if they are to be passed onto a subshell: Character Variable This is the default type of variable Its value is held as a character string Environment Parameter A variable requires to be exported to become and environment parameter See Section 28 Keyword Parameter This is used to give variables values at execution time It may be exported to be passed onto any utility run in a subshell, or may be passed by name by: key_param1=value1 key_param2=value2 utility Positional Parameter A positional parameter's value is passed onto a utility by the position of an argument on the command line Null Variable This is a declared variable that has not been given a value See Section 23 Readonly Variable This is a variable on which the write permission has been removed That is, its value may be used, but not modified The syntax of the command is: readonly variable1 variable2 Fixed-Length Variable (Korn Shell only) The use of the typeset command to define a variable permits a variable to accept a fixed number of characters The variable may be either left or right justified - ie, a character

6 string is either trimmed or padded with blanks to fill the fixed number, either to the right or left, respectively Integer Variable (Korn Shell only) The Korn shell allows variables to be defined as containing integer values That is, a numerical value is not treated as a character string, like all other variable types 25 The Set and Unset Commands To list all variables in the present shell, issue the set command, without any arguments To remove a variable from the present shell, issue the command: unset variable 26 The Contents of a Variable To access the value stored as the content of a variable, a dollar sign ($) has to be placed before the variable's name For example: $ echo $firstname $ echo $surname $ echo $firstname $surname 27 Variable Substitution Variables may be used in substitute for long or complex pieces of text For example: $ mydir=/usr/students/student1/shells $ ls -l $mydir $ mv scrpit1 $mydir/ $ cd $mydir 28 Environment Parameters Environment parameters are special variables Whereas most variables are available to the shell within which they are created, environment parameters are passed to any subshell Environment parameters are created in the same way as any other variable However, to make an ordinary variable into and environment parameter, the variable has to be placed on an export list The syntax of the command is: export variable-list For example: $ MYDIR=/usr/students/student1/shells $ export MYDIR $ INFORMIXDIR=/usr/informix

7 $ DBDATE=dmy4/ $ DBPATH=/usr/dbs/forms:/usr/dbs/reports $ export INFORMIXDIR DBDATE DBPATH Notes: 1 Unlike ordinary variables, it is the convention to use uppercase letters for environment parameter names 2 On most modern UNIX systems, once a variable has been placed on the export list, to become an environment parameter, it does not have to be re-exported if the parameter's value is changed However, older versions of UNIX will not recognise the change of value until the parameter has been re-exported 29 The ENV Command The env command lists all the environment variables and their values in the present shell The command is invoked by its name alone: env 210 The User's Environment When a user logs onto a system, the user's environment is set for him It is this environment which dictates the scope of the user's abilities on the system, as well as storing personal details This environment is stored as Environment parameters The login procedure sets up a number of parameters for the user's environment Some are set up automatically by the system Others are created via three files: /etc/passwd - user id, default group, Home directory and default shell /etc/profile - for general environment details profile (in the user's Home directory) - for user-specific details Note:A list of environment parameters not automatically generated by the system may be displayed using the env command, without any arguments A list of important parameters are: HOME LOGNAME The pathname of user's Home directory The user's login name

8 IFS MAIL PATH PS1 PS2 SHELL TERM Inter-Function Spacing - the characters that are recognised as the permitted spacing between arguments and commands (default: space, tab and newline) The pathname of the user's mail file The pathnames searched automatically for any commands the user issues The command-line (primary) prompt The continuation (secondary) prompt The default shell used at login The terminal type - required for facilities, such as vi 211 Customisation of the User's Environment The user's environment may be modified at any time This may be achieved by changing the value of an existing environment parameter and adding new parameters to the environment For example, to add an extra search path to the PATH parameter: $ PATH=$PATH:/usr/students/student1/shells Note: For any search to include the present directory, make certain that the PATH parameter includes : - a colon followed by a dot This is extremely useful for running shell scripts Customisation of the user's environment may be achieved at two different levels: System Administration Level The system administrator should be the only person to be able to modify the files /etc/passwd and /etc/profile As stated in Section 28, the first sets up the parameters HOME and SHELL, as well as identifying the user's id and membership of a default group The second file permits the system administrator, not only to set up the user environment on a general level, but also permits - via shell programming - to set up more specific parameter values, dependant on various tests For example, the terminal type may be tested and the TERM parameter set, depending on the result of the test: case `tty` in /dev/tty1a) TERM=wyse60 ; export TERM ;; /dev/tty0[1-9] /dev/tty1[12]) TERM=ansi ; export TERM ;; esac User Level

9 If the user has read and write permissions on the Home directory's profile file, personalised parameters, as well as messages, other information and the execution of utilities may be stored in it For example: $ echo "Welcome to the system\nyour login time is `date -T`\n" $ echo "There are `who -u wc -l cut -c6-8` users on the system` $ PATH=$PATH:$HOME/shells $ PS1="$LOGNAME "

10 Exercises Chapter Two 1 Name the five sections of the address space of the UNIX process 2 What is meant by background mode when applied to an executed UNIX command? 3 What is the general syntax for command line arguments? 4 What is an environment parameter? In what manner do they differ from other shell variables? 5 Give one method of declaring a null variable 6 What are the restrictions for the naming of a shell variable? 7 Name two types of variable that are peculiar to the Korn shell 8 What commands perform the following: a) List the environment parameters in a shell b) Remove a variable from the shell c) Re-list the environment parameters in a shell to see the effect of part b 9 Change your shell (primary) prompt to your login name, followed by a colon and a space 10 Create a shell variable that is set to your full name Echo the variable's contents to confirm that it has been created correctly 11 Append your age to your fullname variable Echo the variable's contents to confirm that it has been appended correctly 12 Make your fullname variable readonly Try to append the text "Shell Programming" to it What is the response? 13 Make your fullname variable into an environment parameter Run the env command to confirm its status 14 Remove your fullname variable from the shell Run the env command to confirm that it has been removed

11 Command-Line Processes The simplest method of initiating a shell process is to run a command, or string of commands, from the command line A shell process may contain any of the following: Shell commands I/O redirection symbols Pipes Background processing Wildcards Parameters and variables Conditional processing Further details of parameters and variables and explanations and examples of conditional processing will be given in later chapters 31 I/O Redirection It is often convenient to obtain input from or send the output or error messages to a file For example, a series of frequently updated filenames may be stored in a file and vi may edit each of the filenames in turn by accessing this file for its input Another example would be to store any error messages into a file so that they may read later To redirect the standard I/O, the following symbols are used: < Redirect Input Example: $ vi < filelist where filelist contains a list of filenames > Redirect Output (Overwrite) Example: $ ls *c > cprograms cprograms will contain a list of all C programs in working directory >> Redirect Output (Append) Example: $ ls /usr/nic/*c >> cprograms

12 this will append all C programs in the directory /usr/nic to cprograms 2> Redirect Error(Overwrite) Example: $ mkdir dir1 dir2 dir3 2> errfile errfile will contain any error messages resulting from trying to make any of the three directories 2>> Redirect Error (Append) Example: $ rmdir dir4 dir5 2 >> errfile this will append to errfile any error messages resulting from trying to remove either of the directories >& 2 Redirect Standard Output to Standard Error Example: echo "This is an error message" >&2 2>& 1 Redirect Standard Output and Standard Error to the Same File Example: 32 Piped Commands cat file1 file2 file3 > outfile 2>&1 As stated in Section 31, Standard I/O may be redirected from or to a file It often occurs that the output from one command needs to be reprocessed by another command, etc Using Standard I/O Redirection, this would require a series of temporary files to be created The process is slow and requires some tidying up to be done afterwards A pipe permits the output from one command to be passed directly as input to another command The symbol for a pipe is Examples: The following paginates the list of files in the long directory /bin: $ ls -l /bin pg The following prints out all C program files in the working directory in a pr format: $ ls *c pr -l60 -o6 lp -o length=60 -s 33 Background and Foreground Processing When a sub-shell is created to execute a command, the parent shell is run in the background, until the sub-shell terminates At times, this is very inconvenient, particularly when the command is going to take a considerable time to execute

13 Commands may be forced into the background, allowing the parent shell to run in the foreground, thus permitting the user to continue with other tasks To force a command into the background, end the command line with an ampersand (&) The shell will display the process id number of the command and then display the next command line prompt For example: $ ls -lar / > allfiles 2> /dev/null & [1]427 $ Note:It is a good idea to make a note of the process id number of each background command, so that it may be killed off at any time However, the command: kill -9 $! will kill the last background command issued 34 No Hangup When a user logs off, all active processes are killed automatically This includes any background processes To permit any background processes to continue, after the user has logged off, they have to be disconnected from the user's shell This is achieved by using the no hangup utility, nohup The syntax of its use is: nohup command & Any output from the command will be stored in the user's Home directory in the file named nohupout 35 The echo Command Although the echo command is apparently simple, it can be used to great effect within a shell process For example: $ echo This is a test This is a test $ echo $HOME /usr/students/student1 $ echo * file1 file2

14 This last example prints a list of files in the present directory 36 The Use of Quotes There are several reasons for requiring quotes in a shell command For example, a string of characters may contain white space (spaces, tabs and newline characters) Without the use of quotes, each portion of the string would be treated as a separate entity There are three types of quotes that may be used in a shell command: 361 Single Quotes (' ' ) These treat anything that lies within them as a literal string of characters For example: $ echo 'This is a test' This is a test $ echo '$HOME' $HOME Whereas: $ echo $HOME /usr/students/student1 The following example will search for the string asterisk (*) in file1, rather than treat the asterisk and a wildcard: $ grep 'asterisk (*)' file1 362 Double Quotes (" ") These work similarly to single quotes, with one great exception - they allow for the expansion of anything within them, such as wildcards, the contents of variables and special characters For example: $ echo "$HOME" /usr/students/student1 The following example will search for the string asterisk immediately followed by anything in parentheses in file1: $ grep "asterisk (*)" file1 363 Command Quotes (` `) These allow commands to be embedded inside other shell commands The results of the embedded commands will then be used as part of the outer commands For example: $ echo The time is `date +%T`

15 The time is 10:24:05 $ echo There are `who -u wc -l` users logged on There are 12 users logged on 37 The Backslash The backslash is used to: Indicate Special Characters There are many characters that require special handling if used in a command For example: $ grep > file1 would cause an error message to be printed as the character is the symbol for the redirection of Standard output If the character is to be searched for in file1, the following revised command may be used: $ grep \> file1 Another example is: $ echo \" \' \` are the three types of quotes that may be used " ' ` are the three types of quotes that may be used Identify Escape Sequences These are in direct relationship to those in the C programming language: \b Backspace \c Suppress a newline \f Formfeed \n Newline \r Carriage return \t Tab \0nnn nnn represents an octal ASCII character representation For example: $ echo "One\tTwo\nThree" One Two Three

16 Note: Without the double quotes, the above example would have to be written as: $ echo One\\tTwo\\nThree This is because, without the double quotes, a backslash is required to identify the second backslash as a special character Cause Continuation of a Shell Command on Subsequent Lines For example: $ echo This is a \ > disjointed string \ > that is being entered over \ > several lines \ >!\ > <Return> This is a disjointed string that is being entered over several lines! 38 Multiple Commands on a Command-Line Instead of writing a single command at a shell prompt, a series of commands may be written, separated by a semi-colon (;) For example: $ MYDIR=/usr/students/student1/shells; export MYDIR is equivalent to: $ MYDIR=/usr/students/student1/shells $ export MYDIR 39 The ( ) Construct A command or a set of multiple commands placed within a set of parentheses are executed in a subshell The syntax of the construct is: (command1; command2; ) The construct may be convenient both on the command line and in a shell script when certain commands must not affect the status of the present shell For example, the command cd runs in the present shell, so that it does change directory Therefore, if the following command is executed: $ cd /bin; ls -l the present working directory will be /bin However, if the following command is executed: $ (cd /bin; ls -l)

17 the present working directory will be unchanged 310 The { Construct A command or a set of multiple commands placed within a set of braces are executed in the present shell The syntax of the construct is: { command1; command2; ; Note the differences between the syntax of this and the ( ) construct: White space must be left between the braces and the commands The last command must be followed by a semi-colon - as must all the other commands The uses of this construct are: Instances where the combined output from a number of commands must be processed together: $ { ls /bin; find /etc -user root -print; > rootfiles $ { command1; command2; command3; 2> errorfile Commands that would normally not effect the present shell status will now do so: $ { uniplex; isql; i4gl; exit 0; In this example, the applications packages Uniplex, Informix-SQL and Informix-4GL are run sequentially with an exit from the shell as the final command If this was part of a shell script, the exit command would terminate the present shell, thus logging off the user

18 Exercises Chapter Three 1 Give an example of: a) Redirection of Standard Output to a file b) Redirection of both Standard Output and Standard Error to the same file c) The removal of error messages from output 2 What is the difference between the redirection of Standard Output using the > and the symbols? 3 Write the command to display both the user's Home directory and the search Path 4 What is the main difference between the use of the single and double quotes to echo information to Standard Output? 5 What is a special character? 6 How may a: a) Long command be entered over several lines? b) Number of commands be entered on a single line? 7 What is the main difference between the use of the uses of the ( ) and { ; shell constructs? 8 Create a shell variable that contains a list of all files in the present working directory Use the variable to paginate through the files' contents 9 Create a shell variable that contains the command: ls -l Use the variable to execute the following command: ls -la /bin What precautions must you take to ensure that the command is created correctly? 10 Modify the profile file in your Home directory such that it displays: a) The calendar for the current month b) The date and time of login c) The login name and Home directory d) How many users are logged in

19 Executing an Applications Program An applications program may be: A UNIX command or command construct A shell script An executable program Both UNIX commands and executable programs are, by nature, executable That is, by stating such an application's name on the command line - with or without arguments - the application will execute For example: $ ls -la $ uniplex A shell script is a sequence of commands stored within a file Such a file may not be inherently executable To run shell script, there are a number of commands that may be used The execution of a shell script depends on whether or not the script is to be run in the present shell or in a subshell - hence affecting or not the present shell's status 41 Subshell Execution There are several methods by which a shell script may be executed in a subshell: 411 Executable Script Files A shell script may be made inherently executable by changing its permissions Each file within the UNIX environment has three sets of three permissions: User level: Owner's permissions Group's permissions All other users' permissions

20 Permission types: Read permission Write permission Execute permission By changing access permissions on a shell script file so that it is executable allows the file to be run by entering its name on the command line: chmod 550 script_file script_file Note that the above example: Makes the file executable for the owner and owner's default group All other users will have no access to the file No user has write permission on the file This safeguards the shell script from being overwritten accidentally All users that may run the file also have read permission This is required as the file has to be read for the commands it contains to be executed 412 The sh and ksh Commands The commands sh and ksh execute a subshell Both have the ability to run an application within that shell, whether or not the application is inherently executable The syntax for such subshell execution is: sh script_file or ksh script_file 413 The TIME Command The time command executes an application and prints out the real, user and system times of its execution to the nearest hundredth of a second While not a substitute for the sh and ksh commands, it does have the advantage of giving the user an indication of the efficiency of a shell script process For example: $ time ls -l Output from ls -l real 02

21 sys 01 user Present Shell Execution There are two methods of forcing an application to run in the present shell: 421 The DOT Command The dot command runs an application, whether or not it is inherently executable, in the present shell Any modifications initiated by the application will effect the present shell The syntax of the command is: script_file 422 The EXEC Command The exec command swaps out the present shell process and swaps in the application This means that, when the application terminates, there is no user shell to return to Therefore, the user is automatically logged off The syntax of the command is: exec script_file Usually, the command is placed as the final entry in a user's profile file Therefore, on login, the application is the only process the user has access to and is logged off on its termination It is an extremely useful device for users that require access to a single application, without the problems involved in using the UNIX environment

22 Exercises Chapter Four 1 What are the differences in running a shell script program using: a) sh b) ksh c) d) exec 2 What are the minimum permissions required on a shell script program file for the owner to execute the script inherently? 3 What information does the time command present to the user? 4 Create a shell script program that: a) Lists the terminal type from the environment parameters b) Lists the terminal number - from the UNIX tty command c) Displays the present time d) Lists the number of users on the system e) Changes to the root directory f) Lists all files in the root directory 5 Run the shell script program created for Exercise 4: a) Using the Bourne shell command b) By changing its permissions to executable and running it inherently c) Using the time command d) Using the dot command e) Using the exec command Between each run, check on the present working directory

23 Simple Shell Scripts As stated in the previous chapter, a shell script is a sequence of commands stored within a file In their most simple form, each command in the file is executed sequentially Consider, for example, the following shell script file contents: mv /usr/adm/sulog /usr/adm/osulog chmod 600 /usr/adm/sulog mv /etc/log/filesavelog /etc/log/ofilesavelog chown root /etc/log/filesavelog chgrp sys /etc/log/filesavelog chmod 666 /etc/log/filesavelog cd /lost+found find -mtime +14 -exec rm -rf { \; > /dev/null 2>&1 All commands should be familiar to the student: mv, chmod, chown, chgrp, cd, find and rm This is, however, part of the UNIX cleanup routine that invoked on system boot In fact, much of the system boot processes, as well as the user login process, are reasonably simple shell scripts 51 Shell Script Comments Shell scripts, like any program source files, should be well documented To include comments within a shell script, start the comment with the hash (#) symbol This informs the system that the remainder of the line is a comment and must be ignored during execution For example: $ # echo This will be ignored on execution $ $ echo This will not be ignored # However, this part of the line will be $ This will not be ignored 52 Passing Variables to Shell Scripts As stated in Section 24, there are two methods of passing the contents of variables to shell scripts for inclusion in their execution: Environment Parameters These are made available to the shell script by their presence in the address space of the script's UNIX process (see Section 21)

24 Keyword Parameters These are variables that are give values at execution time They may be passed to the shell script by their evaluation on the command line, immediately preceding the script's execution: key_param1=value1 key_param2 =value2 sh script_file For example, consider the shell script file myname If its contents are: echo $first $second echo $second, $first The following command line process will produce the output: $ first=fred second=bloggs sh myname Fred Bloggs Bloggs, Fred $ 53 Positional Parameters Positional parameters are the arguments passed to an application on the command line For example, consider: $ cp file1 file2 The cp command has two positional parameters, or arguments: file1 is positional parameter 1 file2 is positional parameter 2 The name of the command itself is a positional parameter - at position 0 However, it is treated specially within a shell script's execution The contents of a positional parameter may be accessed in the same manner as the contents of any variable - by placing the dollar sign ($) in front of its positional number However, only the first nine positional parameters may be accessed in this way: $ sh script_file arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10 $0 $1 $2 $3 $4 $5 $6 $7 $8 $9 For example, consider the shell script file myname If its contents are: echo $0 is: echo $1 $2 echo $2, $1 The following command line process will produce the output: $ sh myname Fred Bloggs myname is: Fred Bloggs Bloggs, Fred $ Note that the following command line process will produce the output: $ sh myname "Fred Marmaduke" Bloggs

25 myname is: Fred Marmaduke Bloggs Bloggs, Fred Marmaduke $ As stated in Section 36, the use of double quotes holds a string of characters together as a single entity 54 The SHIFT Command As stated in the previous section, only the contents of the first positional parameters may be accessed individually However, the shift command moves lower-order positional parameters out of range and high-order parameters into their positions The syntax of the command is: shift [n] The number n is optional If a number is stated, that number of parameter positions are shifted If no number is stated, a single position is shifted For example, consider the following shell script file, big_shift: echo $1 $2 $3 $4 shift echo $1 $2 $3 $4 shift 2 echo $1 $2 $3 $4 shift 3 echo $1 $2 $3 $4 The following command line process will produce the output: $ sh bigshift one two three four five six seven eight nine ten one two three four two three four five four five six seven seven eight nine ten $ Now consider the following command line process This will produce a different output: $ sh bigshift "one two" three four five "six seven eight" nine ten one two three four five three four five six seven eight five six seven eight nine ten ten $ This is because "one two" and "six seven eight" are now single positional parameters and are treated as such by their $positions and the shift command

26 55 Special Parameter Variables As well as the nine named positional parameters ($1 to $9) and the name of the application ($0), there are a number of other parameters available to a shell: $* The contents of all arguments passed to the shell $@ The contents of all arguments passed to the shell $# The number of arguments passed to the shell $$ The ID number of the present process $! The ID number of the last background process $? The exit status of the last command Note: The special parameters $* and $@ apparently are the same There is a subtle difference between their uses, which will be covered in a later chapter Examples: 1 Consider the following shell script file, big_shift: echo Number of parameters = $# echo $* shift 2 echo Number of parameters = $# echo $* shift 2 echo Number of parameters = $# echo $* shift 2 echo Number of parameters = $# echo $* The following command line process will produce the output: $ sh bigshift one two three four five six seven eight nine ten Number of parameters = 10 one two three four five six seven eight nine ten Number of parameters = 8 three four five six seven eight nine ten Number of parameters = 6 five six seven eight nine ten Number of parameters = 4 seven eight nine ten 2 $ echo $$ 428

27 This is the ID number of the present shell Consider the following: $ tempfile=/tmp/tmp$$ $ echo $tempfile /tmp/tmp428 The variable tempfile may be used to indicate the file in which to initially store and later retrieve data to be processed The file should be unique within the directory /tmp and each process has an unique ID number The temporary file may be removed later: $rm $tempfile 3 $ find -mtime +14 -exec rm -rf { \; > /dev/null 2>&1 & [1]249 The ID number of the background process is 249 This may be used to terminate the process at any time: $ kill However, if it is the last background process to be executed, to date, the following $ kill -9 $! 4 Consider the following command: $ ls -l Output from ls -l $ echo $? 0 The final output informs that the last command - ls -l - terminated without errors Now consider: $ ls -% ls: illegal option -- % usage: -1ACFRabcdfgilmnopqrstux [files] $ echo $? 2 The final output informs that the last command - ls -% - terminated with errors The value 2 is the error exit status of the command 56 Reading Values into Variables Values may be read directly into variables The syntax of the command is: read [var1 var2 ] The command accepts data entered via Standard Input The first value entered will be assigned to the first-mentioned variable, the second to the second, etc Whatever data is left will be assigned to the final variable For example: $ read a b c Fred Marmaduke Bloggs $ echo $a Fred

28 $ echo $b Marmaduke $ echo $c Bloggs $ read a b Fred Marmaduke Bloggs $ echo $a Fred $ echo $b Marmaduke Bloggs $ read string This is a disjointed string \n $ echo $string This is a disjointed string n Note:If the read command is used without any variables, any data entered will be stored in the variable REPLY For example: $ read Hello World! $ echo $REPLY Hello World! REPLY is not an environment parameter

29 Exercises Chapter Five 1 What symbol precedes documentation text in a shell script? 2 What types of parameter from the present shell may be accessed by a shell script running in a subshell? 3 What is the maximum number of positional parameters that may be accessed by a shell script? 4 What special parameters give the following information: a) The present process's ID number b) The ID number of the last background process c) The exit status of the last command 5 What is meant by the term exit status? 6 What will be the output from the following echo command: $ read a b c d I am Fred Marmaduke Bloggs $ echo $d $c 7 Write a shell script that prints: a) The name of script file b) The number of arguments passed to it c) The arguments passed to it d) The ID number for the process 8 Write a shell script that appends the present directory's full pathname to the PATH environment parameter Execute the script in: a) A subshell b) The present shell After each execution, echo the contents of the PATH parameter to view any changes

30 9 Write a shell script that executes the shell script written for Exercise 7 The new script should pass to the old script all arguments that it receives 10 Write a shell script that displays your specific entry from the output of the command who -u 11 Modify the shell script written for Exercise 10, such that: a) It prompts for and reads in a username b) Obtains the user's entry from the output of the command who -u c) Passes the output as arguments to a second shell script The second shell script should display the username and process ID number only 12 Revise the second script written for Exercise 11 so that it kills off the user's process Test the scripts by using them to log yourself off

31 Miscellaneous Utilities 61 Shell Script Trace Utilities There are two trace utilities that allow the user to debug the execution of a shell script: 611 Verbose Trace (-v) This trace prints each line that is to be executed As such, if there is an error in the script, the line printed immediately prior to an error message will be that which contains the error For example, consider the script file myfault: cd $1 ls - l $2 pwd Now consider the following execution of the script: $ myfault /bin w* - not found l wc who whodo write /bin This may give little indication as to the error in the script Now compare this with the following: $ sh -v myfault /bin w* cd $1 ls - l $2 - not found l wc who whodo write pwd /bin This indicates that there is something wrong with the line "ls - l $2"

32 612 Execution Trace (-x) As a script is executed, this trace prints each line with its variable values substituted in All lines executed are preceded with a plus sign (+) so as to indicate the use of the executable trace: For example: $ sh -x myfault /bin w* + cd /bin + ls - l wc who whodo write - not found l wc who whodo write + pwd /bin 613 The Use of Both Traces Both traces may be used together This will give a full picture of what is happening in the script For example: $ sh -vx myfault /bin w* cd $1 + cd /bin ls - l $2 + ls - l wc who whodo write - not found l wc who whodo write pwd + pwd /bin This prints each line, both before and after the expansion of any variables Note: The output from either trace cannot be piped to a utility such as pg However, Standard Output and Standard Error may be redirected to files which may then be scrutinised at leisure For example: $ sh -vx myfault /bin w* > output 2> traces The file output will contain: l wc

33 who whodo write /bin The file traces will contain: cd $1 + cd /bin ls -l $2 + ls -l wc who whodo write - not found pwd + pwd 62 The SET Command The set command has multiple uses: 621 List Variables The set command, without any arguments, lists all variables that exist in the present shell - both local and exported - in alphabetical order: $ set EDITOR=/bin/vi HOME=/usr/students/student1 HZ=60 IFS= LOGNAME=student1 MAIL=/usr/spool/mail/student1 MAILCHECK=600 OPTIND=0 PATH=/bin:/usr/bin::/usr/students/student1 PS1=$ PS2=> PS3=#? PS4=+ PWD=/usr/students/student1 SHELL=/bin/sh TERM=wyse60 TMOUT=0 VISUAL=/bin/vi 622 Set the Executable Trace Option The executable trace (-x) may be set for the present shell That is, when the trace is on, each command line will be repeated with the expansion of any variables:

34 $ set -x $ echo $HOME + echo /usr/students/student1 /usr/students/student1 $ ls wc - l + ls + wc - l 5 The trace may be switched off at any time by the command: set +x 623 Set the Positional Parameters The set command may be used to reset the positional parameters for the present shell For example: $ set one two three $ echo $1:$2:$3:$4: one:two:three:: $read line This will count the number of words in a line $ set $line $ echo $# 10 Also, consider the shell script file go: set `who -u grep $1` kill -9 $7 If executable, the superuser may use this script to kill off a user by name, rather than process ID number Consider the following use of the script: $ go student1 The command: who - u grep student1 will give the following output: student1 tty01 Sep 23 11: This line contains seven items: student1, tty01, Sep, 23, 11:57,, 264 Setting these as the positional parameters, the process ID number may be accessed by $7, which is then killed off 624 The -- Option If the shell script go was used to kill a non-existent user, the following would occur: EDITOR=/bin/vi HOME=/usr/students/student1 HZ=60 IFS=

35 LOGNAME=student1 MAIL=/usr/spool/mail/student1 MAILCHECK=600 OPTIND=0 PATH=/bin:/usr/bin::/usr/students/student1 PS1=$ PS2=> PS3=#? PS4=+ PWD=/usr/students/student1 SHELL=/bin/sh TERM=wyse60 TMOUT=0 VISUAL=/bin/vi _=/go go[2]: kill: bad argument count The output of the shell variables is due to the fact that `who -u grep fred` returned a null value Therefore, the first line of the script resulted in the set command being run alone (as explained in Section 621) The needless output of the shell variables can be stopped by placing -- after the set command: set -- `who -u grep $1` The -- option suppressed the output from the set command, if it run alone 63 Mathematical Expression Evaluation As stated in Section 24, all variables declared in a Bourne shell are Character variables Also, unless a variable is stated to be Integer in a Korn shell, it will be a Character variable To be able to use a character variable to store a numerical value and to perform arithmetic upon such a value, the expr command must be used The syntax of the command is: expr val1 operator val2 The operators that may be used are: + Addition - Subtraction \* Multiplication / Division % Modulus (remainder) Note:The asterisk required a backslash to precede it This is because the asterisk is a wildcard, when used alone

36 Examples: $ expr $ count=1 $ echo $count 1 $ count=`expr "$count" + 1` $ echo $count 2 Note: The latter expression evaluation contains $count within double quotes This is advisable, not only for this example, but for any expansion of a variable If the variable did not exist or its content was null, without the quotes, the expression would resolve to: $ count=`expr + 1` This would be an invalid use of the expr utility, causing an error to occur that may terminate the shell script 64 Functions A function is a shell script that is not stored as a file Instead, its contents are stored directly in the present shell's data area - along with variables The syntax of a function is: f_name( ) { command1; command2; ; It is inherently executable: by entering the name of the function on the command line, it is executed automatically Examples: $ nu( ) { who - u wc -l; $ nu 12 This prints the number of users presently logged onto the system $ l( ) { ls -l $*; $ l /bin Long list of /bin $ bold( ) { tput bold;

37 Notes:: The format of a function is reminiscent to that of a C program function It uses the { construct to hold its commands As such, all commands in a function affect the present shell Functions are available only to the shell in which they are created Therefore, if they are to be used on a regular basis, they should be placed in the user's profile file 65 The TPUT Command The tput command queries the terminfo database It uses the database to make the values of terminal-dependant attributes available to the shell tput outputs a string, if the terminal attribute is of type string, or an integer, if the attribute is of type integer If the attribute is of type Boolean, tput sets the exit code (0 for true, if the terminal has the capacity, 1 for false if it does not) and produces no output The syntax of the command is: tput [-Ttype] attribute The -T flag indicates the type of the terminal Normally the option is unnecessary as the default is taken from the environment variable TERM The attribute is the terminal capability name from the terminfo database For example: tput clear tput bold tput smso tput rmso Clear the screen Set mode to bold Set mode to standout (normally reverse video) Return mode from standout tput cup x y Move to the spacified cursor position at line x and character position y

38 Exercises Chapter Six 1 What functions do the following perform: a) Execution trace b) Verbose trace 2 List three uses of the set command 3 What is the purpose of the -- option for the set command? 4 What will be the output response from: a) $ expr 5 / 3 b) $ expr 5 * 3 5 What will be the results of executing the function: listbin( ) { cd /bin; ls -la pg; 6 What do the following tput options mean: a) cup b) rmso c) smso 7 Write a shell script that accepts two numerical values as arguments The script should print to Standard Output the following: a) The sum of the values b) The difference of the values c) The result of the square of the first argument divided by the second argument 8 Create a series of functions, choosing suitable short names, to perform the following: a) Clear the screen b) Turn on reverse video

39 c) Turn off reverse video d) Display the exit status of the last command 9 Write a shell script that: a) Reads a line of text from Standard Input b) Sets the text as the positional parameters c) Prints the number of words in the text to Standard Output 10 Write a shell script that will use the who command to: a) Display the user's own details first b) Display a message informing the user to "Press Return to Continue" c) Display the remainder of the users' details, paginated The script should accept a single argument of -u, if required, so that the who command may be executed alone, or with this option Use the tput commands to enhance the presentation of the output Note: Use the trace utilities to check your shell scripts, if they do not perform correctly

40 Shell Program Control The shell script does not have to be a sequential series of commands: Commands may or may not be executed, depending on certain conditions Commands may be repeated a number of times, depending on other conditions 71 The && and Constructs Both the && and constructs execute the succeeding command depending on whether or not the preceding command succeeds, respectively The constructs have the following syntax: command1 && command2 command1 command2 For example: $ mv file1 /student2/file2 && echo "Move succeeded" $ sort file1 -o /tmp/sorted && mv /tmp/sorted file1 $ who -u grep "$user" echo "$user not logged on" $ mv file1 /student2/file2 && echo "Move succeeded" echo "Move failed" Whether a command succeeds or fails is measured by its exit status ($?) 72 The IF-THEN-ELSE-FI Command The if-then-else-fi command expands the && and constructs into a more readable and versatile format The syntax of the command is: if command1 then commands else commands fi The command executes one set of commands if command1 succeeds, and another set if command1 fails

41 For example: $ if mv file1 /student2/file2 > then > echo "Move succeeded" > fi is the equivalent of: $ mv file1 /student2/file2 && echo "Move succeeded" $ if mv file1 /student2/file2 > then > echo "Move succeeded" > else > echo "Move failed" > fi is the equivalent of: $ mv file1 /student2/file2 && echo "Move succeeded" echo "Move failed" 73 The ELIF Construct The elif construct combines an else with a subsidiary if test This is convenient as each if must have an associated fi However, an elif is part of an outlying if and, therefore, does not need a fi construct to close it: if command1 then commands else if command2 then commands else if command3 then commands else commands fi fi fi

42 This may be restated as: if command1 then commands elif command2 then commands elif command3 then commands else commands fi 74 The TEST Command The if-then-else-fi command tests whether or not a command succeeds It is often the case that the required test is to check the value of a variable or the type or the existence of a file The test command permits such checks, succeeding or failing, depending on whether the check is true or false, respectively The syntax of the test command is: test expression The expression depends on the item to be checked: 741 String Operators string1 = string2 string1 and string2 are the same string1!= string2 string1 and string2 are different string The string is not null -n string T he string is not null and exists -z string The string is null but exists For example: $ name1=fred $ name2=burt $ test $name1 = $name2 $ echo $? 1 $ test $name1!= $name2 $ echo $? 0

43 Consider the following example: $ test $name3 $ echo $? 1 This suggests that the variable name3 either has a null value or does not exist Now consider the following: $ test -n $name3 test: argument expected The variable name3 does not exist Because of the command, the shell sees it as: $ test -n which results in the error message If the command had been stated as: $ test -n "$name3" no error would have occurred Note: It is advisable for the expansion of all variables to be placed within a set of double quotes It will limit the number of errors occurring for the misuse of utilities such as the test command For example: $ test "$name1" = "$name2" 742 Integer Operators The expressions that may be used for integer operators are: val1 -eq val2 val1 -ne val2 val1 -lt val2 val1 -le val2 val1 -gt val2 val1 -ge val2 Expression True if: val1 equals val2 val1 does not equal val2 val1 is less than val2 val1 is less than or equal to val2 val1 is greater than val2 val1 is greater than or equal to val2

Assignment clarifications

Assignment clarifications Assignment clarifications How many errors to print? at most 1 per token. Interpretation of white space in { } treat as a valid extension, involving white space characters. Assignment FAQs have been updated.

More information

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs Summer 2010 Department of Computer Science and Engineering York University Toronto June 29, 2010 1 / 36 Table of contents 1 2 3 4 2 / 36 Our goal Our goal is to see how we can use Unix as a tool for developing

More information

5/20/2007. Touring Essential Programs

5/20/2007. Touring Essential Programs Touring Essential Programs Employing fundamental utilities. Managing input and output. Using special characters in the command-line. Managing user environment. Surveying elements of a functioning system.

More information

Bourne Shell Reference

Bourne Shell Reference > Linux Reviews > Beginners: Learn Linux > Bourne Shell Reference Bourne Shell Reference found at Br. David Carlson, O.S.B. pages, cis.stvincent.edu/carlsond/cs330/unix/bshellref - Converted to txt2tags

More information

COMP 4/6262: Programming UNIX

COMP 4/6262: Programming UNIX COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if, debug March 13, 2006 Outline shells shell programming passing arguments (KW Ch.7) exit status if (KW Ch.8) test

More information

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell. Command Interpreters A command interpreter is a program that executes other programs. Aim: allow users to execute the commands provided on a computer system. Command interpreters come in two flavours:

More information

S E C T I O N O V E R V I E W

S E C T I O N O V E R V I E W AN INTRODUCTION TO SHELLS S E C T I O N O V E R V I E W Continuing from last section, we are going to learn about the following concepts: understanding quotes and escapes; considering the importance of

More information

Useful Unix Commands Cheat Sheet

Useful Unix Commands Cheat Sheet Useful Unix Commands Cheat Sheet The Chinese University of Hong Kong SIGSC Training (Fall 2016) FILE AND DIRECTORY pwd Return path to current directory. ls List directories and files here. ls dir List

More information

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script 12 Shell Programming This chapter concentrates on shell programming. It explains the capabilities of the shell as an interpretive high-level language. It describes shell programming constructs and particulars.

More information

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc. Appendix A GLOSSARY SYS-ED/ Computer Education Techniques, Inc. $# Number of arguments passed to a script. $@ Holds the arguments; unlike $* it has the capability for separating the arguments. $* Holds

More information

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02 Essential Unix (and Linux) for the Oracle DBA Revision no.: PPT/2K403/02 Architecture of UNIX Systems 2 UNIX System Structure 3 Operating system interacts directly with Hardware Provides common services

More information

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines Introduction to UNIX Logging in Basic system architecture Getting help Intro to shell (tcsh) Basic UNIX File Maintenance Intro to emacs I/O Redirection Shell scripts Logging in most systems have graphical

More information

Unix Introduction to UNIX

Unix Introduction to UNIX Unix Introduction to UNIX Get Started Introduction The UNIX operating system Set of programs that act as a link between the computer and the user. Developed in 1969 by a group of AT&T employees Various

More information

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Unix Scripts and Job Scheduling. Overview. Running a Shell Script Unix Scripts and Job Scheduling Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring Overview Shell Scripts

More information

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT Unix as a Platform Exercises + Solutions Course Code: OS 01 UNXPLAT Working with Unix Most if not all of these will require some investigation in the man pages. That's the idea, to get them used to looking

More information

UNIX shell scripting

UNIX shell scripting UNIX shell scripting EECS 2031 Summer 2014 Przemyslaw Pawluk June 17, 2014 What we will discuss today Introduction Control Structures User Input Homework Table of Contents Introduction Control Structures

More information

Shell programming. Introduction to Operating Systems

Shell programming. Introduction to Operating Systems Shell programming Introduction to Operating Systems Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number

More information

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009 Bashed One Too Many Times Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009 What is a Shell? The shell interprets commands and executes them It provides you with an environment

More information

UNIX Shell Programming

UNIX Shell Programming $!... 5:13 $$ and $!... 5:13.profile File... 7:4 /etc/bashrc... 10:13 /etc/profile... 10:12 /etc/profile File... 7:5 ~/.bash_login... 10:15 ~/.bash_logout... 10:18 ~/.bash_profile... 10:14 ~/.bashrc...

More information

Shell Programming Overview

Shell Programming Overview Overview Shell programming is a way of taking several command line instructions that you would use in a Unix command prompt and incorporating them into one program. There are many versions of Unix. Some

More information

Bash Reference Manual

Bash Reference Manual Bash Reference Manual Reference Documentation for Bash Edition 3.1-beta1, for Bash Version 3.1-beta1. September 2005 Chet Ramey, Case Western Reserve University Brian Fox, Free Software Foundation This

More information

Shells and Shell Programming

Shells and Shell Programming Shells and Shell Programming 1 Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed

More information

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash CS 25200: Systems Programming Lecture 10: Shell Scripting in Bash Dr. Jef Turkstra 2018 Dr. Jeffrey A. Turkstra 1 Lecture 10 Getting started with Bash Data types Reading and writing Control loops Decision

More information

Processes and Shells

Processes and Shells Shell ls pico httpd CPU Kernel Disk NIC Processes Processes are tasks run by you or the OS. Processes can be: shells commands programs daemons scripts Shells Processes operate in the context of a shell.

More information

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze EECS 2031 Software Tools Prof. Mokhtar Aboelaze Footer Text 1 EECS 2031E Instructor: Mokhtar Aboelaze Room 2026 CSEB lastname@cse.yorku.ca x40607 Office hours TTH 12:00-3:00 or by appointment 1 Grading

More information

Shells and Shell Programming

Shells and Shell Programming Shells and Shell Programming Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed

More information

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

bash, part 3 Chris GauthierDickey

bash, part 3 Chris GauthierDickey bash, part 3 Chris GauthierDickey More redirection As you know, by default we have 3 standard streams: input, output, error How do we redirect more than one stream? This requires an introduction to file

More information

Vi & Shell Scripting

Vi & Shell Scripting Vi & Shell Scripting Comp-206 : Introduction to Week 3 Joseph Vybihal Computer Science McGill University Announcements Sina Meraji's office hours Trottier 3rd floor open area Tuesday 1:30 2:30 PM Thursday

More information

Linux shell programming for Raspberry Pi Users - 2

Linux shell programming for Raspberry Pi Users - 2 Linux shell programming for Raspberry Pi Users - 2 Sarwan Singh Assistant Director(S) NIELIT Chandigarh 1 SarwanSingh.com Education is the kindling of a flame, not the filling of a vessel. - Socrates SHELL

More information

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc. Appendix B WORKSHOP SYS-ED/ Computer Education Techniques, Inc. 1 Introduction There are no workshops for this chapter. The instructor will provide demonstrations and examples. SYS-ED/COMPUTER EDUCATION

More information

Bash Programming. Student Workbook

Bash Programming. Student Workbook Student Workbook Bash Programming Published by ITCourseware, LLC, 7245 South Havana Street, Suite 100, Englewood, CO 80112 Contributing Authors: Julie Johnson, Rob Roselius Editor: Jeff Howell Special

More information

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science CSCI 211 UNIX Lab Shell Programming Dr. Jiang Li Why Shell Scripting Saves a lot of typing A shell script can run many commands at once A shell script can repeatedly run commands Help avoid mistakes Once

More information

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT

Unix as a Platform Exercises. Course Code: OS-01-UNXPLAT Unix as a Platform Exercises Course Code: OS-01-UNXPLAT Working with Unix 1. Use the on-line manual page to determine the option for cat, which causes nonprintable characters to be displayed. Run the command

More information

Shell Start-up and Configuration Files

Shell Start-up and Configuration Files ULI101 Week 10 Lesson Overview Shell Start-up and Configuration Files Shell History Alias Statement Shell Variables Introduction to Shell Scripting Positional Parameters echo and read Commands if and test

More information

A shell can be used in one of two ways:

A shell can be used in one of two ways: Shell Scripting 1 A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) 2 If we have a set of commands

More information

Bourne Shell Programming Topics Covered

Bourne Shell Programming Topics Covered Bourne Shell Programming Topics Covered Shell variables Using Quotes Arithmetic On Shell Passing Arguments Testing conditions Branching if-else, if-elif, case Looping while, for, until break and continue

More information

Last Time. on the website

Last Time. on the website Last Time on the website Lecture 6 Shell Scripting What is a shell? The user interface to the operating system Functionality: Execute other programs Manage files Manage processes Full programming language

More information

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved AICT High Performance Computing Workshop With Applications to HPC Edmund Sumbar research.support@ualberta.ca Copyright 2007 University of Alberta. All rights reserved High performance computing environment

More information

UNIX System Programming Lecture 3: BASH Programming

UNIX System Programming Lecture 3: BASH Programming UNIX System Programming Outline Filesystems Redirection Shell Programming Reference BLP: Chapter 2 BFAQ: Bash FAQ BMAN: Bash man page BPRI: Bash Programming Introduction BABS: Advanced Bash Scripting Guide

More information

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options Basic Unix Command The Unix command has the following common pattern command_name options argument(s) Here we are trying to give some of the basic unix command in Unix Information Related man It is used

More information

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter. Shell Scripts A shell script contains a sequence of commands in a text file. Shell is an command language interpreter. Shell executes commands read from a file. Shell is a powerful programming available

More information

The Online Unix Manual

The Online Unix Manual ACS-294-001 Unix (Winter Term, 2018-2019) Page 14 The Online Unix Manual Unix comes with a large, built-in manual that is accessible at any time from your terminal. The Online Manual is a collection of

More information

Basic Linux (Bash) Commands

Basic Linux (Bash) Commands Basic Linux (Bash) Commands Hint: Run commands in the emacs shell (emacs -nw, then M-x shell) instead of the terminal. It eases searching for and revising commands and navigating and copying-and-pasting

More information

9.2 Linux Essentials Exam Objectives

9.2 Linux Essentials Exam Objectives 9.2 Linux Essentials Exam Objectives This chapter will cover the topics for the following Linux Essentials exam objectives: Topic 3: The Power of the Command Line (weight: 10) 3.3: Turning Commands into

More information

Title:[ Variables Comparison Operators If Else Statements ]

Title:[ Variables Comparison Operators If Else Statements ] [Color Codes] Environmental Variables: PATH What is path? PATH=$PATH:/MyFolder/YourStuff?Scripts ENV HOME PWD SHELL PS1 EDITOR Showing default text editor #!/bin/bash a=375 hello=$a #No space permitted

More information

EECS2301. Lab 1 Winter 2016

EECS2301. Lab 1 Winter 2016 EECS2301 Lab 1 Winter 2016 Lab Objectives In this lab, you will be introduced to the Linux operating system. The basic commands will be presented in this lab. By the end of you alb, you will be asked to

More information

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

CHAPTER 3 SHELL PROGRAMS: SCRIPTS CHAPTER 3 SHELL PROGRAMS: SCRIPTS Any series of commands may be stored inside a regular text file for later execution. A file that contains shell commands is called a script. Before you can run a script,

More information

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Essentials for Scientific Computing: Bash Shell Scripting Day 3 Essentials for Scientific Computing: Bash Shell Scripting Day 3 Ershaad Ahamed TUE-CMS, JNCASR May 2012 1 Introduction In the previous sessions, you have been using basic commands in the shell. The bash

More information

System Programming. Unix Shells

System Programming. Unix Shells Content : Unix shells by Dr. A. Habed School of Computer Science University of Windsor adlane@cs.uwindsor.ca http://cs.uwindsor.ca/ adlane/60-256 Content Content 1 Introduction 2 Interactive and non-interactive

More information

Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002

Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002 .tex Bash Reference Manual Reference Documentation for Bash Edition 2.5b, for Bash Version 2.05b. July 2002 Chet Ramey, Case Western Reserve University Brian Fox, Free Software Foundation Copyright c 1991-2002

More information

85321, Systems Administration Chapter 6: The shell

85321, Systems Administration Chapter 6: The shell Chapter 6 Introduction The Shell You will hear many people complain that the UNIX operating system is hard to use. They are wrong. What they actually mean to say is that the UNIX command line interface

More information

IMPORTANT: Logging Off LOGGING IN

IMPORTANT: Logging Off LOGGING IN These are a few basic Unix commands compiled from Unix web sites, and printed materials. The main purpose is to help a beginner to go around with fewer difficulties. Therefore, I will be adding to this

More information

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017 Linux Shell Scripting Linux System Administration COMP2018 Summer 2017 What is Scripting? Commands can be given to a computer by entering them into a command interpreter program, commonly called a shell

More information

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program. SHELLS: The shell is the part of the UNIX that is most visible to the user. It receives and interprets the commands entered by the user. In many respects, this makes it the most important component of

More information

Unix Handouts. Shantanu N Kulkarni

Unix Handouts. Shantanu N Kulkarni Unix Handouts Shantanu N Kulkarni Abstract These handouts are meant to be used as a study aid during my class. They are neither complete nor sincerely accurate. The idea is that the participants should

More information

LOG ON TO LINUX AND LOG OFF

LOG ON TO LINUX AND LOG OFF EXPNO:1A LOG ON TO LINUX AND LOG OFF AIM: To know how to logon to Linux and logoff. PROCEDURE: Logon: To logon to the Linux system, we have to enter the correct username and password details, when asked,

More information

Computer Systems and Architecture

Computer Systems and Architecture Computer Systems and Architecture Stephen Pauwels Computer Systems Academic Year 2018-2019 Overview of the Semester UNIX Introductie Regular Expressions Scripting Data Representation Integers, Fixed point,

More information

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable. Bourne Shell Programming Variables - creating and assigning variables Bourne shell use the set and unset to create and assign values to variables or typing the variable name, an equal sign and the value

More information

UNIX Essentials Featuring Solaris 10 Op System

UNIX Essentials Featuring Solaris 10 Op System A Active Window... 7:11 Application Development Tools... 7:7 Application Manager... 7:4 Architectures - Supported - UNIX... 1:13 Arithmetic Expansion... 9:10 B Background Processing... 3:14 Background

More information

Linux Shell Script. J. K. Mandal

Linux Shell Script. J. K. Mandal Linux Shell Script J. K. Mandal Professor, Department of Computer Science & Engineering, Faculty of Engineering, Technology & Management University of Kalyani Kalyani, Nadia, West Bengal E-mail: jkmandal@klyuniv.ac.in,

More information

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples EECS2301 Title Unix/Linux Introduction These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University Warning: These notes are not complete, it is a Skelton that will be modified/add-to

More information

Chapter 4. Unix Tutorial. Unix Shell

Chapter 4. Unix Tutorial. Unix Shell Chapter 4 Unix Tutorial Users and applications interact with hardware through an operating system (OS). Unix is a very basic operating system in that it has just the essentials. Many operating systems,

More information

Operating Systems, Unix Files and Commands SEEM

Operating Systems, Unix Files and Commands SEEM Operating Systems, Unix Files and Commands SEEM 3460 1 Major Components of Operating Systems (OS) Process management Resource management CPU Memory Device File system Bootstrapping SEEM 3460 2 Programs

More information

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12) Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12) Objective: Learn some basic aspects of the UNIX operating system and how to use it. What is UNIX? UNIX is the operating system used by most computers

More information

5/8/2012. Specifying Instructions to the Shell Chapter 8

5/8/2012. Specifying Instructions to the Shell Chapter 8 An overview of shell. Execution of commands in a shell. Shell command-line expansion. Customizing the functioning of the shell. Employing advanced user features. Specifying Instructions to the Shell Chapter

More information

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo UNIX COMMANDS AND SHELLS UNIX Programming 2015 Fall by Euiseong Seo What is a Shell? A system program that allows a user to execute Shell functions (internal commands) Other programs (external commands)

More information

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012 Cisco IOS Shell Last Updated: December 14, 2012 The Cisco IOS Shell (IOS.sh) feature provides shell scripting capability to the Cisco IOS command-lineinterface (CLI) environment. Cisco IOS.sh enhances

More information

Basics. I think that the later is better.

Basics.  I think that the later is better. Basics Before we take up shell scripting, let s review some of the basic features and syntax of the shell, specifically the major shells in the sh lineage. Command Editing If you like vi, put your shell

More information

Chapter-3. Introduction to Unix: Fundamental Commands

Chapter-3. Introduction to Unix: Fundamental Commands Chapter-3 Introduction to Unix: Fundamental Commands What You Will Learn The fundamental commands of the Unix operating system. Everything told for Unix here is applicable to the Linux operating system

More information

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala Introduction to Linux Basics Part II 1 Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala pakala@uga.edu 2 Variables in Shell HOW DOES LINUX WORK? Shell Arithmetic I/O and

More information

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename Basic UNIX Commands BASIC UNIX COMMANDS 1. cat This is used to create a file in unix. $ cat >filename This is also used for displaying contents in a file. $ cat filename 2. ls It displays the list of files

More information

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Scripting Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss What a shell

More information

The Unix Environment for Programming (COMP433)

The Unix Environment for Programming (COMP433) The Unix Environment for Programming (COMP433) Student's Practical Manual Dr. Mohamed Ben Laroussi Aissa m.issa@unizwa.edu.om Room 11 I- 13 Spring 2017 1 Textbook Topic # Topic Page 1 Introduction 2 3

More information

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A. Windshield Language Reference Manual Columbia University COMS W4115 Programming Languages and Translators Spring 2007 Prof. Stephen A. Edwards Team members Wei-Yun Ma wm2174 wm2174@columbia.edu Tony Wang

More information

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers A Big Step Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Copyright 2006 2009 Stewart Weiss What a shell really does Here is the scoop on shells. A shell is a program

More information

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p. Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p. 2 Conventions Used in This Book p. 2 Introduction to UNIX p. 5 An Overview

More information

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt ULI101 Week 03 Week Overview Absolute and relative pathnames File name expansion Shell basics Command execution in detail Recalling and editing previous commands Quoting Pathnames A pathname is a list

More information

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012 What Is a Shell? UNIX Shell Scripts CSE 2031 Fall 2012 A program that interprets your requests to run other programs Most common Unix shells: Bourne shell (sh) C shell (csh - tcsh) Korn shell (ksh) Bourne-again

More information

Computer Systems and Architecture

Computer Systems and Architecture Computer Systems and Architecture Introduction to UNIX Stephen Pauwels University of Antwerp October 2, 2015 Outline What is Unix? Getting started Streams Exercises UNIX Operating system Servers, desktops,

More information

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University Introduction to Bash Programming Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University 1 Outline Shell command line syntax Shell builtin commands Shell variables, arguments

More information

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions? Lecture 4 Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions? Tuesday, September 7 CS 375 UNIX System Programming - Lecture

More information

Introduction to Unix: Fundamental Commands

Introduction to Unix: Fundamental Commands Introduction to Unix: Fundamental Commands Ricky Patterson UVA Library Based on slides from Turgut Yilmaz Istanbul Teknik University 1 What We Will Learn The fundamental commands of the Unix operating

More information

RH033 Red Hat Linux Essentials

RH033 Red Hat Linux Essentials RH033 Red Hat Linux Essentials Version 3.5 QUESTION NO: 1 You work as a Network Administrator for McNeil Inc. The company has a Linux-based network. A printer is configured on the network. You want to

More information

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP Unix Guide Meher Krishna Patel Created on : Octorber, 2017 Last updated : December, 2017 More documents are freely available at PythonDSP Table of contents Table of contents i 1 Unix commands 1 1.1 Unix

More information

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems. Copyleft 2005, Binnur Kurt 3 Operating Systems Copyleft 2005, Binnur Kurt Content The concept of an operating system. The internal architecture of an operating system. The architecture of the Linux operating system in more detail.

More information

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing Content 3 Operating Systems The concept of an operating system. The internal architecture of an operating system. The architecture of the Linux operating system in more detail. How to log into (and out

More information

COMS 6100 Class Notes 3

COMS 6100 Class Notes 3 COMS 6100 Class Notes 3 Daniel Solus September 1, 2016 1 General Remarks The class was split into two main sections. We finished our introduction to Linux commands by reviewing Linux commands I and II

More information

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one] Data and Computer Security (CMPD414) Lab II Topics: secure login, moving into HOME-directory, navigation on Unix, basic commands for vi, Message Digest This lab exercise is to be submitted at the end of

More information

h/w m/c Kernel shell Application s/w user

h/w m/c Kernel shell Application s/w user Structure of Unix h/w m/c Kernel shell Application s/w. user While working with unix, several layers of interaction occur b/w the computer h/w & the user. 1. Kernel : It is the first layer which runs on

More information

5/8/2012. Creating and Changing Directories Chapter 7

5/8/2012. Creating and Changing Directories Chapter 7 Creating and Changing Directories Chapter 7 Types of files File systems concepts Using directories to create order. Managing files in directories. Using pathnames to manage files in directories. Managing

More information

CS246 Spring14 Programming Paradigm Notes on Linux

CS246 Spring14 Programming Paradigm Notes on Linux 1 Unix History 1965: Researchers from Bell Labs and other organizations begin work on Multics, a state-of-the-art interactive, multi-user operating system. 1969: Bell Labs researchers, losing hope for

More information

Unix System Architecture, File System, and Shell Commands

Unix System Architecture, File System, and Shell Commands Unix System Architecture, File System, and Shell Commands Prof. (Dr.) K.R. Chowdhary, Director COE Email: kr.chowdhary@iitj.ac.in webpage: http://www.krchowdhary.com JIET College of Engineering August

More information

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2) BANK ON & SHELL PROGRAMMING-502 (CORE PAPER-2) TOPIC 1: VI-EDITOR MARKS YEAR 1. Explain set command of vi editor 2 2011oct 2. Explain the modes of vi editor. 7 2013mar/ 2013 oct 3. Explain vi editor 5

More information

Common File System Commands

Common File System Commands Common File System Commands ls! List names of all files in current directory ls filenames! List only the named files ls -t! List in time order, most recent first ls -l! Long listing, more information.

More information

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell: Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed performs the actions Example:

More information

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1 Shell Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 If we have a set of commands that we want to run on a regular basis, we could write a script A script acts as a Linux command,

More information

Linux shell scripting Getting started *

Linux shell scripting Getting started * Linux shell scripting Getting started * David Morgan *based on chapter by the same name in Classic Shell Scripting by Robbins and Beebe What s s a script? text file containing commands executed as a unit

More information

100 SHELL PROGRAMS IN UNIX

100 SHELL PROGRAMS IN UNIX 100 SHELL PROGRAMS IN UNIX By Sarika Jain Head, MCA Department ABSS, Meerut (U.P.) Shivani Jain Senior Lecturer, Department of Computer Science VCE, Meerut (U.P.) FIREWAL MEDIA (An Imprint of Laxmi Publications

More information

Tcl/Tk for XSPECT a Michael Flynn

Tcl/Tk for XSPECT a Michael Flynn Tcl/Tk for XSPECT a Michael Flynn Tcl: Tcl (i.e. Tool Command Language) is an open source scripting language similar to other modern script languages such as Perl or Python. It is substantially more powerful

More information