260 likes | 433 Views
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
E N D
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 • { list; } executed in current shell (whitespaces separated) • ((expression)expression evaluation • [[ expression ]] conditional expression evaluation (whitespaces needed)
Examples (1) $ [[ "aaa" == "bbb" ]] && ls do not list anything $ [[ "aaa" != "bbb" ]] && ls list current directory $ [[ ("a" == "b")||(0==0) ]] && ls list current directory
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
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
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
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
for command (2) for (( expr1 ; expr2 ; expr3 )) do list done • Like C, java
Example (5) • Compute factorial #!/bin/sh ############################## fac=1 for (( x=1; x<=$1; x++ )) do fac=$(( $fac * $x )) done echo $fac
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
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
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
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
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
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
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
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
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]
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
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`
Exercise • Write a script to sort list of names (countries) alphabetically $ myscript.sh Vietnam USA Ukraina Russia Russia Ukraina USA Vietnam
Homework • Write a script to show a menu for repeated tasks • Can we write a script to perform linear algebra operations ?
Regular expressions is NEXT