zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    [COCI2013]DLAKAVAC
    [TJOI2013]最长上升子序列
    AGC040E Prefix Suffix Addition
    AGC010E Rearranging
    AGC021F Trinity
    AGC002F Leftmost Ball
    JOISC2019D ふたつのアンテナ
    LOJ6210 「美团 CodeM 决赛」tree
    Luogu P3781 [SDOI2017]切树游戏
    Problem. M
  • 原文地址:https://www.cnblogs.com/zhangqigao/p/6215388.html
Copyright © 2011-2022 走看看