shell之for实现

第一类:数字性循环(1-10遍历为例)

expr说明

expr命令是一个手工命令行计数器,用于在LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
下面做一个简单说明:

[root@controller for]# cat test.sh
#数字运算
echo 2+3=$(expr 2 + 3)
#*是特殊符号,所以需要加转义符:\
echo 2x3=$(expr 2 \* 3)
echo 6-3=$(expr 6 - 3)
echo 6%3=$(expr 6 / 3)
# 统计字符
echo "统计字符长度:$(expr length "this is expr")"

[root@controller for]# sh test.sh 
2+3=5
2x3=6
6-3=3
6%3=2
统计字符长度:12
[root@controller for]# 

for1-1.sh

遍历1-10的一种方法

[root@controller for]# cat 1.sh 
#!/bin/bash

for((i=1;i<10;i++)); do
        echo $(expr $i \* 3 + 1)
done
[root@controller for]# sh 1.sh
4
7
10
13
16
19
22
25
28
[root@controller for]# 

for1-2.sh

for循环之 seq方式遍历数字

 [root@controller for]# 
[root@controller for]# cat 2.sh 
#!/bin/bash
     
for i in $(seq 1 10) ; do 
    echo $(expr $i \* 3 + 1);
done

[root@controller for]# sh 2.sh 
4
7
10
13
16
19
22
25
28
31
[root@controller for]# 

for1-3.sh

for之…方式遍历数字

[root@controller for]# cat 3.sh
#!/bin/bash
    
for i in {1..10} ; do
        echo $(expr $i \* 3 + 1);
done
[root@controller for]# sh 3.sh
4
7
10
13
16
19
22
25
28
31

for1-4.sh

BEGIN解释:任何在BEGIN之后列出的操作(在{}内)将在awk开始扫描输入之前执行

[root@controller for]# cat 4.sh 
#!/bin/bash
#如果不加BEGIN,程序不会结束
awk 'BEGIN{for(i=1;i<10;i++)print i}'
[root@controller for]# sh 4.sh
1
2
3
4
5
6
7
8
9
[root@controller for]# 

第二类:字符性循环

for2-1.sh

其实这种使用单个字符的都算不算遍历

[root@controller for]# cat for2-1.sh
#!/bin/bash

for i in 'This' ; do
        echo $i is file name
done
[root@controller for]# sh for2-1.sh
This is file name
[root@controller for]# 

for2-2.sh

接受参数,如果没有定义参数,则没有输出结果

[root@controller for]# cat for2-2.sh
#!/bin/bash

for i in $* ; do
        echo $i is input chart
done
[root@controller for]# sh for2-2.sh 123
123 is input chart
[root@controller for]# 
[root@controller for]# sh for2-2.sh 
[root@controller for]# 

for2-3.sh

依次遍历字符串

[root@controller for]# cat for2-3.sh
#!/bin/bash

for i in f1 f2 f3 ; do
        echo $i is appoint
done
[root@controller for]# sh for2-3.sh
f1 is appoint
f2 is appoint
f3 is appoint
[root@controller for]# 

for2-4.sh

定义变量的形式循环多个字符串

[root@controller for]# cat for2-4.sh
#!/bin/bash

list="rootfsuser date date2"
for i in $list ; do
        echo $i is appoint
done
[root@controller for]# sh for2-4.sh
rootfsuser is appoint
date is appoint
date2 is appoint
[root@controller for]# 

第三类:路径查找

for3-1.sh

遍历的形成列出该路径下的所以目录

[root@controller for]# cat for3-1.sh
#!/bin/bash

for file in /proc/* ; do
        echo $file is file path
done
[root@controller for]# sh for3-1.sh
/proc/1 is file path
/proc/10 is file path
/proc/1036 is file path
/proc/11 is file path
/proc/12 is file path
/proc/13 is file path
/proc/14 is file path
/proc/1480 is file path

for3-2.sh

遍历命令返回的结果 (这里是:默认列出当前路径所有.sh结尾的文件)

[root@controller for]# cat for3-2.sh
#!/bin/bash

for file in $(ls *.sh) ; do
        echo $file is file path
done
[root@controller for]# sh for3-2.sh
1.sh is file path
2.sh is file path
3.sh is file path
4.sh is file path
for2-1.sh is file path
for2-2.sh is file path
for2-3.sh is file path
for2-4.sh is file path
for3-1.sh is file path
for3-2.sh is file path
test.sh is file path
[root@controller for]# 

关于shell中的for循环遍历总结

1、 for((i=1;i<=10;i++));do echo $(expr $i \* 4);done
2、在shell中常用的是 for i in $(seq 10)
3、for i in `ls`
4、for i in ${arr[@]}
5、for i in $* ; do
6、for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
7、for i in f1 f2 f3 ;do
8、for i in *.txt
9、for i in $(ls *.txt)
for in语句与` `和$( )合用,利用` `或$( )的将多行合为一行的缺陷,实际是合为一个字符串数组
 
============ -_- ==============for num in $(seq 1 100)
10、LIST="rootfs usr data data2"
for d in $LIST; do
用for in语句自动对字符串按空格遍历的特性,对多个目录遍历
11、for i in {1..10}
12、for i in stringchar {1..10}
13、awk 'BEGIN{for(i=1; i<=10; i++) print i}'
注意:AWK中的for循环写法和C语言一样的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

҉人间无事人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值