1 / 24

UNIX Shell Script (2)

UNIX Shell Script (2). Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn. Compound commands. list = sequence of commands separated by " | ", " ; ", " & ", " && ", " || ", new line (list) executed in a subshell

melosa
Télécharger la présentation

UNIX Shell Script (2)

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. UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology hoai@cse.hcmut.edu.vn

  2. Compound commands • list = sequence of commands separated by "|", ";", "&", "&&", "||", new line • (list) executed in a subshell • { list; } executed in current shell (whitespaces separated) • ((expression)expression evaluation • [[ expression ]] conditional expression evaluation (whitespaces needed)

  3. Examples (1) $ [[ "aaa" == "bbb" ]] && ls do not list anything $ [[ "aaa" != "bbb" ]] && ls list current directory $ [[ ("a" == "b")||(0==0) ]] && ls list current directory

  4. for command (1) for name [ in word ] ; do list ; done • name is expanded to list of elements • name is assigned each element, then execute list each time • if no [ in word ], arguments are used

  5. Example (2) $ for x in `ls`; do echo $x; done list the current directory $ for x; do echo $x; done list arguments $ for x in 10, 11, 12; do echo $x; done list 10, 11, 12

  6. Example (3) • Homework 2 #!/bin/bash files=`ls $1` for f in $files do nf=`echo $f | tr "[A-Z]" "[a-z]"` mv -f $f $nf 2>/dev/null || echo "Error renaming file $f" done

  7. Example (4) • Homework 2 but for list of files in argument #!/bin/sh ############################## for files do fl=`ls $files` for f in $fl do nf=`echo $f | tr "[A-Z]" "[a-z]"` mv -f $f $nf 2>/dev/null done done

  8. for command (2) for (( expr1 ; expr2 ; expr3 )) do list done • Like C, java

  9. Example (5) • Compute factorial #!/bin/sh ############################## fac=1 for (( x=1; x<=$1; x++ )) do fac=$(( $fac * $x )) done echo $fac

  10. if command (1) if list; then list; [ elif list; then list; ] … [else list; ] fi • exis status of the last command, or 0 if no condition is tested true

  11. Conditional expression

  12. Example (6) • Homework 2 + only process regular files #!/bin/bash files=`ls $1` for f in $files do if [ -f $f ]; then nf=`echo $f | tr "[A-Z]" "[a-z]"` mv -f $f $nf 2>/dev/null || echo "Error renaming file $f" fi done

  13. while, until commands while list; do list; done • performs until last command in while list return non-zero until list; do list; done • performs until last command in while list return zero

  14. Example (7) #!/bin/bash while read line do f1=`echo $line | tr " " "\t" | cut –f1` f2=`echo $line | tr " " "\t" | cut –f2` sum=$(($f1+$f2)) echo $sum done < $1

  15. case command case word in [ [(] pattern[|pattern]…) list; ]…esac • word is expanded • After the first match, no subsequent marches are executed • If no pattern matches, exit status=0. Otherwise, exit status = that of last command

  16. Example (8) #!/bin/bash verbose=0 archive=0 for arg; do case "$arg" in -v) verbose=1 ;; -a) archive=1 ;; -h) echo "Usage: script4.sh -a -v -h <filename>" exit ;; esac done

  17. Assignment • Write a script to compute columns (given on arguments) with 3 options • "-h": shows usage • "-s": shows values of columns • Example • script.sh –s 1 2 3 results.txt

  18. Array (1) • One-dimensional • No maximum limit on size • Indexed using integers and zero-based • Explicitly declare an array declare–a name • Implicitly declare an array name[subscript]=value

  19. Array (2) • Assignment myarray=([1]=5 [3]=6 [4]="hello") • Reference echo ${myarray[4]} • Length of array echo ${#myarray[*]} • Destroy an array unset myarray unset myarray[3]

  20. Function [ function ] name () compound-command [redirection] • Compound-command = list of commands between { and } • Exit status = that of last command • Define a local variable local myvar

  21. Example (9) #!/bin/bash myfac(){ local fac=1 x for (( x=1; x<=$1; x++ )) do fac=$(( $fac * $x )) done echo $fac } echo `myfac $1`

  22. Exercise • Write a script to sort list of names (countries) alphabetically $ myscript.sh Vietnam USA Ukraina Russia Russia Ukraina USA Vietnam

  23. Homework • Write a script to show a menu for repeated tasks • Can we write a script to perform linear algebra operations ?

  24. Regular expressions is NEXT

More Related