Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #215310

Re: Signs of hard drive failure?

From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups linux.debian.user
Subject Re: Signs of hard drive failure?
Date 2019-10-21 15:40 +0200
Message-ID <yTG4F-2V8-3@gated-at.bofh.it> (permalink)
References <yTzmx-75Q-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


On Sun, Oct 20, 2019 at 10:21:20PM -0400, Ken Heard wrote:
> #! /bin/bash
> CURPWD=$PWD
> cd /home/ken
> tar -czf /media/fde/backups/kfinancescurrent.tgz --wildcards\
> - --exclude-from=docs/tarlists/kfinancesarchive.lst docs/01-kens/Finances
> cd $CURPWD

What happens if cd /home/ken fails?  Suddenly your tar command is
running from the wrong location.

You must always check the result of a cd command, and take appropriate
action when it fails.  For example,

#!/bin/bash
cd /home/ken || exit
tar czf ...


If this is the entire script, then that's all you need.  You don't need
the second cd command at all, because the current working directory of
the script doesn't matter after the script exits.

If there's other stuff after this tar command, and you want to "undo"
the cd that you used for tar, there are a few good ways to go about
this.

The first is to encapsulate the cd+tar into a subshell.  The cd happens
in the subshell, so as soon as the subshell terminates, the original
process resumes with its original working directory.

#!/bin/bash
(cd /home/ken &&
 tar czf ...
)
other commands


The second is to take advantage of tar's -C option, which allows tar
to change directory internally.  Then, your script doesn't need to cd
at all.

#!/bin/bash
tar c -C /home/ken -zf ...


Finally, if you *do* choose to store a directory name in a variable and
use that variable later in the script, you must quote it correctly.

cd "$olddir"


This is not optional, and not negotiable.  The quotes are REQUIRED.

https://mywiki.wooledge.org/Quotes

Back to linux.debian.user | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Signs of hard drive failure? Greg Wooledge <wooledg@eeg.ccf.org> - 2019-10-21 15:40 +0200
  Re: Signs of hard drive failure? Renato Gallo <renato@aster-lab.com> - 2019-10-22 11:10 +0200
  Re: Signs of hard drive failure? elvis <elvis@dogonfire.com> - 2019-10-22 11:30 +0200
    Re: Signs of hard drive failure? David Wright <deblis@lionunicorn.co.uk> - 2019-10-22 17:00 +0200
      Re: Signs of hard drive failure? elvis <elvis@dogonfire.com> - 2019-10-22 23:20 +0200
        Re: Signs of hard drive failure? Christopher David Howie <me@chrishowie.com> - 2019-10-23 01:00 +0200
  Re: Signs of hard drive failure? Christopher David Howie <me@chrishowie.com> - 2019-10-22 19:40 +0200
  Re: Signs of hard drive failure? Bob Weber <bobrweber@gmail.com> - 2019-10-22 21:20 +0200

csiph-web