3

I have the code below. Basically this code will ls -tr $FROM_DIRECTORY then redirect the output to /tmp/msc.load.tmp. After this a for loop will execute and move the first 3000 files to another directory. The code is working fine but sometimes it will hang. I have no idea why it hangs. Anyone know what is the problem of the script?

ls -tr $FROM_DIRECTORY > /tmp/msc.load.tmp
echo "$sysdate -- Listing Diretory " >>$LOGFILE
# From the file list, get the 3000 from the top to move. Skip the remaining files in the list 
# in this iteration. 
# Version 1.1 - List the files from the temporary file.  
for file in $(cat /tmp/msc.load.tmp | grep 'MSCERC.*' | head -3000 )
do 
   mv $FROM_DIRECTORY/$file $DESTINATION_DIRECTORY
done
echo "$sysdate -- End of Script " >>$LOGFILE
exit 0
# End of script.
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
user871695
  • 431
  • 4
  • 7
  • 14
  • It would be better to just use the `find` command and dispense with the intermediate file. – Keith Aug 07 '11 at 07:12
  • 1
    run it with "bash -x" so you'll be able to see where and probably why did it hang – hmontoliu Aug 07 '11 at 07:23
  • I new to unix..May i know how to add -x in the script ? Can gv some example ? – user871695 Aug 07 '11 at 07:58
  • The `cat`, the `head`, and the regex wildcard are all unnecessary. Also, [don't read lines with `for`.](https://mywiki.wooledge.org/DontReadLinesWithFor) `grep -m 3000 MSCERC /tmp/msc.load.tmp | while read -r file; do` ... Finally, [quote your variables.](/questions/10067266/when-to-wrap-quotes-around-a-shell-variable/27701642) – tripleee Feb 28 '19 at 13:24

1 Answers1

4

Yeap, try a find.

Also if you're not aware of it, the set -x command is valuable in situations like this. My approach is to add set -x to the top of the script and then run it with output redirected to a file. capturing both standard out and standard error

./script.sh > output.txt 2>&1

If you want you can tail -f output.txt in another window to monitor progress.

set -x echos the commands run and the redirection puts the command and output into chronological order.

Yuri
  • 4,254
  • 1
  • 29
  • 46
Karl
  • 3,312
  • 21
  • 27
  • Which part guarantees that stdout and stderr will be in chronological order? – Ignacio Vazquez-Abrams Aug 07 '11 at 07:20
  • The writing of lines cronologically. As opposed to trying to relate the stderr from the console and stdout from a redirect without the stderr part. – Karl Aug 07 '11 at 07:31
  • But there are still no guarantees that they will be written out in sync. I mean, yes, it's better than having them go to two separate destinations, but if the streams are flooded then they will not necessarily be output in order. But I guess the asker will have to take their chances. – Ignacio Vazquez-Abrams Aug 07 '11 at 07:35
  • I new to unix..May i know how to set -x in the top of the script ? Can gv some example ? – user871695 Aug 07 '11 at 07:59
  • Either call the script with the -x parameter as hmontoliu says above or edit your script and above the line ls -tr $FROM_DIRECTORY > /tmp/msc.load.tmp enter set -x – Karl Aug 07 '11 at 22:29