Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Unix PowerPoint Presentation

Unix

212 Views Download Presentation
Download Presentation

Unix

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Unix Programming with the Shell.

  2. Shell Types in Unix • Bourne Shell. • Bourne Again Shell (bash). • C Shell (c-shell). • Korn Shell (k-shell). • TC Shell (tcsh)

  3. 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.

  4. Shell Features. • Interactive and background processing. • Input/output redirection • Pipes. • Wild card matching. • Programmable. • Shell Variables. • Programming language constructs. • Shell Scripts.

  5. 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,

  6. Korn Shell Features • Integrated programming features • Debugging primitives • Regular expressions • Advanced I/O features • New options and variables • Increased speed • Security features

  7. What is a shell ? Figure 1.1: The shell is a layer around the UNIX operating system

  8. 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.

  9. 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.

  10. 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

  11. 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

  12. 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

  13. Filenames and Wildcards • Expression Matches • [a-zA-Z] All lower- and uppercase letters • [a-zA-Z0-9_-] All letters, all digits, underscore, and dash

  14. 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

  15. 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

  16. 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.

  17. Command grouping { } • It uses current shell only • Ex:- # pwd /root # { cd xyz ; pwd ; } / root/xyz # pwd /root/xyz Directory change is permanent

  18. 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

  19. 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.

  20. 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

  21. Command-line Editing Enabling Command-line Editing $ set -o emacs or $ set -o vi

  22. 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

  23. 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

  24. 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

  25. 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

  26. Shell Variables. • Positional Parameters. • Special Parameters. • Named variables

  27. Positional Parameters. • Acquire values from the position of arguments in command line. • $1, $2, $3,..$9 • sh file1 10 20 30 $1 $2 $3

  28. 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.

  29. Named Variables. • User-defined variable that can be assigned a value. • Used extensively in shell-scripts. • Used for reading data, storing and displaying it.

  30. Accepting Data. • read. • Accepts input from the user. • Syntax : read variable_name. • Example : read sname Variable Name

  31. 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

  32. 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 [ ].

  33. 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

  34. 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

  35. 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.

  36. Logical Operators. • Logical Operators used with test are: !  Negates the expression. -a  Binary ‘and’ operator. -o  Binary ‘or’ operator.

  37. 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.

  38. 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

  39. Program Constructs • if • for • while • until • case

  40. if control command then <commands> else <commands> fi if statement. • Syntax.

  41. for variable-name in value1 value2 .... do <commands> done for statement. • Syntax.

  42. while control command do <commands> done while statement. • Syntax.

  43. until control command do <commands> done until statement. • Syntax.

  44. case value in choice1) commands ;; choice2) commands ;; .... .... esac case statement. • Syntax. The symbols ;; are used as option terminators.

  45. 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.

  46. export command. • export • To make a variable a part of environment and also be accessible to the child shell. export variable_name

  47. 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

  48. 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

  49. 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

  50. 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