如何用“-"声明变量名Linux bash脚本中的char(破折号) [英] how to declare variable name with "-" char (dash ) in linux bash script

查看:59
本文介绍了如何用“-"声明变量名Linux bash脚本中的char(破折号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了如下简单的脚本

#!/bin/bash

auth_type=""

SM_Read-only="Yes"
SM_write-only="No"

echo -e  ${SM_Read-only}
echo -e  ${SM_Write-only}

if [ "${SM_Read-only}" == "Yes" ] && [ "${SM_Write-only}" == "Yes" ] 
then
    auth_type="Read Write"
else
    auth_type="Read"
fi

echo -e $auth_type

当我执行它时,出现以下输出错误.

And when i execute it i got following output with errors.

./script.bash: line 5: SM_Read-only=Yes: command not found
./script.bash: line 6: SM_write-only=No: command not found
only
only
Read

有人知道用-"(破折号)声明变量的正确方法吗?

Any one know correct way to declare the variable with "-" (dash)?

从c代码获得响应并评估例如变量

have getting response from c code and evaluate the variables for example

RESP=`getValue SM_ Read-only ,Write-only 2>${ERR_DEV}`
RC=$?
eval "$RESP"

从上面的脚本

用代码编写我的c二进制 getValue 知道脚本想要只读 Write-only 并将值返回给脚本.在 eval $ RESP 期间导致原因错误,在我的脚本中,我通过以下方式访问变量

from above scripts code my c binary getValue know that script want Read-only and Write-only and return value to script.So during eval $RESP in cause error and in my script i access variable by

echo -e  ${SM_Read-only}
echo -e  ${SM_Write-only}

这也会导致错误.

推荐答案

重命名变量名称,如下所示:

Rename the variable name as follows:

SM_Read_only="Yes"
SM_write_only="No"

请不要在,了解如何设置适当的的问题.

Please, don't use - minus sign in variable names in bash, please refer to the answer, on how to set the proper variable name in bash.

但是,如果您基于其他输出生成代码,则可以简单地使用:

However if you generate the code, based on others output, you can simply process their output with sed:

RESP=$(getValue SM_ Read-rule,Write-rule 2>${ERR_DEV}|sed "s/-/_/g")
RC=$?
eval "$RESP"

这篇关于如何用“-"声明变量名Linux bash脚本中的char(破折号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