Error handling in shell script -
i have following shell script. throws following error if there no file exist in folder. so, how handle script doesn't stop executing?
error:
mv: cannot stat `*': no such file or directory
script:
for file in * fl=$(basename "$file") flname="${fl%%.*}" gunzip "$file" mv "$flname" "$flname-nxdweb2" tar -zcf "$flname-nxdweb2".tar.gz "$flname-nxdweb2" rm "$flname-nxdweb2" done;
if shell bash
, can allow *
expand null string: shopt -s nullglob
before loop.
btw, might want explicitly specify uncompressed filename produce, in case logic doesn't agree gunzip
's (which won't, if there's more 1 dot in name, or file ends .tgz
or .taz
):
gunzip -c "$file" >"$flname"
(you need remove original in case, though)
you can avoid need move, too:
flname="${fl%%.*}-nxdweb2"
and might want use trap
ensure temporaries cleaned in failure case (possible make temporaries in $tmpdir
, too).
Comments
Post a Comment