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 <