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"
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