简体   繁体   English

更好的选择,通过带有'\\ n'的stdin传递变量

[英]Better alternative to pass variables via stdin with '\n'

I'm creating a bash script to automate a process to create an Oracle database. 我正在创建一个bash脚本来自动化创建Oracle数据库的过程。 Basically I want to invoke dbca . 基本上我想调用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 首先,我已经在脚本上定义了sys_password

read -p -s "SYS Password: " sys_password

The simplest way is to create a temporary file with the password and pass it via STDIN . 最简单的方法是使用密码创建一个临时文件,然后通过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 . 因此,我能想象到的最佳解决方案是使用Heredocs并将其通过管道传递给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. 我已经试过echoprintf结合管,但它并没有产生相同的结果,无论如何,这是行不通的。 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 我想知道是否有另一种方式可以在一行中将printfecho输出发送到dbca STDIN

With tom 's hints, I could solve this question. 有了tom的提示,我可以解决这个问题。 Also, Charles Duffy gave me valuable advisor to make the code better. 另外, 查尔斯·达菲Charles Duffy)给了我宝贵的顾问,以使代码更好。 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 

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

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