1 / 42

Shell Programming

Shell Programming. Introducing UNIX Shells. Shell is also a programming language and provides various features like variables, branching, looping and so on. The shell is a UNIX program that interprets the commands you enter from the keyboard

enan
Download Presentation

Shell Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Programming

  2. Introducing UNIX Shells • Shell is also a programming language and provides various features like variables, branching, looping and so on. • The shell is a UNIX program that interprets the commands you enter from the keyboard • UNIX provides several shells, including the Bourne shell, the Korn shell, and the C shell • Steve Bourne at AT&T Bell Laboratories developed the Bourne shell as the first UNIX command processor

  3. Introducing UNIX Shells The Korn shell includes many extensions, such as a history feature that lets you use a keyboard shortcut to retrieve commands you previously entered The C shell is designed for C programmer’s use Linux uses the freeware Bash shell as its default command interpreter

  4. Shell Scripts Definition : “An executable file consisting of a set of commands to be executed is called a shell script or shell program”. A “.sh” extension is used for shell scripts. It is not mandatory to use such extension for shell script, but it is used to differentiate them from other files.

  5. Shell Scripts Shell scripts run in interpretive mode. This means that shell scripts are executed one line at a time. They are not compile to separate executable file as “C” programs.

  6. When to use Shell Scripts ? Shell scripts should be used on following occasions : While customizing a work environment. For example, to display current date and time on log-on. Automating daily tasks. For example, to take backup on regular intervals. Automating repetitive tasks. For example, compiling and executing programs. Executing important system procedures. For example, shutdown, formatting a disk.

  7. When not to use Shell Scripts ? Shell scripts should not be used on following occasions : When the task to be performed is too complex. When the task requires high degree of efficiency. When the task requires variety of software tools.

  8. Creating & Executing Shell Scripts To create and execute the shell script, following the steps given below : Open any editor like as vi, pico, emacs or any other one. Write script instruction in a file and save the file with “.sh’ extension example, suppose the file is given name “example.sh.” By default, any newly created file does not contain execute permissions.

  9. Creating & Executing Shell Scripts So allocate execution permission to the owner of a file. For this purpose chmod command can be use as – $ chmod u+x example.sh Invoking Scripts with sh :To use the sh command. $ sh example.sh Making Files Executable : To make the script file an executable file.

  10. sh Command The sh command invokes a new copy of the shell. You can run your script files using sh command.

  11. sh Command Options of sh Command -n : Read commands, but does not execute them. -v : Prints the input to the shell as the shell reads it. -x : Prints command lines and their arguments as they are executed. This option is used mostly for debugging.

  12. Accepting Inputs There are two methods available to accept input from users. These methods are as follows: Interactive Method Non-Interactive Method

  13. Interactive Method It uses read command to read input values in variables. The syntax of read command is: Read variable_name When script encounters this command, it pauses at that point to take input from the keyboard. Any value entered by the user is stored in the variable specified with the read command.

  14. Non-Interactive Method It is uses command line arguments. User can provide arguments as the command prompt while executing the shell script. Such arguments can be provided along with the script name. For example, following copyfile program takes two argument as file names $ copyfile source.txt dest.txt

  15. Non-Interactive Method When arguments are specified with a shell script, they are assigned to certain special variable called positional parameters. The first argument is read by the shell into the parameter $1 and the second in $2 and so on. In our example, $1 contains source.txt while $2 contains dest.txt.

  16. Non-Interactive Method Along with these positional parameters, there are a few other special parameters used by the shell. They are given as below; $0 : holds the command name (or program name) itself. $* : holds the complete set of positional parameters as a single string. In our example, it contains ‘source.txt, dest.txt’

  17. Non-Interactive Method • $# : holds the number of arguments specified in command line. In our example, it holds 2. • $? : holds the exit status of last command. (if exit status is 0, successful, else failure.) • $$ : holds the process ID(PID) of the current shell. • $! : holds the PID of the last background process.

  18. Evaluate Expressions using ‘test’ & […] The test & […] commands are used to evaluate conditional expressions with integers, strings and file attributes. The syntax of test commands is: test expression The syntax of […] commands is: [expression] Here, expression is the conditions to be evaluated. This expression must be enclosed by white spaces before and after it.

  19. Evaluate Expressions using ‘test’ & […] White space must also separate the expression argument the expression arguments and operators. It the expression evaluates to true, then a zero exit status is returned, otherwise the expression evaluates to false and a non-zero exit status is returned. The various operators that can be used with both of these commands.

  20. Integer Related Operator

  21. File Related Operator

  22. File Related Operator

  23. String Related Operator

  24. String Related test Operator

  25. Other Operator

  26. Branching The shell support various programming constructs to provide branching functionality. This include simple if, if..else, else-if ladder and case statement.

  27. Simple if Syntax : if command1 then commands fi Usage : execute commands if command1 remains successful. Example : if cp source.txt dest.txt then echo “file copied successfully….” fi

  28. if…else Syntax : if command1 then command2 else command3 fi Usage : execute command2 if command1 remains successful, otherwise execute command3.

  29. if…else Example : if cp source.txt dest.txt then echo “file copied successfully….” else echo “some problem in copy operation” fi

  30. else…if ladder Syntax : if command1 then command elif command2 then command elif command-n then commands else commands fi

  31. else…if ladder Usage : whenever any command remains successful either with if or with elif, execute the commands associated with that block. When all the ‘n’ commands remained unsuccessful, then final commands associated with else will be executed.

  32. Case Syntax : case expression in pattern1) command1;; pattern2) command2;; pattern3) command3;; … patternn) commandn;; esac

  33. Case • Usage : execute commands associated with the pattern that matches expression. • Multiple patterns can be given but must be separated with a | character. • Each command list is terminated with a pair of semicolons, and the entire construct is closed with esac (reverse of case)

  34. Case Example : echo “Do u like shell-programming? (y/n)” read answer case $answer in y|Y) echo “wow…! It’s good!”;; n|N) echo “U should like it. It’s important for exams”;; esac

  35. expr Command • Syntax : expr expression • Usage : evaluates the given mathematical expression. • Options : expression can be of the form : expr1 operator expr2 • Here, operator can be +(plus), -(minus), \*(multiplication), /(division) or %(modulo). • Also, operator must be enclosed by white space on either side of it. • Examples : - expr 5 + 3 - expr $x \* $y

  36. Looping • The shell supports various programming constructs to provide looping functionality. • This includes : • while • until • for

  37. While • Syntax : while command1 do commands done • Usage : execute commands while command1 remains successful.

  38. While • Example : echo “enter no:” read no i=1 while [ $i –le $no ] do echo “$i” i=`expr $i + 1` #not ‘ ‘ but ` ` done

  39. until • Syntax : until command1 do commands done • Usage : execute commands while command1 remains UN-successful.

  40. until • Example : echo “enter no:” read no i=1 until [ $i –gt $no ] do echo “$i’ i=`expr $i + 1` done

  41. for • Syntax : for variable in list of argument do commands done • Usage : here, list contains white space separated words, such as- test1 test2 test3 test4 • Commands are executed until list is exhausted. • Each time single word is assigned to variable during loop execution.

  42. for • Example : for i in 1 2 3 4 5 do echo “$i’ done

More Related