我的課程作業涉及到編寫一個Linux腳本來從僞垃圾文件夾'還原'文件。我已經做到了這一點,它看起來像應該工作,但它不。相反,我得到一個錯誤,指出:在linux中使用'sed'時出現額外的字符錯誤
SED:-e表達式#1,燒焦2:命令後,多餘的字符
,但我不明白爲什麼,因爲我不使用-e表達式在我的代碼中的任何地方。然而,我的大腦因爲長時間盯着代碼而炸了,以至於它可能只是我看不見的一個愚蠢的錯誤。任何幫助將不勝感激!
這裏是我的代碼:
#!/bin/sh
if [ "$1" = "-n" ]
then
##if command is used with -n, prompts user to enter a new location for the file
cd ~/my-documents/mydir01/trashbin
restore=`sed "$2" ~/my-documents/mydir01/locations`
name=`basename "$restore"`
echo $name
echo "Enter new location for this file."
read loc
echo $loc
mv -i `"$name" "$newloc"/"$name"`
else
##file will be restored to its previous location
cd ~/my-documents/mydir01/trashbin
restore=`sed "$1" ~/my-documents/mydir01/locations`
loc=`dirname "$restore"`
echo $loc
name=`dirname "$restore"`
mv $1 $name
fi
可能是錯誤的,但我認爲你可能必須將你的名字='direname'$ restore「'放入2個空格以保持if/fi – JoshDM
使用你在一個答案的評論中給出的示例命令, 「sed」被稱爲「sed」NewFile02「〜/ my-documents/mydir01/locations」,無論「if」是否爲真。其中'N'是一個有效的命令,它不需要參數,下面的'e'將'sed'混淆了。這是錯誤來自的地方。基於此,我認爲你並不真正理解「sed」是什麼,或者你不會給出這樣的論點。 – livibetter