[NCLUG] Oops,

Bob Proulx bob at proulx.com
Thu Feb 20 21:38:48 MST 2014


Steve Wolf wrote:
> I agree.  --exclude makes your life much easier.  --recursion is the
> default behavior in tar, so I'd suggest

Agreed to all.

> #!/bin/sh
> pushd /home
> tar cvzpPf /mnt/UserBackup/BKkerrym2.tar.gz \
>   --exclude=kerrym2/KerrysStuff \
>   --exclude=kerrym2/Pictures \
>   --exclude=kerrym2/Downloads \
>   --exclude=kerrym2/.wine/drive_c \
>   kerrym2
> popd
> echo -e "\nBKkerrym2.sh done"
> return

Hmm...  That depends upon the system.

  rwp at dismay:~$ /bin/sh
  $ type pushd
  pushd: not found
  $ type popd
  popd: not found
  $ 

It depends on if your system has /bin/sh symlinked to bash or not
since those are bash'isms.  Ubuntu, Debian, Mint, others, link it to
dash.  But the popd is not needed.  It isn't necessary to cd anywhere
at the end of the script when exiting.  Just exit the script.  And I
mean "exit 0" not "return" too.  :-) "return" is from shell functions
not the script itself.

Amazingly echo is much less portable than people think.  I would avoid
the -e option.  If you really want escapes then "printf" is the best
most portable way to do that these days.  AFAIK the -p is only for
extracting from the tar file.  I also don't think the -P option
is needed either.  With that I suggest:

#!/bin/sh
cd /home || exit 1
tar cvzf /mnt/UserBackup/BKkerrym2.tar.gz \
  --one-file-system \
  --exclude=kerrym2/KerrysStuff \
  --exclude=kerrym2/Pictures \
  --exclude=kerrym2/Downloads \
  --exclude=kerrym2/.wine/drive_c \
  kerrym2
if [ $? -ne 0 ]; then
  echo "Error: tar exited with an error" 1>&2
  exit 1
fi
echo "BKkerrym2.sh done"
exit 0

If we keep passing it around and everyone adds something to it by the
end of the week we would have full option processing and online help
in there too.  :-)

Bob


More information about the NCLUG mailing list