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.
4
  • It would be better to just use the find command and dispense with the intermediate file. Commented Aug 7, 2011 at 7:12
  • 1
    run it with "bash -x" so you'll be able to see where and probably why did it hang Commented Aug 7, 2011 at 7:23
  • I new to unix..May i know how to add -x in the script ? Can gv some example ? Commented Aug 7, 2011 at 7:58
  • The cat, the head, and the regex wildcard are all unnecessary. Also, don't read lines with for. grep -m 3000 MSCERC /tmp/msc.load.tmp | while read -r file; do ... Finally, quote your variables. Commented Feb 28, 2019 at 13:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Which part guarantees that stdout and stderr will be in chronological order?
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.
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.
I new to unix..May i know how to set -x in the top of the script ? Can gv some example ?
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

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.