2012-11-30 60 views
3

需要幫助。這是我用來將文件從垃圾箱目錄恢復到其原始位置的腳本。它位於根之前。然後使用其他腳本,這是「被刪除」,並存儲在垃圾箱目錄,和它的前位置使用該存儲文件中記錄:使用腳本時,下面我要恢復被刪除的文件在linux bash腳本中恢復

case $ans in 
    y) echo "`readlink -f $1`" >>home/storage & mv $1 /home/dustbin ;; 
    n) echo "File not deleted." ;; 
    *) echo "Please input answer." ;; 
esac 

左右,但以下錯誤出現。

#!/bin/sh 

if [ "$1" == "-n" ] ; then 
    cd ~/home/dustbin 
    restore="$(grep "$2" "$home/storage")" 
    filename="$(basename "$restore")" 
    echo "Where to save?" 
    read location 
    location1="$(readlink -f "$location")" 
    mv -i $filename "$location1"/$filename 
else 
    cd ~/home 
    storage=$home/storage 
    restore="$(grep "$1" "$storage")" 
    filename="$(basename "$restore")" 
    mv -i $filename $restore 
fi 

給定的錯誤 - mv: missing file operand

編輯:

這麼好,我改變了我的腳本是這樣的。

#!/bin/sh 

if [ $1 ] ; then 

    cd ~/home 
    storage=~/home/storage 
    restore="$(grep "$1" "$storage")" 
    filename="$(basename "$restore")" 
    mv -i "$filename" "$restore" 

fi 

,我仍然得到錯誤:

MV:不能STAT'文件名':沒有這樣的文件或目錄

+1

在第二個'mv'行之前加上'echo'<$filename><$restore>''(與第一行相似),看看發生了什麼。 – choroba

+0

可能重複[在Linux中恢復腳本](http://stackoverflow.com/questions/13615100/restore-script-in-linux) – shellter

+0

請學習如何在StackOverflow中使用搜索功能。 '[bash]恢復文件'返回已經被回答的許多問題。祝你好運。 – shellter

回答

0

您可能需要做一些基本的錯誤處理,看看你是否依舊存在$filename使用它作爲mv部分:

例如,前:

mv -i $filename "$location1"/$filename 

你或許應該做一個:

if [[ -e "$filename" ]]; then 
    # do some error handling if you haven't found a filename 
fi 

-e選項檢查下一個參數來[[是否指的是一個真實存在的文件名。如果是,則評估爲真,否則爲假。 (或者,使用-f來檢查它是否是普通文件)

或者至少: if [[-z「$ filename」]];然後 #做,如果你還沒有找到一個文件名 網絡

-z選項檢查下一個參數[[是否爲空字符串一些錯誤處理。如果是,則評估爲真,否則爲假。

與您的else條款類似的評論:mv -i $filename $restore

Here's a list of test options.

+0

所以沒關係,我試過這樣... if [[-e「$ filename」]];然後mv -i $ filename「$ location1」/ $ filename fi。我瞭解這一點是否正確?也嘗試了-z和-f。執行恢復文件名後,然後mv:不能stat'文件名':沒有這樣的文件或目錄。但它位於垃圾箱目錄中。 –

+0

評論#1的建議給我顯示了這條消息:,這是什麼意思? –

+0

@RihardsDobelis您的問題中缺少很多細節,例如文件的確切位置,以及您如何調用腳本(以及您傳遞給它的參數) –

0

你做

cd ~/home 

mv -i "$filename" "$restore" 

,而該文件位於垃圾箱目錄中,因此,它沒有找到。 不要要麼

cd ~/home/dustbin 

mv -i "dustbin/$filename" "$restore" 

或只是做

mv -i "~/home/dustbin/$filename" "$restore" 

,丟棄cd