shell 中的與、或表達式


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM