为什么我的bash脚本不起作用

时间:2013-04-24 13:32:03

标签: bash

我在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

1 个答案:

答案 0 :(得分:1)

这条线位于顶部:

LOG="/tmp/activity.log"

然后:

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

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

相关问题