1

我在 bash(一个新手)中编写了一个脚本,该脚本在本地闪存驱动器上创建一个文件并在其上写入随机数据,直到它填满。然后它将文件复制到外部 USB 驱动器并删除本地文件。一旦文件被填满并且它在 USB 驱动器上,它就会被复制回本地 NAND 闪存,删除,然后再次从 USB 闪存驱动器复制(无限期,直到用户停止它或发生错误,以首先 - 这是在一个while循环中)。当我运行脚本时,它显示:

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
4

1 回答 1

1

您在顶部有这一行:

LOG="/tmp/activity.log"

接着:

/bin/echo `date` $* | $LOG

这是没有意义的,因为您需要在管道之后有一个有效的 Unix 命令,而不仅仅是文件名。

于 2013-04-24T13:36:55.680 回答