利用实例:
对照整数a是不是年夜于整数b:if[$a-gt$b]判别字符串a是不是为空:if[-z$a]
注重:
(1)在"["和"]"标记的摆布都留有空格(2)"="摆布都有空格
shell剧本s4.sh
viewplaincopytoclipboardprint?
#!/bin/bash
a=$1
b=$2
if[-z$a]||[-z$b]
then
echo"pleaseenter2no"
exit1
fi
if[$a-eq$b];then
echo"numbera=numberb"
elseif[$a-gt$b]
then
echo"numbera>numberb"
elif[$a-lt$b]
then
echo"numbera<numberb"
fi
fi
#!/bin/bash
a=$1
b=$2
if[-z$a]||[-z$b]
then
echo"pleaseenter2no"
exit1
fi
if[$a-eq$b];then
echo"numbera=numberb"
elseif[$a-gt$b]
then
echo"numbera>numberb"
elif[$a-lt$b]
then
echo"numbera<numberb"
fi
fi
判别
-e文件已存在-f文件是一般文件-s文件巨细不为零-d文件是一个目次
-r文件对以后用户能够读取-w文件对以后用户能够写进-x文件对以后用户能够实行
shell剧本s5.sh
viewplaincopytoclipboardprint?
#!/bin/sh
folder=/home
[-r"$folder"]&&echo"Canread$folder"
[-f"$folder"]||echo"thisisnotfile"
#!/bin/sh
folder=/home
[-r"$folder"]&&echo"Canread$folder"
[-f"$folder"]||echo"thisisnotfile"
shell剧本s6.sh
viewplaincopytoclipboardprint?
#!/bin/bash
DIR=$1
#ifthestringempty
if["$DIR"=""]
then
echo"usage:`basename$0`directorytocreate">&2
exit1
fi
echo"dir"$DIR
if[-d$DIR]
then
echo"Thedirectoryalreadyexist"
exit0
else
echo"Thedirectorydoesexist"
echo-n"Createisnow?[Y/N]:"
readcreate
if["$create"="y"]||["$create"="Y"]
then
echo"creatingnow"
if[mkdir$DIR]
DIR=""
fi
if["$DIR"=""]
then
echo"createdirectorysucess"
else
echo"createdirectoryerror"
fi
elif["$create"="n"]||["$create"="N"]
then
echo"doesnotcreatedirectory"
exit0
else
echo"Errorsorder"
exit1
fi
fi
#!/bin/bash
DIR=$1
#ifthestringempty
if["$DIR"=""]
then
echo"usage:`basename$0`directorytocreate">&2
exit1
fi
echo"dir"$DIR
if[-d$DIR]
then
echo"Thedirectoryalreadyexist"
exit0
else
echo"Thedirectorydoesexist"
echo-n"Createisnow?[Y/N]:"
readcreate
if["$create"="y"]||["$create"="Y"]
then
echo"creatingnow"
if[mkdir$DIR]
DIR=""
fi
if["$DIR"=""]
then
echo"createdirectorysucess"
else
echo"createdirectoryerror"
fi
elif["$create"="n"]||["$create"="N"]
then
echo"doesnotcreatedirectory"
exit0
else
echo"Errorsorder"
exit1
fi
fi
for轮回
for轮回布局与C言语中有所分歧,在bash中for轮回的基础布局式
forvarin
[list]
do
#codelock
done
个中$var是轮回把持变量,
[list]是var遍历的一个汇合,do/done对包括了轮回体。
别的,假如for和do写在统一行,必需在do后面加";"。
shell剧本s7.sh
viewplaincopytoclipboardprint?
#!/bin/bash
fordayinSunMonTueWedThuFriSat
do
echo$day
done
#!/bin/bash
fordayinSunMonTueWedThuFriSat
do
echo$day
done
shell剧本统计以后目次下的文件数
viewplaincopytoclipboardprint?
#!/bin/bash
counter=0
forfilesin*
do
counter=`expr$counter+1`
done
echo"Thereare$counterfilesin`pwd`weneedtoprocess"
#!/bin/bash
counter=0
forfilesin*
do
counter=`expr$counter+1`
done
echo"Thereare$counterfilesin`pwd`weneedtoprocess"
shell剧本将用户输出的数字按倒序的体例输入
viewplaincopytoclipboardprint?
#!/bin/bash
echo-n"Pleaswenternumber:"
readn
sd=0
rev=""
on=$n
echo"$n"
while[$n-gt0]
do
sd=$(($n%10))#getRemainder
n=$(($n/10))#getnextdigit
rev=$(echo$rev$sd)
done
echo"$oninareverseorder$rev"
#!/bin/bash
echo-n"Pleaswenternumber:"
readn
sd=0
rev=""
on=$n
echo"$n"
while[$n-gt0]
do
sd=$(($n%10))#getRemainder
n=$(($n/10))#getnextdigit
rev=$(echo$rev$sd)
done
echo"$oninareverseorder$rev"
until轮回
until轮回的基础布局
until[condition]
do
#codeblock
done
while和until的区分在于while是为真时实行,until是为假时实行。
shell剧本挪动一个文件,假如方针存在,监督该文件,直到文件被删除才挪动文件。
viewplaincopytoclipboardprint?
#!/bin/bash
if["$1"=""]||["$2"=""]
then
echo"Pleaseenterfilename"
exit1
fi
if[-e$2]
then
echo"Thefilealreadyexists"
until[!-f$2]
do
sleep1
done
fi
if[!`mv$1$2`]
then
echo"mvsucessful"
else
echo"mverror"
fi
#!/bin/bash
if["$1"=""]||["$2"=""]
then
echo"Pleaseenterfilename"
exit1
fi
if[-e$2]
then
echo"Thefilealreadyexists"
until[!-f$2]
do
sleep1
done
fi
if[!`mv$1$2`]
then
echo"mvsucessful"
else
echo"mverror"
fi