2015年1月13日《Linux程序设计》学习笔记

时间:2021-12-27 01:34:18
case:例子:#!/bin/sh
echo "Is it morning? Please answer yes or no"
read timeofday

case "$timeofday" in
    yes | y | Yes | YES)
           echo "Good Morning"
           echo "Up bright and early this moring"
           ;;
    [nN]*)
           echo "Good Afternoon"
           ;;
    *)
           echo "Sorry answer not recognized"
           echo "Please answer yes or no"
           exit 1
           ;;
esac

exit 0
在case结构的模式中使用如*这样的通配符时要小心,因为case将使用第一个匹配的模式,即使后续的模式有更加精确的匹配。 yes | y | Yes | YES)语句可换为 [yY] | [yY][eE][sS])
命令列表and列表:statement1 && statement2 && statement3 && ...从左开始执行。如果&&前面的一条语句为true,那后面的才会执行。例子中可以说明这一点#!/bin/sh
touch file_onerm -f file_two
if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo " there"then   echo "in if"else   echo "in else"fi
exit 0输出为:helloin else
OR命令表statement1 || statment2 || statement3 || ...从左边开始执行直到碰到某个语句返回true才停止。例子:#!/bin/shrm -f file_oneif [ -f file_one ] || echo "hello" || echo "there"then    echo "in if"else    echo "in else"fi
exit 0输出为:helloin if
语句块:如果想再某些允许使用单个语句的地方(比如在AND或者OR列表中)使用多条语句,可以把他们括在花括号{}中来构造一个语句块。例如get_confirm &&{    grep -v "$cdcatnum" $tracks_file >$temp_file    cat $temp_file > $tracks_file    echo    add_record_tracks}cat 命令是连接多个文件然后输出例如:cat -n file1 >file2就是将file1中的内容按行编号,输出到file2中。-------------------------page39------------------------------------2.6.5 over----------------------