960 likes | 1.2k Views
Unix. Programming with the Shell. Shell Types in Unix. Bourne Shell. Bourne Again Shell (bash). C Shell (c-shell). Korn Shell (k-shell). TC Shell (tcsh). The Shell. A program that interprets user’s requests to run programs. Provides environment for user programs.
E N D
Unix Programming with the Shell.
Shell Types in Unix • Bourne Shell. • Bourne Again Shell (bash). • C Shell (c-shell). • Korn Shell (k-shell). • TC Shell (tcsh)
The Shell. • A program that interprets user’s requests to run programs. • Provides environment for user programs. • A command line interpreter that • Reads user inputs. • Executes commands.
Shell Features. • Interactive and background processing. • Input/output redirection • Pipes. • Wild card matching. • Programmable. • Shell Variables. • Programming language constructs. • Shell Scripts.
Korn Shell Features Features appropriated from the C shell include Job control • Aliases • Functions • Command history The Korn shell's major new features include • Command-line editing,
Korn Shell Features • Integrated programming features • Debugging primitives • Regular expressions • Advanced I/O features • New options and variables • Increased speed • Security features
What is a shell ? Figure 1.1: The shell is a layer around the UNIX operating system
What is a Shell Script • A shell script is a text file containing a combination of following: • Unix commands (such as ls, cat). • Shell commands (such as variable assignment) and programming constructs (such as if statement and loops) • Since a shell script is a program, it is normally assigned execute permission using the chmod command.
What is a Shell Script • To write effective shell scripts, you need to have working knowledge of basic commands and utilities in Unix. These include: • regular expressions • grep, cut, awk and sed utilities • This course does not permit time to cover the above utilities. Where relevant, we might demonstrate the use of these utilities.
Executing a shell script • There are many ways of executing a shell script: • By passing the shell script name as an argument to the shell. For example: sh script1.sh • If the shell script is assigned execute permission, it can be executed using it’s name. For example: ./script1.sh
Executing a shell script • There are many ways of executing a shell script: • If the shell script has execute permission and is stored in a directory listed in PATH, it can be executed using it’s name. For example: script1.sh -- If the shell script has execute permission and is stored in a directory listed in PATH, it can be executed using the dot command. For example: . script1.sh
Filenames and Wildcards • Wildcard Matches ? Any single character * Any string of characters [set] Any character in set [!set] Any character not in set A set is a list of characters Expression Matches [abc] a, b, or c [ .,;] Period, comma, or semicolon [-_] Dash and underscore [a-c] a, b, or c [a-z] All lowercase letters [!0-9] All non-digits
Filenames and Wildcards • Expression Matches • [a-zA-Z] All lower- and uppercase letters • [a-zA-Z0-9_-] All letters, all digits, underscore, and dash
Special Characters or Metacharacters & Quoting • Table 1.6: Special Characters Character Meaning ~ Home directory # Comment $ Variable expression & Background job * String wildcard ( Start subshell ) End subshell \ Quote next character | Pipe
Special Characters or Metacharacters & Quoting Character Meaning [ Start character-set wildcard ] End character-set wildcard { Start code block } End code block ; Shell command separator ‘ Strong quote “ Weak quote < Input redirect > Output redirect / Pathname directory separator ? Single-character wildcard
Command grouping ( ) • Executes the command group in a sub-shell • Ex:- # pwd /root # (cd xyz ; pwd ) /root/xyz # pwd /root A change of the directory in the child can't reflect in the parent process.
Command grouping { } • It uses current shell only • Ex:- # pwd /root # { cd xyz ; pwd ; } / root/xyz # pwd /root/xyz Directory change is permanent
Here-documents • The <<label redirector essentially forces the input to a command to be the shell's standard input, which is read until there is a line that contains only label The input in between is called a here-document. • Ex:- • 1) cat >> xyz << EOF IBM UNIX TRAINING EOF
Here-documents Ex:- 2) tr '[a-z]' '[A- Z]' << EOF hi hello EOF 3) cat > file1 << EOF djhdksjhfksfkdsf EOF # cat file1 It is used to take away user interaction & provide automation.
Tees joints • Tee uses standard input and standard output • It can be placed anywhere in a pipeline • It breaks up the input inito two components, one component is saved in a file and the other is connected to the standard output • It is used to store the intermediate output of a pipeline • Ex:- ls -l | tee abc who | tee list | wc -l
Command-line Editing Enabling Command-line Editing $ set -o emacs or $ set -o vi
Simple Control Mode Commands Basic vi Control Mode Commands • ESC to Enter control mode Command Description • h Move left one character • l Move right one character • w Move right one word • b Move left one word • e Move to end of current word • 0 Move to beginning of line • ^ Move to first non-blank character in line • $ Move to end of line
Entering and Changing Text Commands for Entering vi Input Mode Command Description • i Text inserted before current character (insert) • a Text inserted after current character (append) • I Text inserted at beginning of line • A Text inserted at end of line • R Text overwrites existing text
Deletion Commands • Command Description • dh Delete one character backwards • dl Delete one character forwards • db Delete one word backwards • dw Delete one word forwards • dB Delete one non-blank word backwards • dW Delete one non-blank word forwards • d$ Delete to end of line • d0 Delete to beginning of line
Moving Around in the History File • Command Description • k or - Move backward one line • j or + Move forward one line • G Move to line given by repeat count • ?string Search backward for string • /string Search forward for string • n Repeat search in same direction as previous • N Repeat search in opposite direction of previous
Shell Variables. • Positional Parameters. • Special Parameters. • Named variables
Positional Parameters. • Acquire values from the position of arguments in command line. • $1, $2, $3,..$9 • sh file1 10 20 30 $1 $2 $3
Special Parameters. • Shell assigns the value for this parameter. • $# Number of Command Line Arguments. • $0 Command Name. • $* Displays all the command line arguments. • $? Exit Status. • $! Process number of the last background command • $@ Same as $*, except when enclosed in double quotes. • $$ PID number.
Named Variables. • User-defined variable that can be assigned a value. • Used extensively in shell-scripts. • Used for reading data, storing and displaying it.
Accepting Data. • read. • Accepts input from the user. • Syntax : read variable_name. • Example : read sname Variable Name
Display Data. • echo • Used to display a message or any data as required by the user. • echo [Message, Variable] • Example: echo “IBM.” echo $sname Variable Name
test command. • Used extensively for evaluating shell script conditions. • It evaluates the condition on its right and returns a true or false exit status. • The return value is used by the construct for further execution. • In place of writing test explicitly, the user could also use [ ].
test command (Contd). • Operators used with test for evaluating numeral data are: -eq Equal To -lt Less than -gt Greater than -ge Greater than or equal to -le Less than or equal to -ne not equal to
test command (Contd). • Operators used with test for evaluating string data are: str1 = str2 True if both equals str1 != str2 True if not equals -n str1 True if str1 is not a null string -z str1 True if str1 is a null string
test command (Contd). • Operators used with test for evaluating file data are: -f file1 True if file1 exists and is a regular file. -d file1 True if file1 exists and is directory. -s file1 True if file1 exists and has size greater than 0 -r file1 True if file1 exists and is readable. -w file1 True if file1 exists and is writable. -x file1 True if file1 exists and is executable.
Logical Operators. • Logical Operators used with test are: ! Negates the expression. -a Binary ‘and’ operator. -o Binary ‘or’ operator.
expr command. • Used for evaluating shell expressions. • Used for arithmetic and string operations. • Example : expr 7 + 3 would give an output 10. • When used with variables, back quotes need to be used. Operator has to be preceded and followed by a space.
Conditional Execution. • && • The second command is executed only when first is successful. • command1 && command2 • || • The second command is executed only when the first is unsuccessful. • command1 || command2
Program Constructs • if • for • while • until • case
if control command then <commands> else <commands> fi if statement. • Syntax.
for variable-name in value1 value2 .... do <commands> done for statement. • Syntax.
while control command do <commands> done while statement. • Syntax.
until control command do <commands> done until statement. • Syntax.
case value in choice1) commands ;; choice2) commands ;; .... .... esac case statement. • Syntax. The symbols ;; are used as option terminators.
Useful Shell Scripting commands. • break • To come out of a loop. • continue • To jump to the start of loop. • exit • To prematurely terminate a program. • # • To interpret the rest of line as comments.
export command. • export • To make a variable a part of environment and also be accessible to the child shell. export variable_name
Customizing User Environment >To customize the environment various built-in shell variables are available. >To change the values of variables permanently , define it in .profile file. The .profile File • the Korn shell reads and runs this file whenever you log in to your system • Various environment variables can be defined in this file • Alias can be defined in .profile file
Aliases • Alias is a synonym for a command or command string • Syntax: alias new=original Ex:- alias search=grep alias cdnew=‘cd /xyz/x1/x2’ >Quotes are necessary if the string being aliased consists of more than one word >it is possible to alias an alias, aliases are recursive Ex:- alias c=cdnew
Aliases • type alias without any arguments, to get a list of all the aliases you have defined as well as several that are built-in. • The command unaliasname removes any alias definition for its argument
set command. • set command • Used for display all the environment variables. • Shows the current values of system variables. • Also allows conversion of arguments into positional parameters. • Syntax : set