shell编程中for/while/until循环命令

简介:

一、for命令

    在shell编程中,有时我们需要重复执行一直命令直至达到某个特定的条件,bash shell中,提供了for命令,允许你创建一个遍历一系列值的循环,每次迭代都通过一个该系列中的值执行一组预定义的命令。

for的基本格式:

    for var in list

    do

        commands

    done

    在list中,你提供了迭代中要用的一系列值。在每个迭代中,变量var包含列表中的当前值,第一个迭代会适用列表中的第一个值,第二个迭代使用第二个值,以此类推,直至列表中的所有值都过一遍。


1.1读取列表中的值

1
2
3
4
5
6
7
8
9
10
11
12
[root@sh shell] # cat for1.sh
#!/bin/bash
for  test  in  aaa bbb ccc ddd
do
         echo  the next state is $ test
done
 
[root@sh shell] # sh for1.sh 
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd

$test变量的值会在shell脚本的最后一次迭代中一直保持有效,除非你修改了它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@sh shell] # cat for1.sh 
#!/bin/bash
for  test  in  aaa bbb ccc ddd
do
     echo  the next state is $ test
done
echo  "the last state we visited was $test"
test =fff
echo  "wait. now we're visiting $test"
 
 
[root@sh shell] # sh for1.sh 
the next state is aaa
the next state is bbb
the next state is ccc
the next state is ddd
the last state we visited was ddd
wait. now we're visiting fff


1.2读取列表中的复杂值

在shell脚本中,优势你会遇到难处理的数。下面是个给shell脚本程序员带来麻烦的经典例子:

1
2
3
4
5
6
7
8
9
10
11
[root@sh shell] # cat for2.sh 
#!/bin/bash
for  test  in  I don 't know if this' ll work
do
     echo  "word:$test"
done
 
[root@sh shell] # sh for2.sh 
word:I
word:dont know  if  thisll
word:work

解决办法:使用转义符或者双引号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@sh shell] # cat for2.sh 
#!/bin/bash
for  test  in  I don\ 't know if "this' ll" work
do
     echo  "word:$test"
done
 
[root@sh shell] # sh for2.sh 
word:I
word:don't
word:know
word: if
word:this'll
word:work


记住:for循环假定每一个值都是用空格分割的,如果在单独的数字值中有空格,那么你必须使用双引号来将这些值圈起来。



1.3从变量读取列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@sh shell] # cat for3.sh 
############################
#!/bin/bash
 
list= "aaa bbb ccc ddd eee"
list=$list " Connecticut"
for  state  in  $list
 
do
   echo  "Have you ever visited $state"
done
 
 
[root@sh shell] # sh for3.sh 
Have you ever visited aaa
Have you ever visited bbb
Have you ever visited ccc
Have you ever visited ddd
Have you ever visited eee
Have you ever visited Connecticut


1.4从命令读取值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@sh shell] # cat for4.sh 
#!/bin/bash
file = "/root/shell/states"   #如果是在当前不用绝对路径,file=“states”即可
 
for  state  in  ` cat  $ file `
do
   echo  "Visit beautiful $state"
done
 
[root@sh shell] # sh for4.sh 
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guangzhou
 
[root@sh shell] # cat states 
shanghai
beijing
hangzhou
nanjing
guangzhou

1.5更改字段分隔符

  • 空格;

  • 制表符:

  • 换行符


    如果bash shell在数据中看到了这些字符中任意一个,它就会假定你在列表中开始了一个新的数据段。

    要解决这个问题,你可以在你shell脚本中临时更改IFS环境变量的值来限制一下被bash shell当作字段分隔符的字符。但这种方式有点奇怪,比如,如果你行IFS的值使其只能识别换行符,你必须这么做:

    IFS=$'\n'

    将这个语句加入到脚本中,告诉bash shell在数据值中忽略空格和制表符。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@sh shell] # cat for5.sh 
#!/bin/bash
file = "states"
 
IFS=$ '\n'
for  state  in  ` cat  $ file `
do
   echo  "Visit beautiful $state"
done
 
[root@sh shell] # sh for5.sh 
Visit beautiful shanghai
Visit beautiful beijing
Visit beautiful hangzhou
Visit beautiful nanjing
Visit beautiful guang zhou
Visit beautiful nan ning
Visit beautiful jiang nan

在处理长脚本中,可能在一个地方需要修改IFS的值,然后忘掉它在脚本中其他地方还原默认值。

    例如:

        IFS.OLD=$IFS

        IFS=$'\n'

        <use the new IFS value in code>

        IFS=$IFS.OLD


