参数与返回
脚本参数
命令行参数
所有命令行参数(位置参数)都可以通过特殊的外壳变量
#!/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- $IFSvariable i.e. “- $*” is one long string and- $IFSact 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
使用说明
您可以使用
gcc
# gcc: no input files
取决于用户输入的$# 特殊的
#!/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
每个$ 的特殊$ 变量使用
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"
退出状态不仅限于$? 来获取命令的退出状态。例如:
$ 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 $?
