Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All, I have an app where user picks up a zipped folder which a shell script unzips and then renames for processing. The zipped folder will always have the following naming convention: "rXXXXX_CityName.info.zip". The folder inside the zip file is named as "CityName.info" When the shell script unzips the zip file, it will append the "rXXXXX_" to the folder name to create a folder called "rXXXXX_CityName.info". Before unzipping ,the shell script checks if a folder called "rXXXXX_CityName.info" exists, and deletes it if it does. Here is the code:

 THEZIPFILE="r12345_Boston.info.zip" 
 DIR="r12345_Boston.info"  
 if [ -d "$DIR" ]; then
    rm -rf "$DIR"
    sleep 1
 fi  

 if [ ! -d "$DIR" ]; then
    unzip -o "$THEZIPFILE" > /dev/null &
    zpid=$!
    wait $zpid
    EXIT_STATUS=$?
    if [ $EXIT_STATUS -eq 0 ]; then
        rm -rf  __MACOSX
        # Do Some processing....
    fi
    echo $EXIT_STATUS
    exit
else
    echo "-1"
    exit
fi

I want to know if this code leaves any room for mistakes? Am I right in assuming that the above code will execute sequentially(i.e wait for every command's completion before moving on to the next one)?

share|improve this question
up vote 2 down vote accepted

Looks OK, just seems some codes are unnecessary? For example, you can do rm directly and you can run unzip without & to wait.

 THEZIPFILE="r12345_Boston.info.zip" 
 DIR="r12345_Boston.info"  
 rm -rf "$DIR"
 unzip -o "$THEZIPFILE" > /dev/null
 EXIT_STATUS=$?
 if [ $EXIT_STATUS -eq 0 ]; then
   rm -rf  __MACOSX
   # Do Some processing....
 fi
 exit $EXIT_STATUS
share|improve this answer

This might be more appropriate for StackOverflow.

That said, you're probably on the right track, but I wouldn't do it that way.

Usually scripts wait till the sub-program exits, unless something specific is done to stop them, specifically, adding the & to the end of your unzip command tells the script to continue without waiting. Then you have quite a bit of code to find out the process id of the command and wait until it finishes.

I'd suggest you simply remove it:

  unzip -o "$THEZIPFILE" > /dev/null

The sleep command after your rm also seems unnecessary.

share|improve this answer

The script doesn't verify $THEZIPFILE exists before attempting to unzip it. Also, if $DIR is a plain file and not a directory, it won't be removed before the unzip which will then give errors.

share|improve this answer
    
I posted a small part of the actual code.. I do all the checks in my shell script – smokinguns Mar 31 '12 at 6:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .