Linux Shell程序设计

http://blog.sina.com.cn/s/blog_74a7e56e0101alo8.html



在Linux操作系统中,shell非常适合于编写一些执行相对简单任务的小工具,因为它们更强调的是易于配置,易于维护和可移植性,而不是执行的效率。不仅可以通过shell执行命令,还可以编写shell程序。


(1)管道和重定向
ls -l > lsout.txt
将ls命令的输出保存到文件lsout.txt中。
ps >> lsout.txt
将ps命令的输出追加到lsout.txt的尾部。
ps | sort >> pssort.out
用管道连接进程,对ps命令的结果进行排序并追加的文件pssort.out的尾部。


(2)标准输出和标准错误输出的重定向
./main >log.txt 2>err.txt
将main进程的标准输出和标准错误分别定向到不同的文件中:
./main >log.txt 2>&1
将main进程的标准输出和标准错误都重定向到文件log.txt中,用>&操作符结合两个输出。


(3)grep命令(General Regular Expression Parser通用正则表达式解析器)
grep in words.txt
在文件words.txt中搜索字符串in,然后输出匹配的行
grep ef$ words.txt
在文件words.txt中查找以字母ef结尾的行,可见shell支持正则表达式。
grep -b fread /usr/include/*.h
在Linux头文件目录中查找fread函数的声明位置。


(4)其它常用Linux命令
nl命令为输入的每一行添加行号。
head命令用于打印文件或流的前十行。
tail命令用于打印文件或流的最后十行。
cat命令顺序打印文件的所有行。
tac命令逆序打印文件的所有行。
sort命令逐行对文件中的内容进行排序。
expand命令用于将输入制表符转换为空格。
unexpand命令用于将输入空格转换为制表符。
locate命令用于从数据库中查找文件名,locate apt-get -c统计查找到apt-get文件的数量。
netstat -p -tcp用于查询活动的TCP连接,计算机监听的端口。


(5)Shell编程的基本语法
#!/bin/sh


# shelltest
# This is my shell programming test file
# Written by TERRY-V


foo1() {
    echo "Example One"
    echo -n "Hi, Is it morning? Please answer yes or no: "
    read timeofday
    if [ "$timeofday" = "yes" ]; then
        echo "Good morning"
    elif [ "$timeofday" = "no" ]; then
        echo "Good afternoon"
    else
        echo "Sorry, $timeofday not recognized. Enter yes or no"
        exit 1
    fi
}


foo2() {
    echo "Example Two"
    for foo in "Hello!" 100 Oh
    do
        echo $foo
        sleep 1
    done
}


foo3() {
    echo "Example Three"
    echo -n "Enter password: "
    read trythis
    while [ "$trythis" != "secret" ]:
    do
        echo "Sorry, try again"
        read trythis
    done
}


foo4() {
    echo "Example Four"
    echo -n "Is it morning? Please answer yes or no: "
    read timeofday
    case "$timeofday" in
        yes | y | Yes | YES )
            printf "Good morning\n"
            echo "Up bright and early in the morning"
            return 1
            ;;
        n* | N* )
            echo "Good afternoon"
            return 2
            ;;
        * )
            echo "Sorry, answer not recognized"
            return 0
            ;;
    esac
}


echo "script starting......"
foo1
foo2
foo3
foo4
echo "script ending......"


exit 0


(6)dialog图形化工具
如果shell脚本只需要运行在Linux控制台上,则可以使用dialog工具命令,该命令使用文本模式的图形和色彩,提供图形化界面。以下是一个复杂的使用dialog工具的程序:
<1>首先,改程序通过一个简单的对话框告诉用户发生的事情,不需要获得返回值和任何用户输入。
#!/bin/sh
# Ask some questions and collect the answer
dialog --title "Questionnaire" --messagebox "Welcome to my simple survey" 9 18


<2>然后用一个简单的yes/no对话框询问用户是否要继续操作,如果不想继续操作,显示一个简单的信息框。
dialog --title "Conform" --yesno "Are you willing to take part?" 9 18
if [ $? != 0 ] ; then
    dialog --infobox "Thank you anyway!" 5 20
    sleep 2
    gdialog --clear
    exit 0
fi


<3>使用一个输入框来询问用户的姓名,重定向标准错误流2到临时文件_1.txt,然后将其放到变量Q_NAME中。
dialog --title "Questionnaire" --inputbox "Please enter your name" 9 30 2>_1.txt
Q_NAME=$(cat _1.txt)


<4>现在显示一个菜单,有4个不同的选项。再次重定向标准错误流到文件_1.txt,然后将其放到变量Q_MUSIC中。
dialog --menu "$Q_NAME, what music do you like most?" 15 30 4 1 "Classical" 2 "Jazz" 3 "Country" 4 "Other" 2>_1.txt
Q_MUSIC=$(cat _1.txt)


<5>对结果进行测试,最后清除对话框并退出程序。
if [ $Q_MUSIC = "1" ] ; then
    dialog --title "Like Classical" --msgbox "good choice!" 12 25
else
    dialog --title "Doesn't like Classic" --msgbox "Shame" 12 25
fi
sleep 2
dialog --clear
exit 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值