280 likes | 445 Views
CSE4251 The Unix Programming Environment. Lecture 7 Bash scripting II: more on if test loops (for, while/until) command line arguments examples. Recap. Bash scripting I Script introduction anything you can type on the command line + control flow statements #!/bin/bash
E N D
CSE4251 The Unix Programming Environment Lecture 7 Bash scripting II: more on if test loops (for, while/until) command line arguments examples
Recap • Bash scripting I • Script introduction • anything you can type on the command line + control flow statements • #!/bin/bash • add execution permission: $ chmodu+x ./myscript.sh • Variables & operations • no space around the =: MY_VAR="hi there" • arithmetic expression: let or ((...)) • Conditions: • if ... then ... fi • if ... then ... else ...fi • if ... then ... elif... elif ... else ... fi
More on if test • check if file exists • check if file is a directory (or if the directory exists) #!/bin/bashfile=./myfile if [[ -e $file ]]; then echo“$file exists!" fi #!/bin/bashfile=./myfile if [[ -d $file ]]; then echo“$file is a directory!" fi
loops • for • while/until forvarinconst1 const2 const3 ... do statements done for (( var_init; condition; var_update )) do statements done while [ condition] do statements done until [ condition] do statements done
for loop • for1.sh #!/bin/bashfor iin12345doecho"Looping ... number $i"done $ ./for1.sh Looping .... number 1Looping .... number 2Looping .... number 3Looping .... number 4Looping .... number 5
for loop • for2.sh #!/bin/bashforiin hello 1*2 goodbye doecho"Looping ... i is set to $i"done
for loop • for2.sh #!/bin/bashforiin hello 1*2 goodbye doecho"Looping ... i is set to $i"done $ ./for2.sh Looping ... i is set to helloLooping ... i is set to 1Looping ... i is set to 0repos #name of first file in current directory... etc ...Looping ... i is set to WinSentinel#name of last file in current directoryLooping ... i is set to 2Looping ... i is set to goodbye
for loop • for3.sh : invoke extern command seq #!/bin/bash for i in $(seq 1 100) do echo "first loop: i = $i" done for i in `seq 1 100` do echo "second loop: i = $i" done two ways to invoke external cmd
for loop • for4.sh : arithmetic expression #!/bin/bash read -p "Please input a number, I will count for 1+2+...+ your_input: " num sum=0 for (( i=1; i<=$num; i=i+1 )) do sum=$(( $sum + $i)) done echo "1+2+...+$num = $sum" [15:43:57][zhengm@apollo-tesla:~/cse4251] $ ./for4.sh Please input a number, I will count for 1+2+...+ your_input: 1000 1+2+...+1000 = 500500
while/until loop • while1.sh #!/bin/bash input=hello while["${input}"!="bye"]doecho"Please type something in (bye to quit): "read inputecho"You typed: ${input}"done echo “OK, bye.”
while/until loop • while2.sh #!/bin/bash while["${input}"!="bye"]doread -p"Please type something in (bye to quit): " inputecho"You typed: ${input}"done echo “OK, bye.”
while/until loop • until.sh #!/bin/bash until["${input}"=="bye"]doread -p"Please type something in (bye to quit): " inputecho"You typed: ${input}"done echo “OK, bye.”
command line arguments • Default variables in script: • $0, $1, $2, ... • total number of arguments (except $0): $# • all arguments: $@ or $* • $ /path/to/script arg1 arg2 arg3 arg4 • $0 $1 $2 $3 $4
command line arguments • arg1.sh #!/bin/bash echo "the script is: $0" echo "total number of arguments is: $#" echo “list all arguments via \$@: $@" echo “list all arguments via \$*: $*"
command line arguments • arg1.sh [18:16:50][zhengm@apollo-tesla:~/cse4251] $ ./arg1.sh a b c the script is: ./arg1.sh total number of arguments is: 3 list all arguments via $@: a b c list all arguments via $*: a b c [18:17:58][zhengm@apollo-tesla:~/cse4251] $ ./arg1.sh a "b c" the script is: ./arg1.sh total number of arguments is: 2 list all arguments via $@: a b c list all arguments via $*: a b c
command line arguments #!/bin/bash echo "the script is: $0" echo "total number of arguments is: $#" echo "iterate over all arguments via \$@: " for arg in "$@" do echo "$arg“ echo "--" done echo "iterate over all arguments via \$*: " for arg in "$*" do echo "$arg“ echo "--" done • arg2.sh
command line arguments • arg2.sh $ ./arg2.sh a b c the script is: ./arg2.sh total number of arguments is: 3 iterate over all arguments via $@: a -- b -- c -- iterate over all arguments via $*: a b c --
command line arguments • arg2.sh $ ./arg2.sh a "b c" the script is: ./arg2.sh total number of arguments is: 2 iterate over all arguments via $@: a -- b c -- iterate over all arguments via $*: a b c --
command line arguments #!/bin/bash echo "the script is: $0" echo "total number of arguments is: $#" echo "iterate over all arguments via \$@: " for arg in $@ do echo "$arg“ echo "--" done echo "iterate over all arguments via \$*: " for arg in $* do echo "$arg“ echo "--" done • arg3.sh
command line arguments $ ./arg3.sh a "b c" the script is: ./arg3.sh total number of arguments is: 2 iterate over all arguments via $@: a -- b -- c -- iterate over all arguments via $*: a -- b -- c -- • arg3.sh
Example 1 • Task: list all command line arguments prepended by argument’s positional index $ ./arg4.sh a b "c d" e arg# 1 is: a arg# 2 is: b arg# 3 is: c d arg# 4 is: e
Example 1 • Task: list all command line arguments prepended by argument’s positional index #!/bin/bash index=1 for arg in "$@" do echo "arg# $index is: $arg" let "++index" done $ ./arg4.sh a b "c d" e arg# 1 is: a arg# 2 is: b arg# 3 is: c d arg# 4 is: e
Example 2 • Task: iterate over each item in the current directory, and check if it is a regular file or a directory $ ./checkfiles.sh arg1.sh is a regular file arg2.sh is a regular file arg3.sh is a regular file arg4.sh is a regular file checkfiles.sh is a regular file for3.sh is a regular file for4.sh is a regular file lab2 is a directory scripts is a directory until.sh is a regular file while2.sh is a regular file
Example 2 • Task: iterate over each item in the current directory, and check if it is a regular file or a directory `ls`: invoke external cmd ls #!/bin/bash for file in `ls` do if [[ -d $file ]]; then echo "$file is a directory" else echo "$file is a regular file" fi done -d: is it a directory?
Example 3 • Task: change the access rights of the files in a directory (including files in the subdirectories)
Example 3 • Task: change the access rights of the files in a directory (including files in the subdirectories) • solution 1: change_permission1.sh (use for loop and hard-coded permissions) #!/bin/bash for file in `ls`; do if [[ -d $file ]]; then chmod–R 700 $file else chmod750 $file fi done
Example 3 • Task: change the access rights of the files in a directory (including files in the subdirectories) • solution 2: change_permission2.sh (add arguments) #!/bin/bash if [[ "$#"!=2 ]]; then echo "Please specify two arguments as permissions" exit 1 fi for file in `ls`; do if [[ -d $file ]]; then chmod–R $1 $ilfe else chmod $2 $file fi done
Lab 3 • http://web.cse.ohio-state.edu/~zhengm/teaching/cse4251au14/CSE4251%20Lab%203.htm • DUE: 11:59pm, Monday, Nov 3, 2014