运维Shell脚本小试牛刀(一)
运维Shell脚本小试牛刀(二)
运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解
运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客
Cenos7安装小火车程序动画
运维Shell脚本小试牛刀(五):until循环
运维Shell脚本小试牛刀(六): Shell中的函数认知
运维Shell脚本小试牛刀(七):从函数文件中调用另外一个脚本文件中函数
运维Shell脚本小试牛刀(八): case模式忽略命令行参数大小写演示
运维Shell脚本牛刀小试(九): 重定向操作符“>“及双重定向“>>“
运维Shell脚本小试牛刀(十)
运维Shell牛刀小试(十一):for循环读取多个命令行参数
一: for循环来接收命令行参数,进行分叉处理不同的逻辑
当有n个命令行参数数,处理使用shift命令读取命令行参数进行分叉处理外,通常还可以使用for循环与特殊参数$*|$@读取命令行参数进行相关的分支逻辑处理
[root@www standandinout]# cat forreadparameter.sh
#!/bin/bash -
#==================================================================================================================
#
#
# FILE: forreadparameter.sh
# USAGE: ./forreadparameter.sh
# DESCRIPTION: for循环读取命令行参数
# OPTIONS: ------------------------------------------------------------------------------------------
# REQUIREMENTS: -------------------------------------------------------------------------------------
#
# BUGS: ---------------------------------------------------------------------------------------------
# NOTES: --------------------------------------------------------------------------------------------
# AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
# ORGANIZATION:
# CREATED: 2023-8-24 09:11:20
# REVISION: ------------------------------------------------------------------------------------------
#
#
#
#
#
#====================================================================================================================
# 定义变量E_quit_signal
e_quit_signal=68#如果特殊变量的$1的值为空,则打印脚本的使用方法,并以退出状态码68退出脚本
if [ ! -n $1 ]
then
# 打印脚本的使用方法到标准输出
echo "Usage: `basename $0` argument1 argument2 ........................................."# 退出脚本,退出状态码为68
exit ${e_quit_signal}
fi# 定义变量index
index=1
# 打印双引号中的内容到标准输出
echo "Listing args with \$*:"# 使用for
for arg in $*
do# 打印输出变量index和arg的值及相应的内容到标准输出
echo "Arg #$index = $arg......"# 将变量index的值加1
let index+=1
done
echo
# 重新将变量的值赋值为1
index=1#打印双引号中的内容到标准输出
echo "Listing args with \"\$@\":"# 使用for循环遍历特殊变量$@的值
for arg in "$@"
do# 打印输出变量index和arg的值及相应内容到标准输出
echo "Arg #$index = $arg"
#将变量index的值加1
let index+=1
done
二: 脚本测试
[root@www standandinout]# ./forreadparameter.sh one two three four five six seven eight night ten
Listing args with $*:
Arg #1 = one......
Arg #2 = two......
Arg #3 = three......
Arg #4 = four......
Arg #5 = five......
Arg #6 = six......
Arg #7 = seven......
Arg #8 = eight......
Arg #9 = night......
Arg #10 = ten......Listing args with "$@":
Arg #1 = one
Arg #2 = two
Arg #3 = three
Arg #4 = four
Arg #5 = five
Arg #6 = six
Arg #7 = seven
Arg #8 = eight
Arg #9 = night
Arg #10 = ten