简体   繁体   English

如何访问 Shell 脚本中的变量

[英]How to Access a variable in a Shell script

I'm currently stuck on how to do the following:我目前坚持如何执行以下操作:

I have a settings file that looks like this:我有一个如下所示的settings文件:

USER_ID=12
ROLE=admin
STARTED=10/20/2010
...

I need to access the role and map the role to one of the variables in the script below.我需要访问角色和 map 角色到下面脚本中的变量之一。 After I will use that variable to call open a doc with the correct name.在我将使用该变量调用打开具有正确名称的文档之后。

test.sh测试.sh

#!/bin/sh

ADMIN=master_doc
STAFF=staff_doc
GUEST=welcome_doc    

echo "accessing role type"
cd /setting

#open `settings` file to access role?
#call correct service
#chmod 555 master_doc.service 

Is there a way to interpolate strings using bash like there is in javascript?有没有办法像 javascript 那样使用 bash 插入字符串? Also, I'm guessing I would need to traverse through the settings file to access role?另外,我猜我需要遍历设置文件才能访问角色?

With bash and grep and assuming that the settings file has exactly one line beginning with ROLE= :使用bashgrep并假设settings文件恰好有一行以ROLE=开头:

#!/bin/bash

admin=master_doc
staff=staff_doc
guest=welcome_doc

cd /setting || exit
role=$(grep '^ROLE=' settings)
role=${role#*=}
echo chmod 555 "${!role}.service"

Drop the echo after making sure it works as intended.在确保它按预期工作后放下echo
Look into Shell Parameter Expansion for indirect expansion .查看Shell 参数扩展以进行indirect expansion

From what I understand, you want to get the variables from settings , use $role as an indirect reference to $admin , ie master_doc , then turn that into a string, master_doc.service .据我了解,您想从settings获取变量,使用$role作为对$admin的间接引用,即master_doc ,然后将其转换为字符串master_doc.service

Firstly, instead of indirection , I recommend an associative array since it's cleaner.首先,我推荐使用 关联数组而不是间接数组,因为它更简洁。

You can use source to get variables from another file , as well as functions and other stuff.您可以使用source从另一个文件中获取变量,以及函数和其他内容。

Lastly, to dereference a variable, you need to use the dollar sign, like $role .最后,要取消引用变量,您需要使用美元符号,例如$role Variable references are expanded inside double-quotes, so that's sort of the equivalent of string interpolation.变量引用在双引号内展开,所以这相当于字符串插值。

#!/bin/bash

# Associative array with doc names
declare -A docs=(
    [admin]=master_doc
    [staff]=staff_doc
    [guest]=welcome_doc
) 

echo "accessing role type"
cd setting || exit

source settings  # Import variables 
true "${ROLE?}"  # Exit if unset

echo chmod 555 "${docs[$ROLE]}.service"  # Select from associative array
# ^ Using "echo" to test. Remove if the script works properly.

You can source the settings file to load the variables:您可以获取设置文件以加载变量:

source settings

And then you can use them in your script:然后你可以在你的脚本中使用它们:

chmod 555 "${admin}.service"  # will replace ${admin} with master_doc

I'd certainly use source(.)我当然会使用source(.)

#!/bin/sh
ADMIN=master_doc
STAFF=staff_doc
GUEST=welcome_doc

echo "accessing role type"
. /setting/settings 2> /dev/null || { echo 'Settings file not found!'; exit 1; }

role=${ROLE^^} # upercase rolename
echo ${!role}.service # test echo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM