2011-03-22 108 views
0

我的工作如下形式的上的安装脚本:导出变量用反斜杠

# get username 
echo "Please enter your oracle username:" 
read -p "> " username 
stty -echo 

# get password 
echo "Please enter your oracle password:" 
read -r -p "> " password; echo 
stty echo 

# -- Create all text to output to config 
finaluser=$usernamelabel$username 
finalpassword=$passwordlabel$password 
echo -e $finaluser"\n"$finalpassword > $configfile 

问题是,如果像“Z \ 2Z”形式的密码,它输出到$ configfile为:

z^Bz 

有没有简单的方法可以避免这种情况?

回答

0

请勿嵌入\n,然后您不需要-e选项,该选项也会解释\2

echo "$finaluser" >$configfile 
echo "$finalpassword" >>$configfile 

cat >$configfile <<EOF 
$finaluser 
$finalpassword 
EOF 

,或者如果你真的想用一个命令

printf '%s\n%s\n' "$finaluser" "$finalpassword" 
+1

+1的'printf'版本第三条道路。还有更多的方法:'echo'$ finaluser“$'\ n'”$ finalpassword“> $ configfile'或'{echo”$ finaluser“;回声“$ finalpassword”; }> $ configfile' – 2011-03-23 02:21:10