日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Linux shell 学习笔记(9)— 循环语句(for、while)以及更改字段分隔符

發(fā)布時間:2023/11/28 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux shell 学习笔记(9)— 循环语句(for、while)以及更改字段分隔符 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. for 語句

bash shell 中 for 命令的基本格式如以下,$var 變量包含著這次迭代對應(yīng)的當(dāng)前列表項中的值。

for var in list
docommands
done

也可以將 do 語句和 for 語句放在同一行,但必須用分號將其同列表中的值分開:for var in list; do

1.1 讀取列表中的值

for 命令最基本的用法就是遍歷 for 命令自身所定義的一系列值。

$ cat test1
#!/bin/bash
# basic for command
for test in Alabama Alaska Arizona Arkansas California Colorado
doecho The next state is $test
done
$ ./test1
The next state is Alabama
The next state is Alaska
The next state is Arizona
The next state is Arkansas
The next state is California
The next state is Colorado
$

1.2 讀取列中的復(fù)雜值

當(dāng)列表中包含單引號時,需要使用轉(zhuǎn)義字符或者雙引號來定義用到單引號的值。

$ cat test2
#!/bin/bash
# another example of how not to use the for command
for test in I don\'t know if "this'll" work
doecho "word:$test"
done
$ ./test2
word:I
word:don't
word:know
word:if
word:this'll
word:work
$

記住,for循環(huán)假定每個值都是用空格分割的。

for 命令用空格來劃分列表中的每個值。如果在單獨的數(shù)據(jù)值中有空格,就必須用雙引號將這些值圈起來。

$ cat test3
#!/bin/bash
# an example of how to properly define values
for test in Nevada "New Hampshire" "New Mexico" "New York"
doecho "Now going to $test"
done
$ ./test3
Now going to Nevada
Now going to New Hampshire
Now going to New Mexico
Now going to New York
$

1.3 從變量讀取列表

將一系列值都集中存儲在了一個變量中,然后需要遍歷變量中的整個列表。

$ cat test4
#!/bin/bash
# using a variable to hold the list
list="Alabama Alaska Arizona Arkansas Colorado"
list=$list" Connecticut"	# 向 list 變量中添加文本
for state in $list
doecho "Have you ever visited $state?"
done
$ ./test4
Have you ever visited Alabama?
Have you ever visited Alaska?
Have you ever visited Arizona?
Have you ever visited Arkansas?
Have you ever visited Colorado?
Have you ever visited Connecticut?
$

注意,代碼還是用了另一個賦值語句向 $list 變量包含的已有列表中添加(或者說是拼接)了一個值。這是向變量中存儲的已有文本字符串尾部添加文本的一個常用方法。

1.4 從命令讀取值

可以用命令替換來執(zhí)行任何能產(chǎn)生輸出的命令,然后在 for 命令中使用該命令的輸出。

$ cat test5
#!/bin/bash
# reading values from a file
file="states"
for state in $(cat $file)
doecho "Visit beautiful $state"
done
$ cat states
Alabama
Alaska
Arizona
Arkansas
Colorado
Connecticut
Delaware
Florida
Georgia
$ ./test5
Visit beautiful Alabama
Visit beautiful Alaska
Visit beautiful Arizona
Visit beautiful Arkansas
Visit beautiful Colorado
Visit beautiful Connecticut
Visit beautiful Delaware
Visit beautiful Florida
Visit beautiful Georgia
$

1.5 使用通配符讀取目錄

可以用 for 命令來自動遍歷目錄中的文件。進行此操作時,必須在文件名或路徑名中使用通配符。它會強制 shell 使用文件擴展匹配。文件擴展匹配是生成匹配指定通配符的文件名或路徑名的過程。

如果不知道所有的文件名,這個特性在處理目錄中的文件時就非常好用。

