首页 > 代码库 > bash脚本(二)
bash脚本(二)
for循环
for VAR in LIST; do
statements
...
done
LIST列表:
1、直接给出
2、数值列表
{start..end},如{1..10}
seq LAST
seq FIRST LAST
seq FIRST STEP LAST,如seq 1 2 10,会读取到13579
3、特殊变量$#,$@,$*
4、通过命令引用获取的参数列表,如($(grep ‘bash$‘ /etc/passwd | cut -d: -f3))
5、通配符读取目录 /tmp/test/*
通过命令引用获取的参数列表
[root@localhost ~]# cat /root/test
aa
bb
cc
dd
ee
[root@localhost ~]# cat test.sh
#!/bin/bash
declare -i x=1
for i in `cat /root/test`; do
echo "value$x$count:=$i"
let x++
done
[root@localhost ~]# bash test.sh
value1:=aa
value2:=bb
value3:=cc
value4:=dd
value5:=ee
通配符读取目录
[root@localhost ~]# cat test.sh
#!/bin/bash
declare -i x=1
for i in /tmp/*; do
echo "value$x$count:=$i"
let x++
done
[root@localhost ~]# bash test.sh
value1:=/tmp/test1
value2:=/tmp/test2
value3:=/tmp/test3
value4:=/tmp/test
for的C风格
for (( variable assignment; condition; iteration process)); do
#statements
done
#/bin/bash
for (( a = 1,b=10; a <= 10; a++,b-- )); do
echo "$a-$b"
done
选择语句
单分支if语句
if [[ condition ]]; then
#statements TRUE
fi
if可以嵌套
if [[ condition ]]; then
if [[ condition ]]; then
#statements
fi
fi
双分支if语句
if [[ condition ]]; then
#statements TRUE
else
#statements FLASE
fi
多分支if语句,哪个condition为TRUE满足则执行下面的statements
if [[ condition ]]; then
elif [[ condition ]]; then
#statements
elif [[ condition ]]; then
#statements
else
#statements
fi
case语句
case word in
pattern )
;;
pattern )
;;
pattern )
;;
* )
;;
esac
pattern可使用通配符:
*:任意长度的任意字符
?: 任意单个字符
[]:指定范围内的任意单个字符
a|b: a或者b
[root@localhost ~]# cat test.sh
#!/bin/bash
read -p "Enter a choice" choice
case ${choice:-b} in
a) echo "a";;
b) echo "b";;
c) echo "b";;
esac
[root@localhost ~]# bash test.sh
Enter a choice
b
while,until语句
while [[ condition TRUE ]]; do
#statements
修正表达式否则会死循环
done
until [[ condition FALSE ]]; do
#statements
修正表达式否则会死循环
done
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9
2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18
3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27
4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36
5x5=25 5x6=30 5x7=35 5x8=40 5x9=45
6x6=36 6x7=42 6x8=48 6x9=54
7x7=49 7x8=56 7x9=63
8x8=64 8x9=72
9x9=81
#!/bin/bash
declare -i i=1
declare -i j=1
while [[ $i -lt 10 ]]; do
let j=$i
until [[ $j -gt 9 ]]; do
echo -e -n "${i}x${j}=$[$i*$j]\t"
let j++
done
let i++
echo
done
9x9=81
8x8=64 8x9=72
7x7=49 7x8=56 7x9=63
6x6=36 6x7=42 6x8=48 6x9=54
5x5=25 5x6=30 5x7=35 5x8=40 5x9=45
4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36
3x3=9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27
2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18
1x1=1 1x2=2 1x3=3 1x4=4 1x5=5 1x6=6 1x7=7 1x8=8 1x9=9
#!/bin/bash
declare -i i=9
while [[ $i -gt 0 ]]; do
j=$i
until [[ $j -gt 9 ]]; do
echo -e -n "${i}x${j}=$[$i*$j]\t"
let j++
done
echo
let i--
done
while的特殊用法
[root@localhost ~]# cat /tmp/test aa aa aa aabb bb bb bbcc cc cc cc
#!/bin/bash
count=1
cat /tmp/test | while read line; do
echo "Line $count:$line"
let count++
done
#!/bin/bash
count=1
while read line; do
echo "Line $count:$line"
let count++
done < /tmp/test
- 2个脚本的效果一样
Line 1:aa aa aa aa
Line 2:bb bb bb bb
Line 3:cc cc cc cc
break可以退出进行中的循环的一个简单方法
[root@localhost ~]# cat test.sh
#!/bin/bash
for i in {1..10}; do
if [ $i -eq 5 ];then
break
fi
echo "$i"
done
[root@localhost ~]# bash test.sh
1
2
3
4
continue命令是提早结束执行循环内部的命令但不完全终止整个循环的一个途径
[root@localhost ~]# cat test.sh
#!/bin/bash
for i in {1..15}; do
if [[ $i -gt 1 ]] && [[ $i -lt 14 ]]; then
continue
fi
echo "$i"
done
[root@localhost ~]# bash test.sh
1
14
15
bash脚本(二)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。