参数与返回

脚本参数

命令行参数

所有命令行参数(位置参数)都可以通过特殊的外壳变量 $1,$2,$3,…,$9 获得。

#!/bin/bash
echo "The script name : $0"
echo "The value of the first argument to the script : $1"
echo "The value of the second argument to the script : $2"
echo "The value of the third argument to the script : $3"
echo "The number of arguments passed to the script : $#"
echo "The value of all command-line arguments (\$* version) : $*"
echo "The value of all command-line arguments (\$@ version) : $@"

# ./cmdargs.sh bmw ford toyota
The script name : ./cmdargs.sh
The value of the first argument to the script : bmw
The value of the second argument to the script : ford
The value of the third argument to the script : toyota
The number of arguments passed to the script : 3
The value of all command-line arguments ($* version) : bmw ford toyota
The value of all command-line arguments ($@ version) : bmw ford toyota

这里对 $@$* 的异同再次进行阐述:

  • $@ expanded as “$1” “$2” “$3” … “$n”
  • $* expanded as “$1y$2y$3y…$n”, where y is the value of $IFS variable i.e. “$*” is one long string and $IFS act as an separator or token delimiters.

相对完整的示例如下:

if test $# = 1
then
    start=1
    finish=$1
elif test $# = 2
then
    start=$1
    finish=$2
else
    echo "Usage: $0 <start> <finish>" 1>&2
    exit 1
fi

for argument in "$@"
do
    if echo "$argument" | egrep -v '^-?[0-9]+$' >/dev/null
    then
        echo "$0: argument '$argument' is not an integer" 1>&2
        exit 1
    fi
done

number=$start
while test $number -le $finish
do
    echo $number
    number=`expr $number + 1`    # or number=$(($number + 1))
done

使用说明

您可以使用 if 命令来检查命令行参数。未通过必需的命令行选项时,许多 Linux 命令都会显示错误或使用情况信息。例如,尝试以下命令:

gcc

# gcc: no input files

取决于用户输入的 shell 脚本必须:验证传递给它的参数数量。如果未将参数或输入传递给脚本,则显示错误或用法消息。您的 shell 脚本还可以使用 if 命令和 $# 特殊的 shell 变量参数来创建此类用法消息。创建一个名为 userlookup.sh 的外壳脚本:

#!/bin/bash
# A shell script to lookup usernames in /etc/passwd file
# Written by: Vivek Gite
# Last updated on: Sep/10/2003
# -------------------------------------------------------
# Set vars
user=$1   # first command line argument

passwddb=/etc/passwd

# Verify the type of input and number of values
# Display an error message if the username (input) is not correct
# Exit the shell script with a status of 1 using exit 1 command.
[ $# -eq 0 ] && { echo "Usage: $0 username"; exit 1; }

grep "^$user" $passwddb >/dev/null
retval=$?    # store exit status of grep

# If grep found username, it sets exit status to zero
# Use exit status to make the decision
[ $retval -eq 0 ] && echo "$user found" || echo "$user not found"

Shell 参数

  • All command line parameters or arguments can be accessed via $1, $2, $3,…, $9.
  • $* holds all command line parameters or arguments.
  • $# holds the number of positional parameters.
  • $- holds flags supplied to the shell.
  • $? holds the return value set by the previously executed command.
  • $$ holds the process number of the shell (current shell).
  • $! hold the process number of the last background command.
  • $@ holds all command line parameters or arguments.

exit

每个 Linux 命令正常或异常终止时都会返回一个状态。您可以在 shell 脚本中使用退出状态的值来显示错误消息或采取某种措施。例如,如果 tar 命令不成功,它将返回一个代码,该代码告诉 Shell 脚本向 sysadmin 发送电子邮件。Shell 脚本或用户执行的每个 Linux 命令都具有退出状态。退出状态是整数。Linux 手册页统计了每个命令的退出状态。退出状态为 0 表示命令成功执行,没有任何错误。非零(1-255 值)的退出状态表示命令失败。您可以使用名为 $ 的特殊 shell 变量。获取先前执行的命令的退出状态。要打印 $ 变量使用 echo 命令:

echo $?
date  # run date command
echo $? # print exit status
foobar123 # not a valid command
echo $? # print exit status

ls -l /tmp
status=$?
echo "ls command exit stats - $status"

退出状态不仅限于 Shell 脚本。每次命令终止的 Shell 程序都会获得一个退出代码,以指示命令的成功或失败。因此,我们可以使用特定的 bash 变量 $? 来获取命令的退出状态。例如:

$ ping -q -c 4 www.cyberciti.biz >/dev/null
$ echo $?

在此示例中,我们将仅看到最后一个命令(command3)的退出状态:

command1 | command2 | command3
## will get the exit status of the last command in the pipeline ##
echo $?
下一页