shell脚本里常见的指令和函数
2011-01-27 14:20
507 查看
shell script可以执行cd指令改变当前目录
shell script下对当前路径的处理——$PWD, 但此值随cd指令而变,因此建议保存在另一个变量中
每隔5秒(sleep 5)就做一些操作,如果用while语句的话,应该如何设置?
Shell的 exit命令
if
exit 1
else
exit 0 脚本退出并返回代码1
菜单显示,不必多个echo一行行显示,可以一整个echo
更简单的菜单显示---------echo函数化
输入y,n函数
pause函数-----------输入回车继续,输入q退出程序,其他值pause
用上面的函数构成的标准的输入script例子
一个开机自动启动HTTP和MYSQL的script
一个从文件中删除某行的shell函数rmline_from_file()
例子:从rc.local中删除含有 "root/bridge"的行
Command <<HELP
将多行文字传递给前面的command
一般用于显示帮助
例2 一个简单的help函数
[root@demo1 test]# vi test.sh cd /home touch go echo haha > go ls go cd /home/mac echo haha > aa |
[root@demo1 test]# sh test.sh go [root@demo1 test]# ls /home go guan iei mac [root@demo1 test]# cat /home/go haha [root@demo1 test]# cat ../aa haha |
[root@nm ntshellhttp]# vi test.sh #!/bin/bash a="$PWD" echo $a cd / ls $a |
echo "input num:" read num echo "input is $num" while test $num -lt 10 do num=$(($num+1)) echo "new num is $num" sleep 5 done |
[root@mac-home macg]# sh test.sh input num: 3 input is 3 new num is 4 new num is 5 new num is 6 new num is 7 new num is 8 new num is 9 |
if
exit 1
else
exit 0 脚本退出并返回代码1
菜单显示,不必多个echo一行行显示,可以一整个echo
echo "=============================================== | ** unix script test | | 1 --- num 1 | | 2 --- num 2 | | 3 --- num 3 | | 4 --- num 4 | ===============================================" |
banner(){ echo "=============================================== | ** unix script test | | 1 --- num 1 | | 2 --- num 2 | | 3 --- num 3 | | 4 --- num 4 | ===============================================" } 引用函数,显示菜单 banner |
getyn() { while echo "enter y/n :" do read yn case $yn in [Yy]) return 0 ;; yes) return 0 ;; YES) return 0 ;; [Nn]) return 1 ;; no) return 1;; NO) return 1;; * ) echo "only accept Y,y,N,n,YES,yes,NO,no";; esac done } |
pause() { while echo "Press <return> to proceed or type q to quit:" do read cmd case $cmd in [qQ]) exit 1;; 直接退到底,退出shell script "") break;; break跳出while语句 引号中间没有任何字符,表示是return *) continue;; continue继续下一循环 esac done |
while getyn command作为条件 do banner done |
[macg@mac-home ~]$ ./test.sh enter y/n : d only accept Y,y,N,n,YES,yes,NO,no enter y/n : y =============================================== | ** unix script test | | 1 --- num 1 | | 2 --- num 2 | | 3 --- num 3 | | 4 --- num 4 | =============================================== enter y/n : n 输入n,getyn函数返回1,1为假,跳出循环 [macg@mac-home ~]$ |
[root@mac-home macg]# vi server.sh : if [ -f /var/run/httpd.pid ] 如果存在/var/run/httpd.pid,就显示其内容 then cat /var/run/httpd.pid else /etc/init.d/httpd start 如果不存在/var/run/httpd.pid,就启动http fi if [ -f /var/run/mysqld/mysqld.pid ] then cat /var/run/mysqld/mysqld.pid else /etc/init.d/mysqld start fi |
[root@mac-home macg]# sh server.sh Starting httpd: [ OK ] Starting MySQL: [ OK ] [root@mac-home macg]# sh server.sh 12944 13044 |
例子:从rc.local中删除含有 "root/bridge"的行
[root@demo1 test]# vi rmline_from_file #!/bin/sh rmline_from_file() { [ -f temp.file ] && rm -f temp.file while read LINE 每次从$1的文件里读一行,在do里处理 do if echo $LINE|grep $2 >/dev/null 2>&1 then continue else echo $LINE >> temp.file fi done < $1 rm -f $1 mv temp.file $1 } |
rmline_from_file rc.local /root/bridge $1是要处理的文件名 $2是要删除的行含有的pattern(用于grep 出行) |
将多行文字传递给前面的command
一般用于显示帮助
cat <<HELP ... ... HELP |
[macg@machome ~]$ vi test.sh help() { cat <<HELP USAGE: xtitlebar [-h] "string_for_titelbar" OPTIONS: -h help text EXAMPLE: xtitlebar "cvs" HELP exit 0 } help |
[macg@machome ~]$ sh test.sh USAGE: xtitlebar [-h] "string_for_titelbar" OPTIONS: -h help text EXAMPLE: xtitlebar "cvs" |
相关文章推荐
- Shell脚本里常见的一些指令和函数
- 05- Shell脚本学习--函数
- shell脚本:创建函数并指定目录进行下载
- shell脚本帮助函数
- Java 执行Shell脚本指令
- Linux命令行和shell脚本编程-函数
- shell脚本中常见工具grep的使用
- shell脚本——函数的操作使用
- Linux命令行与shell脚本(9)--函数参数
- shell脚本的函数、加法的使用实例
- shell的一些常用的语句(if语句,变量,for、while、until语句、函数调用、脚本调用)
- Shell脚本(五)函数
- 使用FUNCNAME 和BASH_LINENO实现shell脚本中定位函数错误在代码中的位置
- SHELL 编程入门与提高(四)SHELL脚本中函数的应用
- shell脚本——练习几个脚本_循环和函数
- [转]Shell中脚本变量和函数变量的作用域
- shell脚本中常见的一些特殊符号和作用详解
- [置顶] 编写shell脚本以及利用函数实现批量安装Linux服务配置
- shell脚本中常见的一些特殊符号和作用详解
- shell脚本编程-函数