$ cat test6
#!/bin/bash
# iterate through all the files in a directory
for file in /home/rich/test/*
doif [ -d "$file" ]	# 如果不用雙引號包含,遇到目錄和文件名包含空格時會失敗thenecho "$file is a directory"elif [ -f "$file" ]thenecho "$file is a file"fi
done
$ ./test6
/home/rich/test/dir1 is a directory
/home/rich/test/myprog.c is a file
/home/rich/test/myprog is a file
/home/rich/test/myscript is a file
/home/rich/test/newdir is a directory
/home/rich/test/newfile is a file
/home/rich/test/newfile2 is a file
/home/rich/test/testdir is a directory
/home/rich/test/testing is a file
/home/rich/test/testprog is a file
/home/rich/test/testprog.c is a file
$

2. C 語言風(fēng)格的 for 命令

待補充

3. while 命令

3.1 while 的基本格式

while test command
doother commands
done

while 命令的關(guān)鍵在于所指定的 test command 的退出狀態(tài)碼必須隨著循環(huán)中運行的命令而
改變。如果退出狀態(tài)碼不發(fā)生變化, while 循環(huán)就將一直不停地進行下去。

最常見的 test command 的用法是用方括號來檢查循環(huán)命令中用到的 shell 變量的值。

$ cat test10
#!/bin/bash
# while command test
var1=10
while [ $var1 -gt 0 ]
doecho $var1var1=$[ $var1 - 1 ]
done
$ ./test10
10
9
8
7
6
5
4
3
2
1
$

3.2 until 命令

until 命令和 while 命令工作的方式完全相反。until 命令要求你指定一個通常返回非零退出狀態(tài)碼的測試命令。只有測試命令的退出狀態(tài)碼不為 0,bash shell 才會執(zhí)行循環(huán)中列出的命令。一旦測試命令返回了退出狀態(tài)碼 0,循環(huán)就結(jié)束了。

until test commands
doother commands
done
$ cat test12
#!/bin/bash
# using the until command
var1=100
until [ $var1 -eq 0 ]
doecho $var1var1=$[ $var1 - 25 ]
done
$ ./test12
100
75
50
25
$

4. 控制循環(huán)

4.1 break 命令

break 命令是退出循環(huán)的一個簡單方法??梢杂?break 命令來退出任意類型的循環(huán),包括 while 和until 循環(huán)。

$ cat test17
#!/bin/bash
# breaking out of a for loop
for var1 in 1 2 3 4 5 6 7 8 9 10
doif [ $var1 -eq 5 ]thenbreakfiecho "Iteration number: $var1"
done
echo "The for loop is completed"
$ ./test17
Iteration number: 1
Iteration number: 2
Iteration number: 3
Iteration number: 4
The for loop is completed
$

4.2 continue 命令

continue 命令可以提前中止某次循環(huán)中的命令,但并不會完全終止整個循環(huán)。

5. 處理循環(huán)的輸出

可以對循環(huán)的輸出使用管道或進行重定向。這可以通過在 done 命令之后添加一個處理命令來實現(xiàn)。

for file in /home/rich/*
doif [ -d "$file" ]thenecho "$file is a directory"elifecho "$file is a file"fi
done > output.txt

shell 會將 for 命令的結(jié)果重定向到文件 output.txt 中,而不是顯示在屏幕上。

6. 更改字段分隔符

內(nèi)部字段分隔符(internal field separator),IFS 環(huán)境變量定義了 bash shell 用作字段分隔符的一系列字符。

默認情況下,bash shell 會將下列字符當(dāng)作字段分隔符:

  • 空格
  • 制表符
  • 換行符

在處理代碼量較大的腳本時,可能在一個地方需要修改 IFS 的值,然后忽略這次修改,在腳本的其他地方繼續(xù)沿用 IFS 的默認值。一個可參考的安全實踐是在改變 IFS 之前保存原來的 IFS 值,之后再恢復(fù)它。

這種技術(shù)可以這樣實現(xiàn):

IFS.OLD=$IFS
IFS=$'\n'

<在代碼中使用新的IFS值>

IFS=$IFS.OLD

這就保證了在腳本的后續(xù)操作中使用的是 IFS 的默認值。如果要指定多個IFS字符,只要將它們在賦值行串起來就行。

IFS=$'\n':;"

這個賦值會將換行符、冒號、分號和雙引號作為字段分隔符。

總結(jié)

以上是生活随笔為你收集整理的Linux shell 学习笔记(9)— 循环语句(for、while)以及更改字段分隔符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。