bash shell 提供了一些不同的方法来从
用户处获取数据,包括命令行参数(添加在命令后的数据)、命令行选项(可改变命令行为的单个字母) 以及直接从键盘读取输入。
命令行参数允许运行脚本时在命令行中添加数据:
$ ./addem 10 30
读取参数
$ cat positional1.sh
#!/bin/bash
# Using one command-line parameter
#
factorial=1
for (( number = 1; number <= $1; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $1 is $factorial
exit
$
$ cat positional10.sh
#!/bin/bash
# Handling lots of command-line parameters
#
product=$[ ${10} * ${11} ]
echo The tenth parameter is ${10}.
echo The eleventh parameter is ${11}.
echo The product value is $product.
exit
$
$ ./positional10.sh 1 2 3 4 5 6 7 8 9 10 11 12
The tenth parameter is 10.
The eleventh parameter is 11.
The product value is 110.
$ ./positional0.sh
This script name is ./positional0.sh .
$ $HOME/scripts/positional0.sh
This script name is /home/christine/scripts/positional0.sh .
name=$(basename $0)
echo This script name is $name.
在 bash shell 中有一些跟踪命令行参数的特殊变量:
参数统计
if [ $# -eq 1 ]
if [ $# -ne 2 ]
echo The last parameter is ${$#} // 错误写法
echo The last parameter is ${!#} // 正确写法
$ cat grabbingallparams.sh
#!/bin/bash
# Testing different methods for grabbing all the parameters
#
echo
echo "Using the \$* method: $*"
echo
echo "Using the \$@ method: $@"
echo
exit
$
$ ./grabbingallparams.sh alpha beta charlie delta
Using the $* method: alpha beta charlie delta
Using the $@ method: alpha beta charlie delta
$
$ cat grabdisplayallparams.sh
#!/bin/bash
# Exploring different methods for grabbing all the parameters
#
echo
echo "Using the \$* method: $*"
count=1
for param in "$*"
do
echo "\$* Parameter #$count = $param"
count=$[ $count + 1 ]
done
#
echo
echo "Using the \$@ method: $@"
count=1
for param in "$@"
do
echo "\$@ Parameter #$count = $param"
count=$[ $count + 1 ]
done
echo
exit
$ ./grabdisplayallparams.sh alpha beta charlie delta
Using the $* method: alpha beta charlie delta
$* Parameter #1 = alpha beta charlie delta
Using the $@ method: alpha beta charlie delta
$@ Parameter #1 = alpha
$@ Parameter #2 = beta
$@ Parameter #3 = charlie
$@ Parameter #4 = delta
$
$ cat shiftparams.sh
#!/bin/bash
# Shifting through the parameters
#
echo
echo "Using the shift method:"
count=1
while [ -n "$1" ]
do
echo "Parameter #$count = $1"
count=$[ $count + 1 ]
shift
done
echo
exit
$
$ ./shiftparams.sh alpha bravo charlie delta
Using the shift method:
Parameter #1 = alpha
Parameter #2 = bravo
Parameter #3 = charlie
Parameter #4 = delta
$ cat bigshiftparams.sh
#!/bin/bash
# Shifting multiple positions through the parameters
#
echo
echo "The original parameters: $*"
echo "Now shifting 2..."
shift 2
echo "Here's the new first parameter: $1"
echo
exit
$
$ ./bigshiftparams.sh alpha bravo charlie delta
The original parameters: alpha bravo charlie delta
Now shifting 2...
Here's the new first parameter: charlie
$
$ cat extractoptions.sh
#!/bin/bash
# Extract command-line options
#
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
echo
exit
$
$ ./extractoptions.sh -a -b -c -d
Found the -a option
Found the -b option
Found the -c option
-d is not an option
$ cat extractoptionsparams.sh
#!/bin/bash
# Extract command-line options and parameters
#
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
#
echo
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
echo
exit
$
$ cat extractoptionsvalues.sh
#!/bin/bash
# Extract 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
#
echo
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
exit
$
$ ./extractoptionsvalues.sh -a -b BValue -d
Found the -a option
Found the -b option with parameter value BValue
-d is not an option
$
getopt optstring parameters
$ getopt ab:cd -a -b BValue -cd test1 test2
-a -b BValue -c -d -- test1 test2
$ getopt ab:cd -a -b BValue -cde test1 test2
getopt: invalid option -- 'e'
-a -b BValue -c -d -- test1 test2
$ getopt -q ab:cd -a -b BValue -cde test1 test2
-a -b 'BValue' -c -d -- 'test1' 'test2'
set -- $(getopt -q ab:cd "$@")
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
#
echo
count=1
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + 1 ]
done
exit
getopts optstring variable
$ cat extractwithgetopts.sh
#!/bin/bash
# Extract command-line options and values with getopts
#
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option with parameter value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
exit
$
$ ./extractwithgetopts.sh -ab BValue -c
Found the -a option
Found the -b option with parameter value BValue
Found the -c option
$
$ ./extractwithgetopts.sh -b "BValue1 BValue2" -a
$ ./extractwithgetopts.sh -abBValue
$ ./extractwithgetopts.sh -d
Unknown option: ?
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option with parameter 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
exit
| 选项 | 描述 |
|---|---|
| -a | 显示所有对象 |
| -c | 生成计数 |
| -d | 指定目录 |
| -e | 扩展对象 |
| -f | 指定读入数据的文件 |
| -h | 显示命令的帮助信息 |
| -i | 忽略文本大小写 |
| -l | 产生长格式输出 |
| -n | 使用非交互模式(批处理) |
| -o | 将所有输出重定向至指定的文件 |
| -q | 以静默模式运行 |
| -r | 递归处理目录和文件 |
| -s | 以静默模式运行 |
| -v | 生成详细输出 |
| -x | 排除某个对象 |
| -y | 对所有问题回答 yes |
echo -n "Enter your name: "
read name
echo "Hello $name, welcome to my script."
exit
read -p "Please enter your age: " age
days=$[ $age * 365 ]
echo "That means you are over $days days old!"
read -p "Enter your first and last name: " first last
read -p "Enter your name: "
echo
echo "Hello $REPLY, welcome to my script."
if read -t 5 -p "Enter your name: " name
then
echo "Hello $name, welcome to my script."
else
echo
echo "Sorry, no longer waiting for name."
fi
read -n 1 -p "Do you want to continue [Y/N]? " answer
#
case $answer in
Y | y) echo
echo "Okay. Continue on...";;
N | n) echo
echo "Okay. Goodbye"
exit;;
esac
echo "This is the end of the script."
exit
read -s -p "Enter your password: " pass
echo
echo "Your password is $pass"
exit
count=1
cat $HOME/scripts/test.txt | while read line
do
echo "Line $count: $line"
count=$[ $count + 1 ]
done
echo "Finished processing the file."
exit