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-Vfoo1() { 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……"foo1foo2foo3foo4echo "script ending……"exit 0(6)dialog图形化工具如果shell脚本只需要运行在Linux控制台上,则可以使用dialog工具命令,该命令使用文本模式的图形和色彩,提供图形化界面。以下是一个复杂的使用dialog工具的程序:<1>首先,改程序通过一个简单的对话框告诉用户发生的事情,不需要获得返回值和任何用户输入。#!/bin/sh# Ask some questions and collect the answerdialog –title "Questionnaire" –messagebox "Welcome to my simple survey" 9 18<2>然后用一个简单的yes/no对话框询问用户是否要继续操作,如果不想继续操作,显示一个简单的信息框。dialog –title "Conform" –yesno "Are you willing to take part?" 9 18if [ $? != 0 ] ; then dialog –infobox "Thank you anyway!" 5 20 sleep 2 gdialog –clear exit 0fi<3>使用一个输入框来询问用户的姓名,重定向标准错误流2到临时文件_1.txt,然后将其放到变量Q_NAME中。dialog –title "Questionnaire" –inputbox "Please enter your name" 9 30 2>_1.txtQ_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.txtQ_MUSIC=$(cat _1.txt)<5>对结果进行测试,最后清除对话框并退出程序。if [ $Q_MUSIC = "1" ] ; then dialog –title "Like Classical" –msgbox "good choice!" 12 25else dialog –title "Doesn’t like Classic" –msgbox "Shame" 12 25fisleep 2dialog –clearexit 0

走走停停,不要害怕错过什么,

Linux Shell程序设计

相关文章:

你感兴趣的文章:

标签云: