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...
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
This is the file Parameters.conf where the values are defined and should come from...
Parameters.conf
USER_TYPE TYPE1
USERNAME NEWUSER
PERMISSION_TYPE SELECT,UPDATE
TARGET_USER TESTUSER
TARGET_TABLE ABC
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
To add additional features to the command while working with shell script can be achieved by using parameters i.e. by the help of command-line options along with the arguments. Parameters are used to help in choosing options from the command line.
$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.
The read command reads one line from standard input and assigns the values of each field in the input line to a shell variable using the characters in the IFS (Internal Field Separator) variable as separators.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With