其他的IFS值,如:在/etc/passwd中可能用到

    IFS=:

也可以赋值多个IFS:

    IFS=$'\n:;"'


1.6用通配符读取目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@sh shell] # cat for6.sh 
#!/bin/bash
for  file  in  /home/ *
do
 
   if  [ -d  "$file"  ]; then
     echo  "$file is a directory"
   elif  [ -f  "$file"  ]; then
     echo  "$file is a file"
   fi
 
done
 
[root@sh shell] # sh for6.sh 
/home/apache-tomcat-8 .0.28. tar .gz is a  file
/home/dir1  is a directory
/home/dir2  is a directory
/home/fie1  is a  file
/home/fie2  is a  file
/home/fie22  is a  file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@sh shell] # cat for7.sh 
#!/bin/bash
for  file  in  /home/ /home/badtest
do
 
   if  [ -d  "$file"  ]; then
     echo  "$file is a directory"
   elif  [ -f  "$file"  ]; then
     echo  "$file is a file"
   else
     echo  "$file doesn't exist"
   fi
 
done
[root@sh shell] # sh for7.sh 
/home/apache-tomcat-8 .0.28. tar .gz is a  file
/home/dir1  is a directory
/home/dir2  is a directory
/home/fie1  is a  file
/home/fie2  is a  file
/home/fie22  is a  file
/home/badtest  doesn't exist


二、C语言风格的for命令

2.1 C语言风格的for命令

    C语言的for命令有一个用来指明变量的特殊方法、一个必须保持成立才能继续迭代的条件,以及另一个为每个迭代改变变量的方法。当指定的条件不成立时,for循环就会停止。条件等式通过标准的数字符号定义。

    for (i=0; i<10; i++)

    {

        printf("The next number is %d\n",i):

    }

    第一部分将一个默认值赋给该变量,中间的部分定义了循环重复的条件,当定义的条件不成立时,for循环就停止迭代,最后一部分定义了迭代的过程。


bash中C语言风格的for循环的基本格式:

    for (( variable assignment;condition;iteration process))

    for (( a = 1; a < 10; a++ ))



本文转自 HMLinux 51CTO博客,原文链接:http://blog.51cto.com/7424593/1738811

相关文章
|
28天前
|
运维 Shell 数据库
Python执行Shell命令并获取结果:深入解析与实战
通过以上内容,开发者可以在实际项目中灵活应用Python执行Shell命令,实现各种自动化任务,提高开发和运维效率。
55 20
|
1月前
|
安全 Shell 数据处理
使用Python执行Shell命令并获取结果
在实际应用中,可以根据需要选择适当的参数和方法来执行Shell命令,并处理可能出现的各种情况。无论是系统管理、自动化任务还是数据处理,掌握这些技巧都将极大地提高工作效率。
64 12
|
3月前
|
人工智能 Shell iOS开发
AI Shell:在命令行里“对话” AI ,微软推出将 AI 助手引入命令行的 CLI 工具,打造对话式交互命令行
AI Shell 是一款强大的 CLI 工具,将人工智能直接集成到命令行中,帮助用户提高生产力。AI Shell 支持多种 AI 模型和助手,通过多代理框架提供丰富的功能和灵活的使用模式。
355 7
|
3月前
|
Java Shell Windows
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
java Runtime.exec()执行shell/cmd命令:常见的几种陷阱与一种完善实现
86 1
|
4月前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
107 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
3月前
|
运维 监控 Shell
深入理解Linux系统下的Shell脚本编程
【10月更文挑战第24天】本文将深入浅出地介绍Linux系统中Shell脚本的基础知识和实用技巧,帮助读者从零开始学习编写Shell脚本。通过本文的学习,你将能够掌握Shell脚本的基本语法、变量使用、流程控制以及函数定义等核心概念,并学会如何将这些知识应用于实际问题解决中。文章还将展示几个实用的Shell脚本例子,以加深对知识点的理解和应用。无论你是运维人员还是软件开发者,这篇文章都将为你提供强大的Linux自动化工具。
|
4月前
|
Shell
Shell编程(下)
Shell编程(下)
123 1
|
4月前
|
Shell Linux Windows
Shell编程(上)
Shell编程(上)
65 1
|
4月前
|
Shell 知识图谱
Shell printf 命令
10月更文挑战第3天
38 1
|
4月前
|
Shell PHP
Shell echo命令
10月更文挑战第3天
34 0