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'의 출력을'$ ($ tldp.org/LDP/abs/html/io-redirection.html) LOG' 파일을'>>'과 함께 사용하십시오. – simont
동의 할 것이고 그 경우에는'/ bin/echo'date' $ * >> $ LOG' – anubhava
@anubhava이어야합니다. - 파이프를 >>로 변경하고 실행하면 결과는 다음과 같습니다 : $ LOG : 모호한 리디렉션 – suffa