shell脚本for循环

如题所述

shell脚本中的for循环是怎样的呢?下面就让我们一起来了解一下吧:
在shell脚本中编写脚本使用for循环一般是用于判断输入的用户名是否存在,若是不存在的话那么创建该用户并设置密码,否则程序会继续提示用户,也就是提示重新输入新建用户名称。
在for命令中的for i in的各种用法介绍如下

for i in “file1” “file2” “file3”
for i in /boot/*
for i in /etc/*.conf
for i in $(seq -w 10) --》等宽的01-10
for i in {1…10}
for i in $( ls )
for I in $( file)
for i in “$@” --》取所有位置参数,可以简写为for i
需要注意的是bash shell支持C式for循环。
示例代码如下:
#!/bin/bash
j=$1
for ((i=1; i=j; i++))
do
touch file$i echo file $i is ok
done
$@: 所有位置变量的内容
$#: 位置变量的个数
$0: 文件名
$*: 所有位置变量的内容
for循环的一般代码格式为:
for 变量名 in 列表
do
command1
command2
...
commandN
done
参考范例:
范例一
输入代码:
for loop in 1 2 3 4 5
do
echo The value is: $loop
done
输出结果为:
The value is: 1
The value is: 2
The value is: 3
The value is: 4
The value is: 5
范例二
若是编写脚本清空所有arp缓存记录,示例代码如下:
#!/bin/bash
for i in $(arp | tail -n +2|tr -s |cut -d -f1)
do
arp -d $i
done
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网