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
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:
while read key value; do ...; done <input
rather than cat input | while read key value; do ...; done
to avoid the bug BashFAQ #24.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 running sqlplus
, the loop should entirely complete before sqlplus
is called.PATH
or LD_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