嵌入式 shell 学习之for语句

本文介绍如何使用for循环遍历目录、实现求和、创建无限循环,并通过实例展示了如何利用for循环生成列表,特别关注了bash用户列表的生成与打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、for语法

for 变量 in 列表;do
    循环体
done

二、常见用法
1、for用来遍历目录
  

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
#Version:0.1
#Author:lovelace
#pragram:This scripts is print all files in directory
#difine an varibale
DIR="/home/scripts/51cto"
#All files in directory traversal
for in $(ls $DIR);do
echo $f
done

 

输出结果为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@lovelace for]# ./dir.sh
1.sh
2curl.sh
adduer.sh
aliquot.sh
argument.sh
argusum.sh
curl.sh
dd.sh
dirper.sh
info.sh
info.tt
ipcheck.sh
jugement.sh
netcard.sh
sum.sh
test.sh
The
Frist
week
The
Third
week

 

2、for ((初始条件;终止条件;异动项))
do
   命令区域
done
   

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
#Version:0.1
#Author:lovelace
#pragram:This pragram is and the sum from 1 to 100
#define an integer
declare -i i
#loops
for ((i=1;i<=10;i=i+1))
do
let sum+=1
done
echo "The result is:" $sum


输出结果为:

1
2
[root@lovelace for]# ./sorsum.sh
The result is: 10

3、for 无穷循环
    for ((;1;));do
      命令区域
     done

    

1
2
3
4
5
[root@lovelace for]# cat forover.sh
#!/bin/bash
for ((;1;));do
echo "forever..."
done

输出结果:

1
2
3
4
5
6
7
8
[root@lovelace for]# ./forover.sh
forever...
forever...
forever...
forever...
forever...
forever...
forever...

三、关于break和continue


break、continue 一样可以运用在for while until select这4中循环中,

break :退出循环  提前退出
continue:提前进入下一轮循环 ,continue后面的语句将不再执行

示例(计算1到100内能被3整除的数之和):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/bin/bash
#Verson:0.1
#Auther:lovelace
#Pragram:This pragram is calculation from 1 to 100 aliquot 3 sum
#
declare -i i=0
declare -i sum=0
#use loop traverse from 1 to 100
while [ $i -lt 100 ];do
let i++
#jugement aliqotu 3 or not
if [ $(($i%3))  -eq 0 ];then
let sum+=i
else
continue
fi
done
#print sum
echo "from 1 to 100 aliquot 3 sum is $sum"

输出结果为:

1
2
[root@lovelace for]# ./three.sh
from 1 to 100 aliquot 3 sum is 1683

四、再次重提如何生成列表
如何生成列表:
1、整数列表  
     {1..100}  生存1到100的列表
2、seq 
    seq 10 1到10
    seq 5 10 5到10
    seq 5 10 2 返回列表为6 8 10
3、`ls /etc`

 

生成列表不单单只有我们列出的这些,实际案例上需要灵活运用

示例:(分别显示当前系统上所有默认shell中为bash的用户和默认为nologin的用户)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[root@lovelace for]# cat checkbash.sh
#!/bin/bash
#Version:0.1
#Author:lovelace
#pragram:This scripts is check user bash and print it
#取出使用bash的用户个数
bashline=`grep 'bash$' /etc/passwd wc -l`
#取出使用nologin的用户个数
nologinline=`grep 'nologin$' /etc/passwd wc -l`
#取出bash用户列表
bashuser=`grep 'bash$' /etc/passwd cut -d: -f1`
#取出nologin用户列表
nologin=`grep 'nologin$' /etc/passwd cut -d: -f1`
#遍历使用bash的用户并打印出来
for in  $bashuser;do
echo "bash users is:$x"
done
#遍历使用nologin的用户并打印出来
for in  $nologin;do
echo "nologin users is:$y"
done
#结果如下
[root@lovelace for]# ./checkbash.sh
bash users is:root
bash users is:nick
bash users is:kale
bash users is:user2
bash users is:user3
bash users is:user4
bash users is:user5
bash users is:user6
bash users is:user7
bash users is:user8
bash users is:user9
bash users is:user10
bash users is:mark
bash users is:lovelace
bash users is:lovetest
nologin users is:bin
nologin users is:daemon
nologin users is:adm
nologin users is:lp
nologin users is:mail
nologin users is:uucp
nologin users is:operator
nologin users is:games
nologin users is:gopher
nologin users is:ftp
nologin users is:nobody
nologin users is:nscd
nologin users is:vcsa
nologin users is:pcap
nologin users is:ntp
nologin users is:dbus
nologin users is:avahi
nologin users is:rpc
nologin users is:mailnull
nologin users is:smmsp
nologin users is:sshd
nologin users is:oprofile
nologin users is:rpcuser
nologin users is:nfsnobody
nologin users is:xfs
nologin users is:haldaemon
nologin users is:avahi-autoipd
nologin users is:gdm
nologin users is:sabayon
nologin users is:jack

### 嵌入式 Linux Shell 命令教程 #### 了解 Shell 的基本概念 Shell 是一种命令解释器,负责接收用户的指令并将这些指令转化为操作系统可以理解的形式来执行相应操作。通过编写 Shell 脚本——即包含一系列按顺序排列的命令集合的文本文件,用户可以让计算机自动完成复杂的任务处理流程[^2]。 #### 创建简单的 Shell 脚本实例 为了更好地掌握如何利用 Shell 进行开发,在这里给出一段用于展示当前工作目录下所有 .txt 文件大小总和的小型脚本例子: ```bash #!/bin/bash total=0 for file in *.txt; do size=$(stat --format=%s "$file") total=$((total + size)) done echo "Total size of all txt files is $total bytes" ``` 此段代码首先定义了一个名为 `total` 的变量用来保存累加后的字节数;接着遍历当前路径下的每一个 `.txt` 类型文档,并调用 `stat` 工具获取其实际占用空间数;最后输出统计结果给终端显示。 #### 探索更多实用工具与技巧 除了上述提到的基础功能之外,还有许多其他重要的知识点等待探索,比如条件判断语句、循环结构以及函数封装等特性都能极大提升工作效率。对于希望深入研究的朋友来说,《Linux》基础篇三提供了丰富的关于 shell 命令的内容介绍[^1]。 #### 学习资源推荐 针对想要进一步提高自己在这方面技能水平的学习者而言,《嵌入式 Linux Shell 编程》是一份不可多得的好资料,它不仅涵盖了从入门到精通所需的知识要点,还附带大量实战案例供读者练习巩固所学内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值