今天总结一下linux shell中逻辑关机表达方式。
逻辑与的表达:
1)、if [ $xxx=a -a $xx=b ]
注:-a表示and的意思
2)、if [ $xxx=a ] && [ $xx=b ]
eg:
#! /bin/bash
webapps_dir='/var/log/webapps'
webapps_owner=`ls -l /var/log|grep 'webapps$'|awk '{print $3}'`
webapps_group=`ls -l /var/log|grep 'webapps$'|awk '{print $4}'`
localhost_ip=`ifconfig |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "|head -1`
if [ -d ${webapps_dir} ]; then
#与的用法
if [ ${webapps_owner} = 'whtest' ] && [ ${webapps_group} = 'whtest' ]; then
exit 0
else
chown -R whtest:whtest ${webapps_dir}
echo "host_ip:${localhost_ip},webapps文件赋予whtest"
fi
else
mkdir -p ${webapps_dir}
chown -R whtest:whtest ${webapps_dir}
echo "host_ip:${localhost_ip},webapps文件已创建,且赋予whtest"
fi
逻辑或的表达:
1)、if [ $xxx=a -o $xx=b ]
注:-o表示or的意思
2)、if [ $xxx=a ] || [ $xx=b ]
eg:
#! /bin/bash
webapps_dir='/var/log/webapps'
webapps_owner=`ls -l /var/log|grep 'webapps$'|awk '{print $3}'`
webapps_group=`ls -l /var/log|grep 'webapps$'|awk '{print $4}'`
localhost_ip=`ifconfig |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "|head -1`
if [ -d ${webapps_dir} ]; then
#或的用法
if [ ${webapps_owner} = 'whtest' ] || [ ${webapps_group} = 'whtest' ]; then
exit 0
else
chown -R whtest:whtest ${webapps_dir}
echo "host_ip:${localhost_ip},webapps文件赋予whtest"
fi
else
mkdir -p ${webapps_dir}
chown -R whtest:whtest ${webapps_dir}
echo "host_ip:${localhost_ip},webapps文件已创建,且赋予whtest"
fi