To simplify detecting success or failure, I just echoed success or failure.
I also made some other corrections to your attempt:
- Used && instead of ; so that tar extraction is not attempted if the cd fails.
- Changed the parens to create sub-shell on RHS.
- Used . rather than * in the tar command to get all files in the dir (even those starting with .) and to ensure all sub-dirs are (recursively) copied.
- Removed verbosity on tar command.
I used the existence of the target directory as a
simple way to test the failure case (how are you testing the failure case?).
Worked for me as shown below:
$ pwd
/home/knob
$ cat t1.sh
#!/bin/ksh
if tar cf - . | (cd /var/tmp/dump && tar xf -); then
echo "success"
else
echo "failure"
fi
$ rm -fr /var/tmp/dump
$ ./t1.sh
./t1.sh[2]: /var/tmp/dump: not found.
failure
$ mkdir /var/tmp/dump
$ ./t1.sh
success
$ ls -l
total 8
-rwxrwxrwx 1 knob devel 108 30 Aug 17:51 t1.sh
$ ls -l /var/tmp/dump
total 8
-rwxrwxr-x 1 knob devel 108 30 Aug 17:51 t1.sh
Note that, for simplicity, I created a
scratch directory (/home/knob above) containing just the
test script itself t1.sh and cd'ed
into this directory -- so this is
the directory that is copied.
If yours does not work, post the output of running your command
(as I did above).
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.