2017-09-25 65 views
-1
#!/bin/bash 
source_dir="~/p6_tmp" 
target_dir="~/trash" 
if [ ! -d ${target_dir} ]; 
then 
mkdir target_dir 
else 
echo "Displaying the contents of current directory" 
for entry in "$source_dir"/* 
do 
    echo "$entry" 
echo "Please enter the filename you wish to trash:" 
read filename 
if [ -f ${source_dir}/${filename} ] 
then 
    if [ -f ${target_dir}/${filename} ] 
    then 
     mv "${source_dir}/${filename}" "${target_dir}/${filename}_bak" 
    else 
     mv "${source_dir}/${filename}" "$target_dir" 
    fi 
else 
    echo "The file ${source_dir}/${filename} does not exist" 
fi 

的問題是: 使用Bourne shell/bin/sh的書寫工具,叫做垃圾滿足以下規格:行26:語法錯誤:文件意外結束

使用方法:垃圾-l | -p | {filename} *

垃圾是rm工具的替代品。垃圾郵件將文件移動到主目錄中名爲.trash的子目錄中,而不是刪除文件。當有一個有效的文件要移動時,如果.trash不存在,它將被創建。如果.trash存在,則-l選項列出當前內容。如果.trash存在,-p選項將刪除目錄及其中的所有文件。

這段代碼有什麼問題?意外文件在哪裏,我是否想要關閉一個文件或者我是否有額外的文件?我試圖修復,但它不讓我做任何事情。 謝謝

+4

修復您的縮進問題將立即明顯,然後你就完成了。 –

+0

在發佈之前學習在http://shellcheck.net中剪切/粘貼代碼。祝你好運。 – shellter

回答

0

由於埃德莫頓先生也建議,因爲EOF錯誤即將到來,所以當你正確地縮進你的代碼時,你會知道罪魁禍首在哪裏。所以如果block不是用fi關閉的話。

#!/bin/bash 
source_dir="~/p6_tmp" 
target_dir="~/trash" 
if [ ! -d ${target_dir} ]; 
then 
    mkdir target_dir 
else 
    echo "Displaying the contents of current directory" 
    for entry in "$source_dir"/* 
    do 
     echo "$entry" 
     echo "Please enter the filename you wish to trash:" 
     read filename 
    if [ -f ${source_dir}/${filename} ] 
    then 
     if [ -f ${target_dir}/${filename} ] 
     then 
     mv "${source_dir}/${filename}" "${target_dir}/${filename}_bak" 
     else 
     mv "${source_dir}/${filename}" "$target_dir" 
     fi 
    else 
     echo "The file ${source_dir}/${filename} does not exist" 
    fi 
fi ## <---- This is the one which I added newly 
相關問題