1 for 命令 for 命令的基本格式: for var in list do commands done 1. 2. 3. 4. 在list 参数中, 需要提供迭代中要用到的 一系列值. 每次迭代, 变量 $var 会包含 list 的当前值.do 和 done 语句之间是每次迭代需要执行的命令(组). for 命令读取列表中的值: $ cat temp.sh #
#!/bin/bashfor file in $( ls )#for file in *doecho "file: $file"done 也可一使用for file in *,通配符*产生文件名扩展,匹配当前目录下的所有文件。 for通过命令行来传递脚本中for循环列表参数 #!/bin/bashecho "number of arguments is $#"echo "What you input is: "for argument in "$@"d...
① forname in列表;do 循环体 done ②for (( exp1; exp2; exp3 )) ;do cmd done exp1只执行一次,相当于在for里嵌了while ③ 执行机制:依次将列表中的元素赋值给“变量名”; 每次赋值后即执行一次循环体; 直到列表中的元素耗尽,循环结束 列表的表示方法,可以glob 通配符,如{1..10} 、*.sh ;也可以...
for username in ;do useradd $username done 遍历list元素,遍历结束,循环退出。 #for i in `seq 601 610`; do echo $i;done #for i in `seq 601 610`; do echo user$i;done #for i in `seq 601 610`; do useradd tuser$i;done #for i in {601..610};do useradd tuser$i;done bash...
# 显示ls的结果echo$CUR_DIRforvalin$CUR_DIRdo# 若val是文件,则输出该文件名if[ -f $val ];thenecho"FILE: $val"fidoneexit0 二、输出1-10之间数字的总和。bash脚本内容如下: #!/bin/bashsum=0for((i=1;i<10;i++))do((sum=$sum+$i))doneecho"sum=$sum"exit0 ...
其实file命令本身即可实现,主要是了解一下可以以通配符展开来生成LIST。 #file/root/* 4、计算当前所有用户的UID之和。 #!/bin/bash declare-isum=0foriin$(cut-d : -f3/etc/passwd);dosum=$[$sum+$i]doneecho"The sum of UIDs is $sum." ...
#filename:debug.sh for i in {1..6}; do set -x echo $i set +x done echo "script executed" [cairui@cai shell]$ sh debug.sh + echo 1 1 + set +x + echo 2 2 + set +x + echo 3 3 + set +x + echo 4 4 + set +x ...
⑥与for循环类似,可以省略 in list, 此时使用位置参量 (2)案例: 生成菜单,并显示选中的价钱 PS3="Please choose the menu: " select menu in mifan huimian jiaozi babaozhou quit do case $REPLY in 1|4) echo "the price is 15" ;; 2|3) ...
echo"FILE: $val"fi done 2.4 循环 For循环基本格式:for variable in list do commands done While语句基本语句格式为:while test-condition do commands done 2.5 数组 Bash中数组是通过空格符号隔开,并且是包含在()里面。引用时从序号0开始。如:Array=(23.5 27 29 31 25.7) ...
find_lock.sh - tries to find if a lockfile is used in the given or current working directory by taking snapshots of the file list before and after a prompt in which you should open/close an application foreach_path_bin.sh - runs each binary of the given name found in $PATH with the...