shell(13): Shell脚本,if语句使用方双括号

shell(13): Shell脚本,if语句使用方双括号

今天在测试shell脚本时,发现永远都是true,值不对时也不会为false,最后发现是判断语句写错了。整理下以下情况:

Shell脚本的例子1:

#!/bin/bash



echo "----------------Java调用shell脚本执行客户端加解密任务,开始--------------------------"

pwdDir=`pwd`

cd $pwdDir

echo "当前工程目录:"$pwdDir



#查找数据库文件并读取数据

db_properties=`find $pwdDir/target/classes -name "db-java.properties"`

if [ -f "$db_properties" ];then

    #获取数据库类型

    dbtype=$(cat "$db_properties" | grep "DB_TYPE_ONE" | awk -F '=' '{print $2}')

    if [ "$dbtype"=="oracle" ]; then

      user=$(cat "$db_properties" | grep "ORACLE_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "ORACLE_SECRET" | awk -F '=' '{print $2}')

elif [ "$dbtype"=="postgresql" ]; then

      user=$(cat "$db_properties" | grep "POSTGRES_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "POSTGRES_SECRET" | awk -F '=' '{print $2}')

    elif [ "$dbtype"=="vertica" ]; then

      user=$(cat "$db_properties" | grep "VERTICA_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "VERTICA_SECRET" | awk -F '=' '{print $2}')

else

  user=$(cat "$db_properties" | grep "MYSQL_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "MYSQL_SECRET" | awk -F '=' '{print $2}')

    fi



    echo $user

echo $password

fi

echo "----------------Java调用shell脚本执行客户端加解密任务,结束--------------------------"

执行结果:

发现永远取的是oracle的值

if [ "$dbtype"=="oracle" ]; then

      user=$(cat "$db_properties" | grep "ORACLE_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "ORACLE_SECRET" | awk -F '=' '{print $2}')

原因:当dbtype=vertica时

执行if [ "$dbtype"=="oracle" ]; then

永远都是真,那么就直接取这个值了

解决方法:一、改成if [ $dbtype == "oracle" ]; then

if [ $dbtype == "oracle" ]; then才能为false

解决方法:二:方双括号(此种更好些)

if [[ $dbtype == "oracle" ]]; then

shell编码规则:

放双括号说明:

 (1) 双方括号提供了字符串比较的高级特性。

(2) 括号中可以定义一些正则表达式来匹配字符串

(3) 注意不是所有的shell都支持双方括号!

双括号中常用的运算符

特点:

1、在双括号结构中,所有表达式可以像c语言一样,如:a++,b--等。

2、在双括号结构中,所有变量可以不加入:“$”符号前缀。

3、双括号可以进行逻辑运算,四则运算

4、双括号结构 扩展了for,while,if条件测试运算

5、支持多个表达式运算,各个表达式之间用“,”分开

最后正确例子:

#!/bin/bash

echo "----------------Java调用shell脚本执行客户端加解密任务,开始--------------------------"

pwdDir=`pwd`

cd $pwdDir

echo "当前工程目录:"$pwdDir



#查找数据库文件并读取数据

db_properties=`find $pwdDir/target/classes -name "db-java.properties"`

if [ -f "$db_properties" ];then

    #获取数据库类型

    dbtype=$(cat "$db_properties" | grep "DB_TYPE_ONE" | awk -F '=' '{print $2}')

    if [[ $dbtype == "oracle" ]]; then

      user=$(cat "$db_properties" | grep "ORACLE_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "ORACLE_SECRET" | awk -F '=' '{print $2}')

elif [[ $dbtype == "postgresql" ]]; then

      user=$(cat "$db_properties" | grep "POSTGRES_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "POSTGRES_SECRET" | awk -F '=' '{print $2}')

    elif [[ $dbtype == "vertica" ]]; then

      user=$(cat "$db_properties" | grep "VERTICA_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "VERTICA_SECRET" | awk -F '=' '{print $2}')

else

  user=$(cat "$db_properties" | grep "MYSQL_USER" | awk -F '=' '{print $2}')

      password=$(cat "$db_properties" | grep "MYSQL_SECRET" | awk -F '=' '{print $2}')

    fi



    echo $user

echo $password

fi

echo "----------------Java调用shell脚本执行客户端加解密任务,结束--------------------------"

参考:

shell 单括号与双中括号的区别 - 如尘如水 - 博客园

参考:Shell中的括号、双括号、方括号和双方括号

https://www.jianshu.com/p/3e1eaaa3fee8

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁宁可可

您的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值