1.循环语句
#!/bin/ksh
n=1
while [[ n -lt 5 ]]
do
echo $n;
let n=n+1
done
输出1至5,也可以改写为如下,效果一样.
#!/bin/ksh
n=1
while (( n < 5))
do
echo $n;
let n=n+1
done
其中,let n=n+1可以用((n+=1))代替.
2.if-elif-else-fi语句
例1:
#!/bin/ksh
echo please enter the file name and press enter
read file
if [ -f "$file" ]
then
echo $file is a regular file
elif [ -d "$file"]
then echo $file is a dirctory
fi
执行脚本,提示输入文件名,回车.
例2:
#!/bin/ksh
file="$1"
if [ -f "$file" ]
then
echo $file is a regular file
elif [ -d "$file"]
then echo $file is a dirctory
fi
在执行脚本的时候跟上待检测的文件名.
3. for语句
例3:
#more test5.sh
#!/bin/ksh
for varfile in *.sh
do
echo $varfile
done
显示当前路径下以.sh结尾的文件.
执行跟踪以set -x 打开,以set +x 关闭跟踪功能.
也可以以sh -x test5.sh 打开跟踪执行,这会使得SHELL显示每个被执行到的命令,并在前面加"+"号,紧跟着一个空格(此符号可以通过修改PS4变量来修改).执行后显示如下:
#sh -x test5.sh
+ echo test1.sh
test1.sh
+ echo test2.sh
test2.sh
+ echo test3.sh
test3.sh
显示当前路径下有3个以.sh结尾的文件.
4.read 语句
例4:
#!/bin/ksh
while IFS=: read user pass uid gid fullname homedir shell
do
echo "$user - $uid"
done < passwd
执行后输出如下:
root - 0
daemon - 1
bin - 2
sys - 3
adm - 4
uucp - 5
guest - 100
nobody - 4294967294
lpd - 9
lp - 11
invscout - 6
snapp - 200
nuucp - 7
ipsec - 201
esaadmin - 8
oracle - 301
weblogic - 501
阅读(1786) | 评论(0) | 转发(0) |