2014-03-27 36 views
0

我在bash腳本中不斷得到模糊的重定向。與我的「模棱兩可的重定向」相混淆

我是新來的,所以如果有人能解釋爲什麼我用我的下面的代碼得到這個,那就太好了。

這是我的代碼!

$input = [user inputs an email] 
$variable=$(date +"%Y-%m-%d-%H:%M") 
$mem_list=/root/Desktop/Dan/Logs/member-name-file" "$variable.txt 

這裏有一個是越來越曖昧的代碼(有一對夫婦這些,但同樣的問題。

if [[ -f $mem_list ]]   #check if file exists already 
then 
    echo $input >> $mem_list #if file is already there, just append input to the file 
else 
    echo $input > $mem_list #if file is not found, make a new one [Error is here!!] 
fi 

幫助我很好,謝謝!

+0

另請參閱[鏈接](http://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error) – 2016-05-05 23:52:00

回答

3

因爲$ mem_list包含空格,你有引用它;例如

if [[ -f "$mem_list" ]] 
    echo $input >> "$mem_list" 
+0

你還應該把'「$輸入「'用雙引號引起來,而且通常任何可變的插值,其中分詞和通配符擴展並不是你所需要的。 – tripleee

+0

ack;由於不支持'--'分隔符( - >嘗試輸出'input = -e'),所以'echo'對於發出用戶輸入是不利的。編寫'printf'%s「」$ input「>>」$ mem_list「'會更可靠。 – ensc

+0

有趣的事情,和我的主要問題是,它適用於>>但不適用於>(不含引號)。 – Dan

0

您的變量賦值也是錯誤的。左值沒有$標誌,並且不得有=附近的空格。

mem_list="/root/Desktop/Dan/Logs/member-name-file $variable.txt" 

你是什麼意思與[user inputs an email]?我的bash 4.2是有點困惑...

+0

由[用戶輸入一個電子郵件],我的意思是在我的整個程序中,用戶輸入電子郵件,一個接一個,保存到一個文本文件 – Dan