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语句
一.for语法 for 变量 in 列表:do 循环体 done 二.常见用法 1.for用来遍历目录 1 2 3 4 5 6 7 8 9 10 #!/bin/bash #Version:0. ...
- shell 学习之case语句
一般建议变量用引号括起来 -v 显示信息 case shift 把刚才的变量踢掉 一.case语句:语法结构case stitch in value1) statement ... ...
- [Bash Shell] Shell学习笔记
1. Shell简介 Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命 ...
- shell学习-基础篇
shell学习-基础篇 Linux? 挺好的! shell是基础- 最近利用闲暇时间在 http://c.biancheng.net/ 网站上学习了shell基础篇,整理成博客以加深理解 文章目录 L ...
- Linux| |Shell学习
Shell学习 # 符号 1. 初识 1.1 Shell定位 Shell就是用C编写的程序,是用户是用Linux的桥梁.Shell就是Linux内核的一个外壳,调用内核的接口 1.2 Shell和Ba ...
- shell学习笔记 (2)
shell学习笔记 (2) ---china.pub.com linux shell电子书学习笔记 第2章 使用find和xargs find pathname -options [-print - ...
- 详解Shell编程之if语句实战(小结)
本篇文章主要介绍了详解Shell编程之if语句实战(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 对于if语句,其实很多人都肯定的听说过,那么if语句到底是什么, ...
- Python语言学习之常见语句命令那些事:python和常见语句命令(条件语句、pass语句)使用方法之详细攻略
Python语言学习之常见语句命令那些事:python和常见语句命令(条件语句.pass语句)使用方法之详细攻略 目录 Python常见语句命令 1.python的条件语句 2.Python之pass ...
- 【转】shell学习笔记(一)——学习目的性、特殊字符、运算符等
1 学习shell的目的性 写之前我们先来搞清楚为什么要学shell,学习要有目的性 shell简单.灵活.高效,特别适合处理一些系统管理方面的小问题 shell可以实现自动化管理,让系统管理员的工作 ...
最新文章
- RNN,LSTM,GRU基本原理的个人理解重点
- redis3.2.3安装部署实战
- 2013年展望:大数据发展十大趋势分析
- 批量kill掉包含某个nginx的进程
- 2017.10.9 放棋子 思考记录
- 【registry】registry An exception was thrown while processing request with message
- JEE_Ajax技术
- wpf中button的无边框实现
- 解决标签回车后产生的空格2
- iis php 知乎,设置 | WeCenter创建你的知乎
- Python文件属性、文件摘要
- Office-014 显示域代码
- html文件中top什么意思,margin-top在html中的意思是什么
- 使用R制作世界热力地图
- 微信小程序最简单的轮播图
- 教大家用python画皮卡丘的脸
- 洛谷 P2517 [HAOI2010]订货
- 华农acm:scau9505 射穿多少
- 论“东数西算”对气象行业的影响
- 前端拖拽drag的使用
热门文章
- .NET 4.5 HttpClient 中使用Cookie
- eclipse部署web没部署成功的问题
- 此电脑怎么放在桌面上_电脑内部与麦克风的声音怎么同时录制?详细教程在此...
- 后台admin省市县镇公共组件
- idea lombok插件安装_开发效率不高?墙裂推荐这十款精选 IntelliJ IDEA 插件
- mysql读会产生事务吗_来谈谈MySQL事务及事务引发的问题
- 理解 Generator 的执行
- C#代码实现矢量画图
- 【转】AndroidStudio升到最新版本(3.1.2)之后
- MyBatis学习总结_03_优化MyBatis配置文件中的配置