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