# using one command line parameter # factorial=1 for (( number = 1; number <= $1; number++ )) do truefactorial=$[ $factorial * $number ] done echo The factorial of $1 is $factorial wsx@wsx-ubuntu:~/script_learn$ ./test1.sh 5 The factorial of 5 is 120
wsx@wsx-ubuntu:~/script_learn$ cat test7.sh #!/bin/bash # testing parameters before use # if [ -n "$1" ] # -n 选项检查字符是否非空 then trueecho Hello $1, glad to meet you. else trueecho "Sorry, you did not identify yourself." fi
wsx@wsx-ubuntu:~/script_learn$ ./test7.sh shixiang Hello shixiang, glad to meet you. wsx@wsx-ubuntu:~/script_learn$ ./test7.sh Sorry, you did not identify yourself.
wangsx@SC-201708020022:~/tmp$ cat test9.sh #!/bin/bash # Testing parameters # if [ $# -ne 2 ] then echo echo Usage: test9.sh a b echo else total=$[ $1 + $2 ] echo echo The total is $total echo fi
wangsx@SC-201708020022:~/tmp$ cat test15.sh #!/bin/bash # extracting command line option as parameter # echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) echo "Found the -b option";; -c) echo "Found the -c option";; *) echo "$1 is not an option";; esac shift done
wangsx@SC-201708020022:~/tmp$ ./test15.sh -a -b -c -d
Found the -a option Found the -b option Found the -c option -d is not an option
wangsx@SC-201708020022:~/tmp$ cat test16.sh #!/bin/bash # extracting options and paramters echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) echo "Found the -b option";; -c) echo "Found the -c option";; --) shift break ;; *) echo "$1 is not an option";; esac shift done # count=1 for param in $@ do echo "Parameter #$count: $param" count=$[ $count + 1 ] done
wangsx@SC-201708020022:~/tmp$ cat test17.sh #!/bin/bash # extracting command line options and values echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option, with parameter value $param" shift;; -c) echo "Found the -c option";; --) shift break;; *) echo "$1 is not an option";; esac shift done # count=1 for param in "$@" do echo "Paramter #$count: $param" count=$[ $count + 1 ] done wangsx@SC-201708020022:~/tmp$ ./test17.sh -a -b test1 -d
Found the -a option Found the -b option, with parameter value test1 -d is not an option
wangsx@SC-201708020022:~/tmp$ cat test18.sh #!/bin/bash # Extract command line options & values with getopt # set -- $(getopt -q ab:cd "$@") # echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option, with parameter value $param" shift ;; -c) echo "Found the -c option";; --) shift break ;; *) echo "$1 is not an option" esac shift done # count=1 for param in "$@" do echo "Parameter #$count: $param" count=$[ $count + 1 ] done #
可以看到它跟test17.sh不同的地方是加入了getopt命令来帮助格式化命令行参数。
下面测试发现新加的功能实现了,之前的也没问题。
1 2 3 4 5 6 7 8 9 10 11 12 13
wangsx@SC-201708020022:~/tmp$ ./test18.sh -ac
Found the -a option Found the -c option wangsx@SC-201708020022:~/tmp$ ./test18.sh -a -b test1 -cd test2 test3 test4
Found the -a option Found the -b option, with parameter value 'test1' Found the -c option -d is not an option Parameter #1: 'test2' Parameter #2: 'test3' Parameter #3: 'test4'
相当不错啦。不过getopt命令隐藏一个问题。
1 2 3 4 5 6 7 8 9
wangsx@SC-201708020022:~/tmp$ ./test18.sh -a -b test1 -cd "test2 test3" test4
Found the -a option Found the -b option, with parameter value 'test1' Found the -c option -d is not an option Parameter #1: 'test2 Parameter #2: test3' Parameter #3: 'test4'
wangsx@SC-201708020022:~/tmp$ cat test19.sh #!/bin/bash # Simple demonstration of the getoptscommand # echo while getopts :ab:c opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -b option, with value $OPTARG";; c) echo "Found the -c option";; *) echo "Unknown option: $opt";; esac done wangsx@SC-201708020022:~/tmp$ ./test19.sh -ab test1 -c
Found the -a option Found the -b option, with value test1 Found the -c option
wangsx@SC-201708020022:~/tmp$ cat test20.sh #!/bin/bash # Processing options & parameters with getopts # echo while getopts :ab:cd opt do case "$opt" in a) echo "Found the -a option" ;; b) echo "Found the -a option, with value $OPTARG" ;; c) echo "Found the -c option" ;; d) echo "Found the -d option" ;; *) echo "Unknown option: $opt" ;; esac done # shift $[ $OPTIND -1 ] # echo count=1 for param in "$@" do echo "Parameter $count: $param" count=$[ $count + 1 ] done
wangsx@SC-201708020022:~/tmp$ ./test20.sh -a -b test1 -d test2 test3 test4
Found the -a option Found the -a option, with value test1 Found the -d option
wangsx@SC-201708020022:~/tmp$ cat test21.sh #!/bin/bash # testing the read -p option # read -p "Please enter your age: " age # days=$[ $age * 365 ] echo "That makes you over $days days old!" # wangsx@SC-201708020022:~/tmp$ ./test21.sh Please enter your age: 23 That makes you over 8395 days old!
wangsx@SC-201708020022:~/tmp$ cat test22.sh #!/bin/bash # entering multiple variable # read -p "Enter your name: " first last echo "Checking data for $last, $first..."
wangsx@SC-201708020022:~/tmp$ ./test22.sh Enter your name: shixiang wang Checking data for wang, shixiang... wangsx@SC-201708020022:~/tmp$ ./test22.sh Enter your name: shixiang wang hhhhh Checking data for wang hhhhh, shixiang...
wangsx@SC-201708020022:~/tmp$ cat test23.sh #!/bin/bash # timing the data entry # if read -t 5 -p "Please enter your name: " name then echo "Hello $name, welcome to my script" else echo echo "Sorry, too slow!" fi
wangsx@SC-201708020022:~/tmp$ ./test23.sh Please enter your name: shixiang wang Hello shixiang wang, welcome to my script wangsx@SC-201708020022:~/tmp$ ./test23.sh # 这里输入后等以下 不要输入 Please enter your name: Sorry, too slow!
wangsx@SC-201708020022:~/tmp$ cat test24.sh #!/bin/bash # getting just one character of input # read -n1 -p "Do you want to continue [Y/N]? " answer case $answer in Y | y ) echo echo "fine, continue on...";; N | n ) echo echo OK, goodbye exit;; esac echo "This is the end of the script." wangsx@SC-201708020022:~/tmp$ ./test24.sh Do you want to continue [Y/N]? Y fine, continue on... This is the end of the script. wangsx@SC-201708020022:~/tmp$ ./test24.sh Do you want to continue [Y/N]? n OK, goodbye
wangsx@SC-201708020022:~/tmp$ cat test25.sh #!/bin/bash # hiding input data from the monitor # read -s -p "Enter your password: " pass echo echo "Is your password really $pass?" wangsx@SC-201708020022:~/tmp$ ./test25.sh Enter your password: Is your password really what? wangsx@SC-201708020022:~/tmp$ ./test25.sh Enter your password: Is your password really shixiang?
wangsx@SC-201708020022:~/tmp$ cat test26.sh #!/bin/bash # reading data from a file # count=1 cat test | while read line do echo "Line $count: $line" count=$[ $count + 1 ] done echo "Finished processing the file"
wangsx@SC-201708020022:~/tmp$ cat test The quick brown dog jumps over the lazy fox. This is a test, this is only a test. O Romeo, Romeo! Wherefore art thou Romeo? wangsx@SC-201708020022:~/tmp$ ./test26.sh Line 1: The quick brown dog jumps over the lazy fox. Line 2: This is a test, this is only a test. Line 3: O Romeo, Romeo! Wherefore art thou Romeo? Finished processing the file