linux--shell--one

shell

shell结构:
1.#!指定执行脚本的shell
2.#注释行
3.命令和注释行

特殊变量:
$*这个程序的所有参数
$#这个程序的所有参数个数
$$这个程序的PID
$!执行上一个后天命令的PID
$?执行上一个命令的返回值

shell 命令
1.read命令:从键盘读入数据,赋值给变量。
sh -x shellname :可以观察脚本执行的内容。
2.expr命令:
对整数型变量进行算术运算

自动备份脚本(from lamp brother):
#!/bin/sh
# back file by date

DATE=`date +%y%m%d`
/bin/tar -cf /back/$1.$DATE.tar $1 > /dev/null 2>> /backup/$1.bak.log
/bin/gzip /back/$1.$DATE.tar

if [ $? -eq 0 ]
then
echo "$1 $DATE backup sucessfully!" >> /backup/$1.back.log
else
echo "ERRORS :failure $1 $DATE backup!" >> /backup/$1.back.log
if

#crontab -e
# 0 3 * * 2,5 script

test 变量测试语句:
1.用于测试变量是否相等、是否为空、文件类型等。
2.test 测试条件
测试范围:整数、字符串、文件。
3.变量测试语句可用[]进行简化,如:
test -d $1 等价于[-d $1]

测试test
#!/bin/sh
#"if ...else" usage
#using this program to show your system's service

echo "Now,the web services of the linux system will be detect ..."
echo

#Detect www service
web=`/user/bin/pgrep httpd`
if [ "$web" != "" ]
then
echo "This service is running."
else
echo "This service is not running,and it will be start"
/etc/rc.d/init.d/httpd start
fi

流程控制:
1.if..else..fi
2.if...elif..else..fi
3.for in
do
done

posted @ 2012-03-18 22:55  Alex-Zeng  阅读(201)  评论(0编辑  收藏  举报