1

I wrote a script in bash (a neophyte) that creates a file on the local flash drive and writes random data on it until it fills up. It then copies the file to an external usb drive and deletes the local file. Once the file has been filled up and it is on the usb drive, it is copied back to the local NAND flash, deleted, and copied from the usb flash drive again (indefinitely, until the user stops it or an error occurrs, whichever is first - this is in a while loop). When I run the script it displays:

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

and just hangs there. Can someone tell me if there are errors in my script? I'm very green when it comes to bash. Below is the code:

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 Answer 1

1

You have this line at the top:

LOG="/tmp/activity.log"

and then:

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

It doesn't make sense since you need to have a valid Unix command after pipe not just the file name.

10
  • 1
    What the OP probably meant to do was redirect the output of /bin/echo into the $LOG file with >>.
    – simont
    Apr 24, 2013 at 13:39
  • Agreed and in that case it should have been /bin/echo date` $* >> $LOG`
    – anubhava
    Apr 24, 2013 at 13:40
  • @anubhava - I changed the pipe to >> and now the result when running is: $LOG: ambiguous redirect
    – suffa
    Apr 24, 2013 at 13:46
  • @suffa: You probably don't need a separate echo function like this, what if some command writes output using some other way like using printf. You can insert exec > $LOG line just below LOG="/tmp/activity.log" line to capture all the output.
    – anubhava
    Apr 24, 2013 at 13:52
  • @anubhava - having done that, do I need to change all of the lines where echo is used to printf?
    – suffa
    Apr 24, 2013 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.