shell編程—for循環

shell循環

shell循環的分類

1、for

2、while

3、until

for循環結構

for 變量 in 列表; do 
    循環體
done 

1、求1加到100的和

#!/bin/bash
# sum of 1 to 100

Sum=0
for i in {1..100};do
        Sum=$(($Sum+$i))
done
echo "Sum is $Sum"

shell編程—for循環

2、依次向/etc/passwd中的每個用戶問好,並顯示對方的shell,例如:
Hello,root,your shell: /bin/bash

#!/bin/bash
#

UserNum=`wc -l /etc/passwd | cut -d' ' -f1`

for i in `seq 1 $UserNum`; do
        UserName=`head -$i /etc/passwd | tail -1 | cut -d':' -f1`
        UserShell=`head -$i /etc/passwd| tail -1 |cut -d':' -f7`
        echo "Hello, $UserName, your shell: $UserShell"
done

shell編程—for循環

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章