linux命令行下写for语句,Linux下Shell的for循环语句示例
循環(huán)不管在程序中還是腳本中都需要經(jīng)常用到,在寫shell腳本時(shí),經(jīng)常需要for進(jìn)行100次循環(huán)。for 循環(huán)是固定循環(huán),也就是在循環(huán)時(shí)已經(jīng)知道需要進(jìn)行幾次循環(huán)。有時(shí)也把 for 循環(huán)稱為計(jì)數(shù)循環(huán)。
Shell for循環(huán)語(yǔ)法
for 變量 in 列表
do
command1
command2
...
commandN
done
**也可以寫成:for var in list; do
讀取列表中的值
#!/bin/bash
#basic for command
for linuxidc in Linux公社 Linuxmi linux Ubuntu
do
echo The next state is $linuxidc
done
執(zhí)行結(jié)果:
linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh
The next state is Linux公社
The next state is Linuxmi
The next state is linux
The next state is Ubuntu
在最后一次迭代后,$linuxidc變量的值會(huì)在shell腳本的剩余部分保持有效。它會(huì)一直保持最后一次迭代的值(除非你已經(jīng)修改了它)。
讀取列表中的復(fù)雜值
有兩種解決辦法:
*使用轉(zhuǎn)義字符(反斜線)來(lái)將單引號(hào)轉(zhuǎn)移;
*使用雙引號(hào)來(lái)定義用到單引號(hào)的值。
#!/bin/bash
#basic for command
for linuxidc in Kotlin Linuxmi\'com linux Ubuntu "CentOS'rhel" Oracle
do
echo The next state is $linuxidc
done
執(zhí)行結(jié)果:
linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh
The next state is Kotlin
The next state is Linuxmi'com
The next state is linux
The next state is Ubuntu
The next state is CentOS'rhel
The next state is Oracle
*記住,for命令用空格來(lái)劃分列表中的每個(gè)值。如果在單獨(dú)的數(shù)據(jù)值中有空格,就必須用雙引號(hào)將這些值圈起來(lái)。
從變量讀取列表
將一系列的值都集中存儲(chǔ)在一個(gè)變量中,然后需要遍歷變量中的整個(gè)列表。
#!/bin/bash
#using a variable to hold the list
list="Linuxidc Linuxmi Ubuntu Fedora"
#向已有列表中添加或拼接一個(gè)值
list=$list" success"
for state in $list
do
echo "this word is $state"
done
執(zhí)行結(jié)果:
linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh
this word? is Linuxidc
this word? is Linuxmi
this word? is Ubuntu
this word? is Fedora
this word? is success
從命令讀取值
有兩種方式可以將命令輸出賦值給變量:
(1)反引號(hào)字符(`)
(2)$()格式
例如:
linuxidc=`date`
linuxidc=$(date)
生成列表中所需值就是使用命令的輸出。
#!/bin/bash
# reading values from a file
file="states"
for state in $(cat $file)
do
echo "welcome $state"
done
states文件內(nèi)容;
www.linuxidc.com
Hello World
Linuxmi com
linuxidc.net Linux公社
執(zhí)行結(jié)果:
linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh
welcome www.linuxidc.com
welcome Hello
welcome World
welcome Linuxmi
更改字段分隔符
造成這個(gè)問(wèn)題的原因是特殊的環(huán)境變量IFS,叫作內(nèi)部字段分隔符。默認(rèn)情況下,bash shell會(huì)將下列字符當(dāng)作字段分隔符:
*空格
*制表符
*換行符
如果bash shell在數(shù)據(jù)中看到這些字符中的任意一個(gè),它就會(huì)假定這表明了列表中一個(gè)新數(shù)據(jù)字段的開始。
想修改IFS的值,使其只能識(shí)別換行符,那就必須:
IFS=$'\n'
將這個(gè)語(yǔ)句加入到腳本中,告訴bash shell在數(shù)據(jù)值中忽略空格和制表符。
#!/bin/bash
# reading values from a file
file="states"
IFS=$'\n'
for state in $(cat $file)
do
echo "Welcome $state"
done
執(zhí)行結(jié)果:
Welcome www.linuxidc.com
Welcome Hello World
Welcome Linuxmi com
Welcome linuxidc.net Linux公社
一個(gè)可參考的安全實(shí)踐是在改變IFS之前保存原來(lái)的IFS值,之后再恢復(fù)它。
實(shí)現(xiàn):
IFS.OLD=$IFS
IFS=$'\n'
IFS=$IFS.OLD
這就保證了在腳本的后續(xù)操作中使用的是IFS的默認(rèn)值。
遍歷一個(gè)文件中用冒號(hào)分隔的值:
IFS=:
如果要指定多個(gè)IFS字符,只要將它們?cè)谫x值行串起來(lái)就行。
IFS=$'\n':;"
這個(gè)賦值會(huì)將換行符、冒號(hào)、分號(hào)和雙引號(hào)作為字段分隔符。如何使用IFS字符解析數(shù)據(jù)沒(méi)有任何限制。
用通配符讀取目錄
#!/bin/bash
for file in /home/linuxidc/linuxidc.com/*;
do
echo $file is file path \! ;
done
執(zhí)行結(jié)果:
linuxidc@linuxidc:~/linuxidc.com$ ./linuxidc.sh
/home/linuxidc/linuxidc.com/03.jpg is file path !
/home/linuxidc/linuxidc.com/123.jpg is file path !
/home/linuxidc/linuxidc.com/123.png is file path !
/home/linuxidc/linuxidc.com/2.avi is file path !
/home/linuxidc/linuxidc.com/amp is file path !
/home/linuxidc/linuxidc.com/atom-amd64.deb is file path !
/home/linuxidc/linuxidc.com/car.jpg is file path !
/home/linuxidc/linuxidc.com/car.png is file path !
/home/linuxidc/linuxidc.com/chenduling.jpg is file path !
/home/linuxidc/linuxidc.com/com.testdemo.java is file path !
/home/linuxidc/linuxidc.com/DarkPicDir is file path !
/home/linuxidc/linuxidc.com/data is file path !
類C風(fēng)格for循環(huán)的語(yǔ)法格式
for((expr1; expr2; expr3))
do
command
command
...
done
有些部分并沒(méi)有遵循bash shell標(biāo)準(zhǔn)的for命令:
*變量賦值可以有空格
*條件中的變量不以美元符開頭
*迭代過(guò)程的算式為用expr命令格式
ex9、輸出前6個(gè)正數(shù)
#!/bin/bash
#使用類C風(fēng)格for循環(huán)輸出1~6
for ((integer = 1; integer <= 6; integer++))
do
echo "$integer"
done
執(zhí)行如下:
盡管可以使用多個(gè)變量,但你只能在for循環(huán)中定義一種條件。
#!/bin/bash
for ((x=1,y=8;x<=8;x++,y--))
do
echo "$x - $y"
done
執(zhí)行如下:
使用類C風(fēng)格for循環(huán)要注意以下事項(xiàng):
a.如果循環(huán)條件最初的退出狀態(tài)為非0,則不會(huì)執(zhí)行循環(huán)體
b.當(dāng)執(zhí)行更新語(yǔ)句時(shí),如果循環(huán)條件的退出狀態(tài)永遠(yuǎn)為0,則for循環(huán)將永遠(yuǎn)執(zhí)行下去,從而產(chǎn)生死循環(huán)
c.Linux shell中不運(yùn)行使用非整數(shù)類型的數(shù)作為循環(huán)變量
d.如果循環(huán)體中的循環(huán)條件被忽略,則默認(rèn)的退出狀態(tài)為0
e.在類C風(fēng)格的for循環(huán)中,可以將三個(gè)語(yǔ)句全部忽略掉,下面是合法的for循環(huán)
#!/bin/bash
for((; ; ))
do
echo "hello world www.linuxidc.com"
done
總結(jié)
以上是生活随笔為你收集整理的linux命令行下写for语句,Linux下Shell的for循环语句示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle新增、删除索引以及主键修改
- 下一篇: linux启动jar后回到根目录,Spr