嵌入式 shell 学习之for语句
一、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 f 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 x in $bashuser; do
echo "bash users is:$x"
done
#遍历使用nologin的用户并打印出来
for y 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
|
嵌入式 shell 学习之for语句相关推荐
- shell 学习之for语句
shell 学习之for语句 一.for语法 for 变量 in 列表:do 循环体 done 二.常见用法 1.for用来遍历目录 1 2 3 4 5 6 7 8 9 10 #!/bin/b ...
- shell 学习之case语句
一般建议变量用引号括起来 -v 显示信息 case shift 把刚才的变量踢掉 一.case语句:语法结构case stitch in value1) statement ... ...
- 嵌入式开发学习路线图
之前看到别人在学习视频中的回复,觉得很全面,分享给大家 --------------我就是个搬运工 基础学习Ⅰ---Linux入门 目前嵌入式主要开发环境有 Linux.Wince等:L ...
- shell学习-基础篇
shell学习-基础篇 Linux? 挺好的! shell是基础- 最近利用闲暇时间在 http://c.biancheng.net/ 网站上学习了shell基础篇,整理成博客以加深理解 文章目录 L ...
- 利用SpiderMonkey进行嵌入式开发——学习总结
利用SpiderMonkey进行嵌入式开发--学习总结 许峰 2007/07/30 最近在学习javascript引擎SpiderMonkey,学了一个星期了,终于有点眉目,现将学习经验记录下来,已被 ...
- 嵌入式linux学习步骤
<一> 1.Linux 基础 安装Linux操作系统 Linux文件系统 Linux常用命令 Linux启动过程详解 熟悉Linux服务能够独立安装Linux操作系统 能够熟练使用Linu ...
- [Bash Shell] Shell学习笔记
1. Shell简介 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命 ...
- linux嵌入式如何学习,嵌入式Linux要如何学习?嵌入式linux学习路线讲解
在学习嵌入式linux之前,一定要有C语言基础,而且是越熟练越好.对于汇编基础来说没有那么重要,当然对于C语言来说起码能够熟练写出一个数组排序.输入数字求和之类的程序代码.当然如果不熟悉的话就需要多写 ...
- 嵌入式linux学习路线参考(LINUX学习者必看经典)
整理了嵌入式linux学习路线供参考,希望对您有所参考价值! 一.linux入门 目前嵌入式主要开发环境有 Linux.Wince等:Linux因其开源.开发操作便利而被广泛采用.而Linux操作系统 ...
最新文章
- 二叉树的前序,中序,后序的递归、迭代实现
- 中介分析 相对直接效应 相对简介效应_中介效应分析方法和流程
- Windows 网络服务架构系列课程详解(六) ---利用NLB群集实现WEB服务器的可靠性...
- android 进lanucher的广播,(转)Android中Launcher对于AppWidget处理的分析:AppWidgetHost角色...
- linux服务器同时运行两个或多个tomcat
- spring + shiro + cas 实现sso单点登录
- linux centos8新特性及安装教程
- CentOS查看每个进程的网络流量
- jenkins解决jenkins内存溢出问题
- Top3获奖金10万,Seq2seq对话系统设计方案
- java加密解密代码_java加解密文件公用方法整合(多看一本书,少写三行代码)
- 大数据该如何使用进行分析
- PAT1137 Final Grading
- 编程基本功:聊了一下午上学房子,晚上却自行加班
- Atitit 标签式tab 切换的实现 Softdev=declare+intercept 申明+解释 软件=代码+文档 软件=数据结构+算法 软件=程序+数据+文档 申明式 decla
- Visual Studio 2019设置透明背景图片
- Echarts 柱形图最全详解
- 安徽大学计算机复试刷人比例,658人进复试刷掉564多人!盘点21复试比奇高、刷人狠的院校专业...
- SpringMvc后端往往前端
- 基于学术研究于NASA官网GPM卫星数据下载详细教程