1 / 53

C151 Multi-User Operating Systems

C151 Multi-User Operating Systems. Scripts and Shell Programming. Shell Script. ------------ ------------ ------------ ------------ ------------. Shell interpreter. A file containing list of commands Usually it has the extension .sh .

andradeb
Download Presentation

C151 Multi-User Operating Systems

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. C151 Multi-User Operating Systems Scripts and Shell Programming

  2. Shell Script ------------ ------------ ------------ ------------ ------------ Shell interpreter A file containing list of commands Usually it has the extension .sh. Each line (command) is interpreted and executed by a shell program (interpreter). We will discuss bash shell. Shell script

  3. Steps to Run a Shell Script • Create a file (say file_name.sh)using your favorite editor • Make the file executable chmod 700 file_name.sh • Run the script file_name.sh

  4. First Line • The first line should look like this #!/bin/bash • This line tells: • Which shell (interpreter) should be used to execute the script: bash • Where to find the shell (interpreter): /bin/bash

  5. Comments • Comments: any line starting with a # will be considered as comments • Exception: The very first one • Example: #!/bin/bash #This is my first shell script pwd ls –l

  6. Combining Commands • Several commands on the same line: separated with ; • Example:cd.. ; make ; cdsrc

  7. Output Use echo command #!/bin/bash #This is my first shell script echo “This is my current working directory” pwd echo “These are the files in my current working directory” ls –l

  8. See This Script #!/bin/bash echo $0 echo $1 echo $2 echo $# echo $* test.sh

  9. See This Script #!/bin/bash echo $0 echo $1 echo $2 echo $# echo $* test.sh

  10. See this script #!/bin/bash echo $0 echo $1 echo $2 echo $# echo $* test.sh

  11. Command Line Parameters • The command parameters are arguments that can be given in the command line when we run the script. • $0is the name of the script file. • $1is the first argument in the command line, $2is the second, etc. • $# is the number of arguments. • $* contains all the arguments.

  12. Variables • The name of a variable is a placeholder for its value, the data it holds. • If v is the name of a variable, then $v is a reference to its value • What result will be displayed for the following script? #!/bin/bash v=23 echo v echo $v

  13. Variables • The name of a variable is a placeholder for its value, the data it holds. • If v is the name of a variable, then $v is a reference to its value • What result will be displayed for the following script? #!/bin/bash v=23 echo v echo $v v 23

  14. Assignment operator = • Rule: • No space permitted on either side of = sign.

  15. Variable Rule • Assign a value to name of a variable • Read variable value through reference of the variable • Example: • Name of the variable: num • Value of the variable $num num 23

  16. Task 1 • Create two variables: a, b • Assign a with value 100 • Assign b with value 200 • Disply the values of a and b • Reassign b with the value of a • Display the values of a and b

  17. Script for Task 1 #!/bin/bash a=100 b=200 echo $a echo $b b=$a echo $a echo $b

  18. Script for Task 1 #!/bin/bash a=100 b=200 echo $a echo $b b=$a echo $a echo $b 100 200 100 100

  19. Number Operations • All computations in a shell script are done with integers. • A number operation must be placed within the following syntax: $[expression] • For example a=$[2+3] echo $a (5 will be displayed)

  20. Number Operations v1=100 echo $v1 (100) v2=$v1/2 echo $v2 (?)

  21. Number Operation v1=100 echo $v1 (100) v2=$v1/2 echo $v2 (100/2)

  22. Number Operation v1=100 echo $v1 (100) v2=$[$v1/2] echo $v2 (50)

  23. Arithmetic Operators • + plus • - minus • * multiplication • / division • ** exponentiation • % modulo

  24. Task 2 • Create two variables a and b, and assign each with a value. • Calculate value a plus value b and assign it to variable c • Calculate value a minus value b and assign it to variable d • Calculate value c multiply value d and assign it to variable e • Calculate value e divided by 2 and assign it to variable f • Display value of f

  25. Script for Task 2 #!/bin/bash a=5 b=3 c=$[$a+$b] d=$[$a-$b] e=$[$c*$d] f=$[$e/2] echo $f

  26. Script for Task 2 #!/bin/bash a=5 b=3 c=$[$a+$b] d=$[$a-$b] e=$[$c*$d] f=$[$e/2] echo $f 8

  27. Input • Use readcommand read variable_name

  28. Input Example #!/bin/bash echo “Enter your name:” read name echo “Enter your age:” read age echo “The age of $name is $age”

  29. Input Example #!/bin/bash echo “Enter your name:” read name echo “Enter your age:” read age echo “The age of $name is $age”

  30. Integer Comparison • Test if two integer variables are equal or not if [ $a -eq $b ] • -Note: must have a space before and after brackets and operators

  31. Integer Comparison • -eq is equal to • -ne is not equal to • -gt is greater than • -ge is greater than or equal to • -lt is less than • -le is less than or equal to

  32. If Statement if [ condition ] then command(s) • elif[ condition ] • then • command(s) • else command(s) fi if [ condition ] then command(s) else command(s) fi You can have more elif [] then statements

  33. Task 3 • Ask user to enter two integer numbers and display which number is greater

  34. Script for Task 3 #!/bin/bash echo “Enter number 1:” read a echo “Enter number 2:” read b if [ $a -ge $b ] then echo “$a is greater than or equal to $b” else echo “$a is less than $b” fi

  35. String Comparison • == is equal to • if [ “$a” == “$b” ] • != is not equal to • if [ “$a” != “$b” ] • Note: • Must have a space before and after [ and ]. • Must have spaces before and after operators == and != • Must use double quote for string variables • You can also use double quote for number variables. But, it is not required.

  36. Task 4 • Ask user to enter two strings and display whether they are equal or not

  37. Script for Task 4 #!/bin/bash echo “Enter string 1:” read a echo “Enter string 2:” read b if [ “$a” == “$b” ] then echo "$a is same as $b" else echo "$a is different from $b" fi

  38. Logical Operators • &&AND • || OR • Example: #!/bin/bash a=100 b=200 if ([ $a -gt 0 ] && [ $b -gt 0]) then echo "both $a and $b are positive" fi

  39. While Loop while [ condition ]do command(s)... done • Note: keep working in loop as long as condition is true (terminate if condition is false)

  40. Task 5 • Ask the user to continually enter an integer number and show the summation of all the numbers the user has entered so far. • The program terminates when user enters word “end” • No input error checking is required

  41. Scripts for Task 5 #!/bin/bash sum=0 echo "Enter a number(end to exit)" read n while [ "$n" != "end" ] do sum=$[$sum+$n] echo “The sum of the numbers you entered so far is $sum" echo "Enter a number(end to exit)" read n done

  42. Until Loop until [ condition ]do command(s)... done • Keep in loop as long as condition is false (terminate when condition is true) • Condition is opposite to while loop

  43. Scripts for Task 5 (using until loop) #!/bin/bash sum=0 echo "Enter a number(end to exit)" read n until [ "$n" == "end" ] do sum=$[$sum+$n] echo “The sum of the numbers you entered so far is $sum" echo "Enter a number(end to exit)" read n done

  44. A Note about Conditions in Brackets • Always keep spaces between the brackets and the actual check/comparison • Always keep spaces between comparison operators and variable (data) if [$a -lt 0 ] while [ $a –gt $b ] until [ “$a” == “$b” ]

  45. For Loop • for loop is used to extract items from a list forarg in $list_variabledo command(s)... done

  46. For Loop Example #!/bin/bash fruits=“apples oranges pears bananas” for fruit in $fruits #fruits is considered as a list (items are separated by spaces) do echo $fruit # Each fruit on a separate line. done • Result • apples • oranges • pears • bananas

  47. For Loop Example #!/bin/bash fruits=“apples oranges pears bananas” for fruit in “$fruits” #fruits is considered as a single item do echo $fruit done Result: apples oranges pears bananas

  48. When to use double quote on string variable? • Should use double quote: • Single value string (no need to separate words) • Should not use double quote: • String is a list of word and we need to separate words from the list

  49. Command Substitution • Assigns the output of a command to a variable variable_name=$(command) • Example #!/bin/bash cw=$(pwd) echo $cw

  50. Redirect Output to Files • We can redirect the output of Shell Script from Screen to a File echo …….. >> file_name

More Related