2011-03-22 55 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