1.以清理日志为范例,|| 命令 和 && 命令
[root@mysql1 shell]# cat clean_log.sh
#!/bin/bash
#清除日志脚本,版本 2
LOG_DIR=/var/log
ROOT_UID=0 # $UID 为 0 的时候,用户才具有 root 用户的权限
#要使用 root 用户来运行。
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit 1
fi
cd $LOG_DIR || {
echo "Cannot change to necessary directory." >&2
exit 1
}
cat /dev/null > messages && echo "Logs cleaned up."
exit 0
2.执行
[root@mysql1 shell]# sh clean_log.sh
Logs cleaned up.
[root@mysql1 shell]#
[root@mysql1 shell]# ll /var/log/messages
-rw------- 1 root root 0 Aug 10 16:22 /var/log/messages