[转] 8. Bourne Shell及shell编程
H缦滤荆?
$ . .profile
(csh相对于.命令的是source命令).
(7)使用And/Or结构进行有条件的命令执行
<1> And , 仅当第一个命令成功时才有执行后一个命令,如同在逻辑与表达式中如果前面的
结果为真时才有必要继续运算,否则结果肯定为假。
格式如下:
command1 && command2
例:rm $TEMPDIR/* && echo "File successfully removed"
等价于
if rm $TEMPDIR/*
then
echo "File successfully removed"
fi
<2>Or, 与AND相反,仅当前一个命令执行出错时才执行后一条命令
例: rm $TEMPDIR/* || echo "File not removed"
等价与:
if rm $TEMPDIR/*
then
command
else
echo "File not removed"
fi
<3> 混合命令条件执行
command1 && command2 && command3
当command1, command2成功时才执行command3
command1 && command2 || comamnd3
仅当command1成功,command2失败时才执行command3
当然可以根据自己的需要进行多种条件命令的组合,在此不多讲述。
(8) 使用getopts命令读取unix格式选项
UNIX格式选项指如下格式的命令行参数:
command -options parameters
使用格式:
getopts option_string variable
具体使用方法请参考getopts的在线文档(man getopts).
示例如下:
#newdate
if [ $# -lt 1 ]
then
date
else
while getopts mdyDHMSTjJwahr OPTION
do
case $OPTION
in
m) date '+%m ';; # Month of Year
d) date '+%d ';; # Day of Month
y) date '+%y ';; # Year
D) date '+%D ';; # MM/DD/YY
H) date '+%H ';; # Hour
M) date '+%M ';; # Minute
S) date '+%S ';; # Second
T) date '+%T ';; # HH:MM:SS
j) date '+%j ';; # day of year
J) date '+%y%j ';;# 5 digit Julian date
w) date '+%w ';; # Day of the Week
a) date '+%a ';; # Day abbreviation
h) date '+%h ';; # Month abbreviation
r) date '+%r ';; # AM-PM time
?) echo "Invalid option $OPTION";;
esac
done
fi
$ newdate -J
94031
$ newdate -a -h -d
Mon
Jan
31
$ newdate -ahd
Mon
Jan
31
$
示例程序:复制程序
# Syntax: duplicate [-c integer] [-v] filename
# where integer is the number of duplicate copies
# and -v is the verbose option
COPIES=1
VERBOSE=N
while getopts vc: OPTION
do
case $OPTION
in
c) COPIES=$OPTARG;;
v) VERBOSE=Y;;
?) echo "Illegal Option"
exit 1;;
esac
done
if [ $OPTIND -gt $# ]
then
echo "No file name specified"
exit 2
fi
shift `expr $OPTIND -1`
FILE=$1
COPY=0
while [ $COPIES -gt $COPY ]
do
COPY=`expr $COPY + 1`
cp $FILE ${FILE}${COPY}
if [ VERBOSE = Y ]
then
echo ${FILE}${COPY}
fi
done
$ duplicate -v fileA
fileA1
$ duplicate -c 3 -v fileB
fileB1
fileB2
fileB3
4.还有问题请来论坛寻求帮助:http://www.xxlinux.com/bbs/
$ . .profile
(csh相对于.命令的是source命令).
(7)使用And/Or结构进行有条件的命令执行
<1> And , 仅当第一个命令成功时才有执行后一个命令,如同在逻辑与表达式中如果前面的
结果为真时才有必要继续运算,否则结果肯定为假。
格式如下:
command1 && command2
例:rm $TEMPDIR/* && echo "File successfully removed"
等价于
if rm $TEMPDIR/*
then
echo "File successfully removed"
fi
<2>Or, 与AND相反,仅当前一个命令执行出错时才执行后一条命令
例: rm $TEMPDIR/* || echo "File not removed"
等价与:
if rm $TEMPDIR/*
then
command
else
echo "File not removed"
fi
<3> 混合命令条件执行
command1 && command2 && command3
当command1, command2成功时才执行command3
command1 && command2 || comamnd3
仅当command1成功,command2失败时才执行command3
当然可以根据自己的需要进行多种条件命令的组合,在此不多讲述。
(8) 使用getopts命令读取unix格式选项
UNIX格式选项指如下格式的命令行参数:
command -options parameters
使用格式:
getopts option_string variable
具体使用方法请参考getopts的在线文档(man getopts).
示例如下:
#newdate
if [ $# -lt 1 ]
then
date
else
while getopts mdyDHMSTjJwahr OPTION
do
case $OPTION
in
m) date '+%m ';; # Month of Year
d) date '+%d ';; # Day of Month
y) date '+%y ';; # Year
D) date '+%D ';; # MM/DD/YY
H) date '+%H ';; # Hour
M) date '+%M ';; # Minute
S) date '+%S ';; # Second
T) date '+%T ';; # HH:MM:SS
j) date '+%j ';; # day of year
J) date '+%y%j ';;# 5 digit Julian date
w) date '+%w ';; # Day of the Week
a) date '+%a ';; # Day abbreviation
h) date '+%h ';; # Month abbreviation
r) date '+%r ';; # AM-PM time
?) echo "Invalid option $OPTION";;
esac
done
fi
$ newdate -J
94031
$ newdate -a -h -d
Mon
Jan
31
$ newdate -ahd
Mon
Jan
31
$
示例程序:复制程序
# Syntax: duplicate [-c integer] [-v] filename
# where integer is the number of duplicate copies
# and -v is the verbose option
COPIES=1
VERBOSE=N
while getopts vc: OPTION
do
case $OPTION
in
c) COPIES=$OPTARG;;
v) VERBOSE=Y;;
?) echo "Illegal Option"
exit 1;;
esac
done
if [ $OPTIND -gt $# ]
then
echo "No file name specified"
exit 2
fi
shift `expr $OPTIND -1`
FILE=$1
COPY=0
while [ $COPIES -gt $COPY ]
do
COPY=`expr $COPY + 1`
cp $FILE ${FILE}${COPY}
if [ VERBOSE = Y ]
then
echo ${FILE}${COPY}
fi
done
$ duplicate -v fileA
fileA1
$ duplicate -c 3 -v fileB
fileB1
fileB2
fileB3
4.还有问题请来论坛寻求帮助:http://www.xxlinux.com/bbs/
浙公网安备 33010602011771号