今天總結一下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