0

I have the following the shell script. Which throws the following error if there is no file exist in the folder. So, how we do handle this so that script doesn't stop executing?

Error:

   mv: cannot stat `*': No such file or directory 

Script:

for file in *
    do
        fl=$(basename "$file")            
        flname="${fl%%.*}"              
        gunzip "$file"
        mv "$flname" "$flname-NxDWeb2" 
        tar -zcf "$flname-NxDWeb2".tar.gz "$flname-NxDWeb2"
        rm "$flname-NxDWeb2"   
done;

1 Answer 1

2

If the shell is bash, you can allow * to expand to the null string: shopt -s nullglob before your loop.

BTW, you might want to explicitly specify the uncompressed filename to produce, in case your logic doesn't completely agree with gunzip's (which it probably won't, if there's more than one dot in the name, or the file ends with .tgz or .taz):

gunzip -c "$file" >"$flname"

(you will need to remove the original yourself in this case, though)

You can avoid the need to move, too:

flname="${fl%%.*}-NxDWeb2"

And you might want to use trap to ensure your temporaries are cleaned up in the failure case (possible make your temporaries in $TMPDIR, too).

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

Comments

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.