1
我在bash(一位新手)中編寫了一個腳本,它在本地閃存驅動器上創建一個文件並在其上寫入隨機數據,直到它填滿爲止。然後它將文件複製到外部USB驅動器並刪除本地文件。一旦文件已經被填滿並且位於USB驅動器上,它將被複制回本地NAND閃存,被刪除,並且再次從USB閃存驅動器複製(無限期地,直到用戶停止它或發生錯誤,無論是哪一個首先 - 這是在一個while循環)。當我運行腳本時,它顯示:爲什麼我的bash腳本不能正常工作
nand_test.sh: line 19: /tmp/activity.log: Text file busy
nand_test.sh: line 19: /tmp/activity.log: Text file busy
nand_test.sh: line 19: /tmp/activity.log: Text file busy
nand_test.sh: line 19: /tmp/activity.log: Text file busy
並且只是掛在那裏。有人能告訴我腳本中是否有錯誤?說到bash,我很綠。下面是代碼:
LOG="/tmp/activity.log"
function if_error
{
if [[ $? -ne 0 ]]
then
print "$1 TIME:$TIME" | tee -a $LOG
exit $?
fi
}
#whenever the echo command is ran this function allows me add a timestamp
function echo {
/bin/echo `date` $* | $LOG
}
#condition below checks if activity.log exists. If not, it creates it.
mydir="/media/myusb"
chmod +x /tmp/activity.log
activity="/tmp/activity.log"
if [ -e "$activity" ];then
echo - "$activity" LOG ALREADY EXISTS >> "$activity"
else
> /activity.log
#echo - "$activity" LOG HAS BEEN CREATED >> "$activity"
fi
#condition below checks if myusb directory is created. If not, it creates it.
if [ -d "$mydir" ];then
echo - "$mydir" ALREADY EXISTS >> "$activity"
else
mkdir /media/myusb
#echo - "$mydir" HAS BEEN CREATED >> "$activity"
fi
#check if external usb has been mounted. if not mount it.
device0="/dev/sda1"
if [ -b "$device0" ];then
echo - "$device0" IS ALREADY MOUNTED >> "$activity"
else
mount LABEL=MYFLASH /media/myusb
#echo - "$device0" HAS BEEN MOUNTED >> "$activity"
fi
#condition below checks if testfile.txt has been created. If not, it creates it.
testfile="/storage/testfile.txt"
if [ -e "$testfile" ];then
echo - "$testfile" FILE ALREADY EXISTS >> "$activity"
else
>> /storage/testfile.txt
#echo - "$testfile" HAS BEEN CREATED! >> "$activity"
fi
dd if=/dev/urandom of=/storage/testfile.txt >> "$activity"
ret=$?
if_error "urandom data failed writing to testfile.txt"
if [ "$ret" gt 0 ];then
echo - "NAND FLASH DATA TEST FAILED >> "$activity"
else
cp "$testfile" "$mydir"
fi
rm "$testfile"
sync
sleep 3
while [ -e "/media/myusb/testfile.txt" ]
do
cp /media/myusb/testfile.txt /storage
sync
DIFF= diff /media/myusb/testfile.txt "$testfile"
if [ "$DIFF" != "" ];then
echo - "ERROR: THE FILE HAS BEEN MODIFIED" >> "$activity"
else
echo - "FILE COMPARISON SUCCESSFUL" >> "$activity"
fi
rm "$testfile"
sync
OP可能要做的是將'/ bin/echo'的輸出(重定向)(http://www.tldp.org/LDP/abs/html/io-redirection.html)放入'$帶'>>'的LOG'文件。 – simont 2013-04-24 13:39:17
同意,在這種情況下,應該是'/ bin/echo'date' $ * >> $ LOG' – anubhava 2013-04-24 13:40:18
@anubhava - 我將管道更改爲>>,現在運行時的結果是:$ LOG:模糊重定向 – suffa 2013-04-24 13:46:09