Shell Scripts

1,550 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,550
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Shell Scripts

  1. 1. Unix Shell Scripts
  2. 2. What are scripts ? <ul><li>Text files in certain format that are run by another program </li></ul><ul><li>Examples: </li></ul><ul><ul><li>Perl </li></ul></ul><ul><ul><li>Javascript </li></ul></ul><ul><ul><li>Shell scripts (we learn c-shell scripts) </li></ul></ul>
  3. 3. How they differ from C programs ? <ul><li>C programs are compiled into machine code (executables) </li></ul><ul><li>Executables are not portable , but they run faster (no translation during runtime) </li></ul>
  4. 4. What to use ? <ul><li>Use scripts for lightweight operations : </li></ul><ul><ul><li>Simple manipulations on files </li></ul></ul><ul><ul><li>Sending mails </li></ul></ul><ul><ul><li>Network communication </li></ul></ul><ul><ul><li>Rapid development </li></ul></ul><ul><li>Use c programs for: </li></ul><ul><ul><li>CPU bound computations </li></ul></ul><ul><ul><li>Complex data structures </li></ul></ul><ul><ul><li>Critical time applications </li></ul></ul><ul><ul><li>Embedded applications </li></ul></ul>
  5. 5. Some useful c-shell commands <ul><li>head / tail – returns first / last lines of a file </li></ul><ul><li>echo – print command </li></ul><ul><li>sort – used to sort files in lexicographic order </li></ul><ul><li>cat – concatenate files and print </li></ul><ul><li>grep – find regular expressions in files </li></ul><ul><li>find – find files by criteria </li></ul><ul><li>wc – word count on files </li></ul><ul><li>diff - find differences between files </li></ul><ul><li>basename / dirname – extract file / directory name from full path </li></ul><ul><li>touch – change file timestamp </li></ul><ul><li>mail – sending mail </li></ul><ul><li>whereis – locate program </li></ul>
  6. 6. Redirection and pipes <ul><li>prog redirection file </li></ul><ul><li>> : redirect stdout </li></ul><ul><li>>> : append stdout </li></ul><ul><li>>& : redirect stdout and stderr </li></ul><ul><li>>>& : append stdout and stderr </li></ul><ul><li>< : redirect stdin </li></ul><ul><ul><ul><li>prog1 pipe prog2 </li></ul></ul></ul><ul><li>| : redirect stdout of prog1 to stdin of prog2 </li></ul><ul><li>|& : same, with stdout and stderr </li></ul>
  7. 7. How to write a c-shell script <ul><li>Edit the code file (no mandatory extension) </li></ul><ul><li>Make sure the first line in the file is: </li></ul><ul><li>#!/bin/csh –f (for c-shell scripts) </li></ul><ul><li>#!/bin/awk –f (for awk scripts) </li></ul><ul><li>Add executable permission : </li></ul><ul><li>chmod +x filename </li></ul>
  8. 8. <ul><li>#!/bin/csh –f </li></ul><ul><li>################ </li></ul><ul><li>#simple_example # </li></ul><ul><li>################ </li></ul><ul><li># We can define variables and print their values </li></ul><ul><li>set course = soft1 </li></ul><ul><li>echo $course </li></ul><ul><li># We can do the same for arrays </li></ul><ul><li>set names = ( Danny Dina Eyal Ayelet Ori Neta ) </li></ul><ul><li>echo $names </li></ul><ul><li>echo $#names # size of array </li></ul><ul><li>echo $names[2] # the second element </li></ul><ul><li>echo $names[2-] # starting from the second element </li></ul><ul><li>echo $names[-2] # until the second element </li></ul><ul><li>echo $names[2-3] # elements 2,3 </li></ul>
  9. 9. <ul><li># '@' at the beginning of the line - treat as an arithmetic expression and not </li></ul><ul><li># as a string </li></ul><ul><li>@ num = 17 </li></ul><ul><li>echo $num </li></ul><ul><li>@ num -= 3 </li></ul><ul><li>echo $num </li></ul><ul><li>@ num *= 14 </li></ul><ul><li>echo $num </li></ul><ul><li># if we want to assign the value of a command to a variable </li></ul><ul><li>set chars = `wc -l ./simple_example` </li></ul><ul><li>echo $chars </li></ul><ul><li># accessing program parameters </li></ul><ul><li>echo The program name is : $0, the first parameter is $1 and the second is $2 </li></ul><ul><li>echo The number of parameters (not including program name) is $#argv </li></ul>
  10. 10. Loops <ul><li>foreach identifier (set) </li></ul><ul><li>. . . </li></ul><ul><li>end </li></ul><ul><li>while ( condition) </li></ul><ul><li>. . . </li></ul><ul><li>end </li></ul>
  11. 11. Conditional statements <ul><li>if condition then </li></ul><ul><li>. . . </li></ul><ul><li>endif </li></ul><ul><li>switch ( value) </li></ul><ul><li>case value1 : </li></ul><ul><li>breaksw </li></ul><ul><li>. . . </li></ul><ul><li>default: </li></ul><ul><li>endsw </li></ul>
  12. 12. Testing files attributes <ul><li>if - op file_name </li></ul><ul><li>-r : read access </li></ul><ul><li>-w : write access </li></ul><ul><li>-x : execute access </li></ul><ul><li>-e : existence </li></ul><ul><li>-o : ownership </li></ul><ul><li>-f : plain file </li></ul><ul><li>-d : directory </li></ul><ul><li> -l : link </li></ul>
  13. 13. <ul><li>#!/bin/csh –f </li></ul><ul><li>######## </li></ul><ul><li># sum # </li></ul><ul><li>######## </li></ul><ul><li>if $#argv == 0 then </li></ul><ul><li>echo Usage: $0 num1 [num2 num3 ...] </li></ul><ul><li>exit 1 </li></ul><ul><li>endif </li></ul><ul><li>@ sum = 0 </li></ul><ul><li>foreach number($argv) </li></ul><ul><li>@ sum += $number </li></ul><ul><li>end </li></ul><ul><li>echo The sum is : $sum </li></ul><ul><li>@ average = $sum / $#argv </li></ul><ul><li>@ remainder = $sum % $#argv </li></ul><ul><li>echo The avergae is: $average($remainder) </li></ul>
  14. 14. <ul><li>#!/bin/csh -f </li></ul><ul><li>############ </li></ul><ul><li># sort_files # </li></ul><ul><li>############ </li></ul><ul><li>if $#argv == 0 then </li></ul><ul><li>echo USAGE: $0 file_names_to_sort </li></ul><ul><li>echo This command writes on the original files !!! </li></ul><ul><li>exit 1 </li></ul><ul><li>endif </li></ul><ul><li>foreach file($argv) </li></ul><ul><li>sort $file > $file.tmp </li></ul><ul><li>mv $file.tmp $file </li></ul><ul><li>end </li></ul>
  15. 15. <ul><li>#!/bin/csh -f </li></ul><ul><li># Biggest_file </li></ul><ul><li># INPUT: Directory name </li></ul><ul><li># OUTPUT: The file with the biggest number of characters in the given directory </li></ul><ul><li>if $#argv == 0 then </li></ul><ul><li>echo USAGE: $0 directory_name </li></ul><ul><li>exit 1 </li></ul><ul><li>endif </li></ul><ul><li>if -d $1 then </li></ul><ul><li>@ max = 0 </li></ul><ul><li>foreach file($1/*) </li></ul><ul><li>if (-r $file && -f $file) then </li></ul><ul><li>set wc_out = `wc -c $file` </li></ul><ul><li>if ($wc_out[1] > $max) then </li></ul><ul><li>set biggest_file = $wc_out[2] </li></ul><ul><li>@ max = $wc_out[1] </li></ul><ul><li>endif </li></ul><ul><li>else </li></ul><ul><li>if !(-r $file) then echo $file unreadable </li></ul><ul><li> endif </li></ul><ul><li>end </li></ul><ul><li>echo The biggest file is $biggest_file </li></ul><ul><li>echo The number of characters is $max </li></ul><ul><li>else </li></ul><ul><li>echo $1 is not a directory </li></ul><ul><li>endif </li></ul>
  16. 16. <ul><li>#!/bin/csh -f </li></ul><ul><li># Modulo3 </li></ul><ul><li># INPUT: sequence of integer numbers separated by terminated by 0 </li></ul><ul><li># OUTPUT: prints the value of each number modulo 3 </li></ul><ul><li>set num = $< </li></ul><ul><li>while ($num != 0) </li></ul><ul><li>@ modulo = $num % 3 </li></ul><ul><li>switch ($modulo) </li></ul><ul><li>case 0: </li></ul><ul><li>echo 0 </li></ul><ul><li>breaksw </li></ul><ul><li>case 1: </li></ul><ul><li>echo 1 </li></ul><ul><li>breaksw </li></ul><ul><li>case 2: </li></ul><ul><li>echo 2 </li></ul><ul><li>breaksw </li></ul><ul><li>default: </li></ul><ul><li>endsw </li></ul><ul><li>set num = $< </li></ul><ul><li>end </li></ul>
  17. 17. Retrieving returned values <ul><li>Returned values (by return, exit) are stored in status environment variable </li></ul><ul><li>The script: </li></ul><ul><li>#!/bin/csh -f </li></ul><ul><li>./prog </li></ul><ul><li>if $status != 0 then </li></ul><ul><li>echo &quot;Error occured!&quot; </li></ul><ul><li>endif </li></ul><ul><li>runs prog (from current dir) and reports </li></ul><ul><li>whether an error occurred. </li></ul>
  18. 18. awk <ul><li>Useful utility for file manipulation </li></ul><ul><li>For details see : </li></ul><ul><ul><li>http://www.gnu.org/manual/gawk-3.1.1/gawk.html </li></ul></ul>
  19. 19. <ul><li>#!/bin/awk -f </li></ul><ul><li>BEGIN { stud_num = total = high_stud_num = total_high = 0} </li></ul><ul><li>NF != 3 { print &quot;error in line&quot;, FNR, &quot;:&quot;, $0 </li></ul><ul><li> next } </li></ul><ul><li>{ ++stud_num </li></ul><ul><li> total += $3 </li></ul><ul><li> if ($3 > 80){ </li></ul><ul><li>total_high += $3 </li></ul><ul><li>++high_stud_num </li></ul><ul><li> } </li></ul><ul><li>} </li></ul><ul><li>END { print &quot;Number of students: &quot;, stud_num </li></ul><ul><li>print &quot;Avergae grade is: &quot;, total / stud_num </li></ul><ul><li>print &quot;Average of high grades is: &quot;, total_high / high_stud_num} </li></ul><ul><li> </li></ul>
  20. 20. <ul><li>Running with input : </li></ul><ul><li>Ron 033453671 91 </li></ul><ul><li>Yael 034567832 73 </li></ul><ul><li>Ran 040478124 100 </li></ul><ul><li>Yoav 060381253 95 </li></ul><ul><li>Tal 045623141 78 90 </li></ul><ul><li>Output is : </li></ul><ul><li>error in line 5 : Tal 045623141 78 90 </li></ul><ul><li>Number of students: 4 </li></ul><ul><li>Avergae grade is: 89.75 </li></ul><ul><li>Average of high grades is: 95.3333 </li></ul>

×