while [ condition ]
do
程式段落
done
until [ condition ]
do
程式段落
done
[root@linux scripts]# vi sh12.sh
#!/bin/bash
# Program:
# Use loop to try find your input.
# History:
# 2005/08/29 csie First release
while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
[root@linux scripts]# vi sh12-2.sh
#!/bin/bash
# Program:
# Use loop to try find your input.
# History:
# 2005/08/29 csie First release
until [ "$yn" == "yes" ] || [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
1+2+3+....+100:
[root@linux scripts]# vi sh13.sh
#!/bin/bash
# Program:
# Try to use loop to calculate the result "1+2+3...+100"
# History:
# 2005/08/29 csie First release
s=0
i=0
while [ "$i" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
for (( 初始值; 限制值; 執行步階 ))
do
程式段
done
i=1 設定;
i<=100;
[root@linux scripts]# vi sh14.sh
#!/bin/bash
# Program:
# Try do calculate 1+2+....+100
# History:
# 2005/08/29 csie First release
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
for var in con1 con2 con3 ...
do
程式段
done
第一次迴圈時, $var 的內容為 con1 ;
第二次迴圈時, $var 的內容為 con2 ;
第三次迴圈時, $var 的內容為 con3 ;
....
[root@linux scripts]# vi sh15.sh
#!/bin/bash
# Program:
# Using for .... loop to print 3 animal
# History:
# 2005/08/29 csie First release
for animal in dog cat elephant
do
echo "There are ""$animal""s...."
done
[root@linux scripts]# vi sh16.sh
#!/bin/bash
# Program:
# let user input a directory and find the whole file's permission.
# History:
# 2005/08/29 csie First release
# 1. 先看目錄是否存在?
read -p "Please input a directory: " dir
if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi
# 2. 開始測試檔案
filelist=`ls $dir`
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done
while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
until [ "$yn" == "yes" ] || [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program: " yn
done
s=0
i=0
while [ "$i" != "100" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is ==> $s"
for animal in dog cat elephant
do
echo "There are ""$animal""s...."
done
read -p "Please input a directory: " dir
if [ "$dir" == "" ] || [ ! -d "$dir" ]; then
echo "The $dir is NOT exist in your system."
exit 1
fi
filelist=`ls $dir`
for filename in $filelist
do
perm=""
test -r "$dir/$filename" && perm="$perm readable"
test -w "$dir/$filename" && perm="$perm writable"
test -x "$dir/$filename" && perm="$perm executable"
echo "The file $dir/$filename's permission is $perm "
done
2017-06-14