运维Shell牛刀小试(十一):for循环读取多个命令行参数|read重定向读取文件内容


运维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循环读取多个命令行参数

运维Shell脚本小试牛刀(十二):awk编程尝鲜 


一: 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) ,[email protected]
#    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
 


三: read命令重定向读取文本文件的内容


[root@www standandinout]# cat readtwolines.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  readtwolines.sh
#                           USAGE: ./readtwolines.sh
#    DESCRIPTION: 从文件输入,从文件中重定向读取文件的内容 
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,[email protected]
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 如果参数的数量不为一个,则执行if结构中的语句块
if [ $# -ne 1 ];then
    #打印脚本文件的使用方法
    echo "Usage: $0 FILEPATH"

    # 退出
    exit
fi

# 定义一个变量file,并将脚本命令行第一个参数赋值给file
file=$1

# 定义一段代码块
{
  # 读取文件的一行内容,并将读取的内容存入变量line1中
  read line1
  # 读取文件的一行内容,并将读取的内容存入变量line2中
  read line2

  # 将代码块的标准输入执行file代表的文件,代码块读取的数据源来自文件file
} < $file

  # 打印变量file的值和一些信息
  echo " First line in ${file} is : "
  # 打印读取到的内容
  echo "$line1"
  
  # 打印变量file的值和一些信息
  echo " Sencond line in ${file} is : "
  # 打印读取到的内容
  echo "$line2"

# 退出脚本,且退出状态码为0
  exit 0
 
 


四: 测试代码块读取文件的内容

[root@www standandinout]# cat data.db 
Shell 重定向从标准的键盘指向从标准的文件读取数据
read命令从标准的文件一次只能读取一行命令,测试重定向是否成功
Shell默认从标准的输入设备键盘读取输入数据源

[root@www standandinout]# ./readtwolines.sh ./data.db 
 First line in ./data.db is : 
Shell 重定向从标准的键盘指向从标准的文件读取数据
 Sencond line in ./data.db is : 
read命令从标准的文件一次只能读取一行命令,测试重定向是否成功
 

猜你喜欢

转载自blog.csdn.net/u014635374/article/details/132899802