shell脚本里常见的指令和函数

shell script可以执行cd指令改变当前目录

[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



   shell script下对当前路径的处理——$PWD, 但此值随cd指令而变,因此建议保存在另一个变量中

[root@nm ntshellhttp]# vi test.sh
#!/bin/bash

a="$PWD"
echo $a
cd /
ls $a


    每隔5秒(sleep 5)就做一些操作,如果用while语句的话,应该如何设置?

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



  Shell的 exit命令
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                       |
==============================================="



  更简单的菜单显示---------echo函数化

banner(){
echo "===============================================
|           ** unix script test             |
|           1 --- num 1                       |
|           2 --- num 2                       |
|           3 --- num 3                       |
|           4 --- num 4                       |
==============================================="
}

引用函数,显示菜单
banner



    输入y,n函数

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函数-----------输入回车继续,输入q退出程序,其他值pause

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



    用上面的函数构成的标准的输入script例子

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 ~]$



   一个开机自动启动HTTP和MYSQL的script

[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



   一个从文件中删除某行的shell函数rmline_from_file()
例子:从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 <<HELP
将多行文字传递给前面的command
一般用于显示帮助

cat <<HELP
...
...
HELP


   例2 一个简单的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,unix,脚本,command,input,cvs)