bash编程入门


1. 基本操作
1)第一行放置头格式说明
#!/bin/bash
2)写完shell脚本后,设置脚本执行权限
chmod +x filename
3)执行脚本
sh filename
4)shell自动化脚本一般放在~/script/目录下


2. 写一个简单的脚本,自动收集Apache日志
#!/bin/bash
if [[ -a /var/log/httpd/access_log ]];then
        mv /var/log/httpd/access_log /var/tmp/access_log`date +%Y%m%d%H%M%S`
        echo "Log collected!"
else
        echo "Log can't be collected. Log doesn't exist!"
fi

/etc/init.d/httpd restart > /dev/null 2>&1


3.彩色shell输出
echo -e "\033[前景色号m输出文本\033[0m"
echo -e "\033[前景色号;背景色号m输出文本\033[0m"
注意:要记得-e开关,[0m是为了还原色彩
如:
echo -e "\033[32mLog collected!\033[0m"
echo -e "\033[31mLog can't be collected. Log doesn't exist!\033[0m"

你可能感兴趣的:(shell,脚本,bash)