Better alternative to pass variables via stdin with '\n'

152 Views Asked by At

I'm creating a bash script to automate a process to create an Oracle database. Basically I want to invoke dbca. I already found two ways to do it but I still did not like those solutions. I was just wondering if has a better way to do it.

First I already have the sys_password defined on my script

read -p -s "SYS Password: " sys_password

The simplest way is to create a temporary file with the password and pass it via STDIN.

echo $sys_password > /tmp/file_pwd.txt
echo $sys_password >> /tmp/file_pwd.txt
echo "" >> /tmp/file_pwd.txt
dbca -silent -createDatabase -responseFile /assets/dbca.rsp < /tmp/file_pwd.txt

However, this approach has a security issue because I'm creating a physical file with a sensitive information in the filesystem, even after removed in somehow it can be recovered.

So, the best solution that I could imagine was using the heredocs and pass it via pipe to dbca.

cat <<EOF | dbca -silent -createDatabase  -responseFile /assets/dbca.rsp
${sys_password}
${sys_password}
EOF

I have tried echo and printf combined with pipe but it did not produce the same result, anyway, it does not work. Bellow is the code not working:

printf "${sys_password}\n${sys_password}\n" | \
    dbca -silent -createDatabase  -responseFile /assets/dbca.rsp

I would like to know if there is another way to send the printf or echo output to dbca STDIN in jut one line

1

There are 1 best solutions below

0
On

With tom's hints, I could solve this question. Also, Charles Duffy gave me valuable advisor to make the code better. Thanks for all your support! Here is the final code:

printf "%s\n%s\n" "${sys_password}" "${sys_password}" | \
    dbca -silent -createDatabase -responseFile /assets/dbca.rsp