Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology

Similar presentations


Presentation on theme: "UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"— 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script Examples (1) $ [[ "aaa" == "bbb" ]] && ls do not list anything $ [[ "aaa" != "bbb" ]] && ls list current directory $ [[ ("a" == "b")||(0==0) ]] && ls list current directory

4 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script Example (4) #!/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 Homework 2 but for list of files in argument

8 Dr. Tran, Van Hoai 2007 UNIX Shell Script for command (2) for (( expr1 ; expr2 ; expr3 )) do list done Like C, java

9 Dr. Tran, Van Hoai 2007 UNIX Shell Script Example (5) Compute factorial #!/bin/sh ############################## fac=1 for (( x=1; x<=$1; x++ )) do fac=$(( $fac * $x )) done echo $fac

10 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script Conditional expression -a file True if file exists -d file True if file exists and is a directory -f file True if file exists and is a regular file -h file True if file exists and is a symbolic link -[rwx] file True if file exists and is readable, writable, or executable -z string True if length of string is zero ==, !=, String comparison -eq, -ne, -lt, -le, -gt, -ge Arithmetic comparison

12 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 " exit ;; esac done

17 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script 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 Dr. Tran, Van Hoai 2007 UNIX Shell Script Exercise Write a script to sort list of names (countries) alphabetically $ myscript.sh Vietnam USA Ukraina Russia Russia Ukraina USA Vietnam

23 Dr. Tran, Van Hoai 2007 UNIX Shell Script Homework Write a script to show a menu for repeated tasks Can we write a script to perform linear algebra operations ?

24 Dr. Tran, Van Hoai 2007 UNIX Shell Script Regular expressions is NEXT


Download ppt "UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology"

Similar presentations


Ads by Google