2013-04-11 16 views
0

我創建了一個腳本,它創建兩個文件,並在兩個文件不同時反轉其內容。我無法理解如何解決這個錯誤:[:<filename>:shell編程中的意外錯誤

[:我的文件名:意外的錯誤在shell編程

代碼:

echo "Enter first filename :" 
read file1 
echo "Enter second filename:" 
read file2 
echo "Enter content of file 1 : " 
gedit $file1 
echo "Enter content of file 2 : " 
gedit $file2 

check=" " 

x=` cmp $file1 $file2 ` 
if [ $x -eq $check ] 
then 
echo "Both files are same" 
rm $file2 
echo "Redundant file removed!!!" 
else 
echo "They are different" 
fi 

tac $file1 | cat > temp1 
rev temp1 | cat > temp11 
tac $file2 | cat > temp2 
rev temp2 | cat > temp22 
mv temp11 $file1 
mv temp22 $file2 

echo "Content of both the files reversed" 
+2

哪行代碼導致錯誤? – 2013-04-11 17:47:48

+1

我的猜測是這樣的:'if [$ x -eq $ check]'應該可以重寫爲if'「$ x」==「$ check」]'作爲開始。 – William 2013-04-11 18:05:16

+2

當提出這樣的問題時,您需要發佈顯示問題的最小代碼片段。這意味着(因爲你的問題幾乎肯定存在於你的'if'中)跳過所有「輸入內容」的東西,以及'tac'的東西(不管那是什麼)。你應該能夠產生一個四行或五行的例子,它能夠再現出現問題的地方。回報(除了讓人們更有可能幫助你),你通常會通過這條路線發現自己的解決方案。 – 2013-04-11 18:26:24

回答

0

您可以直接使用cmpif聲明

if cmp -s $file1 $file2 
then 
    echo "Both files are same" 
    rm $file2 
    echo "Redundant file removed!!!" 
else 
    echo "They are different" 
fi 
0

你必須給w你正在使用的外殼。

但我認爲if [ $x -eq $check ]會導致問題。你的shell解釋包含' - '作爲開關的文件名。您可以重命名mYfILEname的到MyFileName的或雙引號的$ X:

if [ "$x" -eq "$check" ] 
0

在bash,邏輯運算符-eq -ne -gt -lt -ge -le特定的數值參數。對於比較字符串,您可以簡單地使用

if [ $x = $check ]; then 
# do whatever 
fi