将键/值参数从文件读取到shell脚本中 [英] Reading key/value parameters from a file into a shell script
本文介绍了将键/值参数从文件读取到shell脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的脚本工作差不多了。这样做的目的是从文件中获取定义的值,然后在运行时在shell脚本中填充这些valeus。
I've got my script working almost. The goal of this is to take defined values from a file and then populate these valeus in a shell script at runtime.
请看看我在这里有什么...
Please take a look at what i have here...
第一个文件:ab.sh
The first file: ab.sh
#!/bin/bash
USER_TYPE=$1 #IDENTIFY USER TYPE TYPE1,TYPE2,TYPE3,TYPE4
USERNAME=$2
PERMISSION_TYPE=$3 #IDENTIFY PERMISSION TYPE SELECT,UPDATE,DELETE,INSERT
TARGET_USER=$4
TARGET_TABLE=$5
if [ $USER_TYPE == 'TYPE1' ]
then
cat Parameters.conf |while read USERNAME
do
sqlplus / as sysdba << E00
CREATE USER ${USERNAME}
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
ACCOUNT UNLOCK;
ALTER USER ${USERNAME} DEFAULT ROLE ALL;
GRANT CREATE SESSION TO ${USERNAME};
GRANT CONNECT TO ${USERNAME};
exit
E00
done
cat p.conf |while read PERMISSION_TYPE TARGET_SCHEMA TARGET_TABLE USERNAME
do
sqlplus / as sysdba > /home/o/output/output.log << E01
GRANT ${PERMISSION_TYPE} ON ${TARGET_USER}.${TARGET_TABLE} TO ${USERNAME};
E01
done
fi
这是参数文件。 conf定义值的地方,应来自......
This is the file Parameters.conf where the values are defined and should come from...
Parameters.conf
Parameters.conf
USER_TYPE TYPE1
USERNAME NEWUSER
PERMISSION_TYPE SELECT,UPDATE
TARGET_USER TESTUSER
TARGET_TABLE ABC
推荐答案
第一选择:关联数组
这需要使用bash 4.0或更高版本来支持声明-A
;请参阅下面的其他选项。
First choice: Associative array
This requires bash 4.0 or newer to support declare -A
; see below for other options.
#!/bin/bash
# first, read your key/value pairs into shell variables
declare -A v=( )
while read -r var value; do
v[$var]=$value
done < Parameters.conf
# second, perform a command that depends on them
sqlplus / as sysdba << E00
CREATE USER ${v[USERNAME]}
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
ACCOUNT UNLOCK;
ALTER USER ${v[USERNAME]} DEFAULT ROLE ALL;
GRANT CREATE SESSION TO ${v[USERNAME]};
GRANT CONNECT TO ${v[USERNAME]};
exit
E00
sqlplus / as sysdba > /home/o/output/output.log << E01
GRANT ${v[PERMISSION_TYPE]} ON ${v[TARGET_USER]}.${v[TARGET_TABLE]} TO ${v[USERNAME]};
E01
要点:
Key points:
- 在本身命名的shell变量的赋值/mywiki.wooledge.org/BashFAQ/006#Assigning_indirect.2Freference_variablesrel =nofollow> BashFAQ#6 ;同样用于关联数组。
- 重定向需要以
的形式完成,同时读取键值;做......;完成< input
而不是cat输入|读取键值;做......;完成
以避免错误 BashFAQ#24 。 - 实际的
sqlplus
调用不应该在循环中,因为循环体在文件中每行运行一次。由于您希望 all 在运行sqlplus
之前读取文件的行(以及分配的所有变量),循环应该在调用sqlplus
之前完全完成。 - 使用前缀或关联数组可确保来自配置文件的变量可以不要覆盖系统环境变量,如
PATH
或LD_PRELOAD
。
- Assignment to a shell variable which is itself named in a variable is described in BashFAQ #6; likewise for associative arrays.
- The redirection needs to be done as
while read key value; do ...; done <input
rather thancat input | while read key value; do ...; done
to avoid the bug BashFAQ #24. - The actual
sqlplus
calls shouldn't be inside the loop, because the loop body is run once per line in the file. Since you want all the file's lines to be read (and all the variables assigned) before runningsqlplus
, the loop should entirely complete beforesqlplus
is called. - Using either a prefix or an associative-array ensures that variables coming from the configuration file can't override system environment variables like
PATH
orLD_PRELOAD
.
#!/bin/bash
while read -r var value; do
printf -v "v_$var" %s "$value"
done <Parameters.conf
# second, perform a command that depends on them
sqlplus / as sysdba << E00
CREATE USER ${v_USERNAME}
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP
ACCOUNT UNLOCK;
ALTER USER ${v_USERNAME} DEFAULT ROLE ALL;
GRANT CREATE SESSION TO ${v_USERNAME};
GRANT CONNECT TO ${v_USERNAME};
exit
E00
sqlplus / as sysdba > /home/o/output/output.log << E01
GRANT ${v_PERMISSION_TYPE} ON ${v_TARGET_USER}.${v_TARGET_TABLE} TO ${v_USERNAME};
E01
这篇关于将键/值参数从文件读取到shell脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文