#!/bin/bash
#this program is use $0 $# and so on
echo "THe script name is ==> ${0}"
echo "The total parameter number is ==> $#"
["$#" -lt 2 ] && echo "the number of parameter is less than 2,Stop here ." && exit 0
echo "your whole parameter is '$@'"
echo "your whole para is '$*'"
echo "the 1st parameter ==>${1}"
echo "the 2dn parameter ==>${2}"
echo "the 2dn parameter ==>${3}"
echo "the 2dn parameter ==>${4}"
执行结果:
v2x@ubuntu:~/shell_script_learn$ ./bianliang.sh I love you 520 这边执行时有4个参数
THe script name is ==> ./bianliang.sh
The total parameter number is ==> 4
./bianliang.sh: line 5: [4: command not found
your whole parameter is 'I love you 520'
your whole para is 'I love you 520'
the 1st parameter ==>I
the 2dn parameter ==>love
the 2dn parameter ==>you
the 2dn parameter ==>520
echo "THe script name is ==> ${0}" 这个脚本的名字
echo "The total parameter number is ==> $#" 这个脚本执行时有几个参数
["$#" -lt 2 ] && echo "the number of parameter is less than 2,Stop here ." && exit 0
echo "your whole parameter is '$@'" 脚本执行时的所有参数,参数各自独立
echo "your whole para is '$*'" 脚本执行时的所有参数,参数用一个空格隔开,是一个整体
echo "the 1st parameter ==>${1}" 脚本执行时的第一个参数
echo "the 2dn parameter ==>${2}" 脚本执行时的第二个参数
echo "the 2dn parameter ==>${3}" 脚本执行时的第三个参数
echo "the 2dn parameter ==>${4}" 脚本执行时的第四个参数
本文深入探讨了Bash脚本中变量与参数的使用方法,包括如何获取脚本名称、参数总数、所有参数及其独立和组合形式,以及如何处理特定数量的参数。通过实际案例展示了Bash脚本在参数处理上的灵活性。

被折叠的 条评论
为什么被折叠?



