Gentoo Archives: gentoo-user

From: "edwardunix@××××.com" <edwardunix@××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] to nest commands
Date: Mon, 25 Nov 2013 23:45:43
Message-Id: BLU0-SMTP1470E2F1AF586A685AF572CA8ED0@phx.gbl
In Reply to: Re: [gentoo-user] to nest commands by Alan McKinnon
1 On Tue, 26 Nov 2013 01:16:45 +0200
2 Alan McKinnon <alan.mckinnon@×××××.com> wrote:
3
4 > You don't do it that way. I understand what you want to do, but your
5 > description makes no sense.
6 >
7 > How you do it is by running two commands on one line, one after the other.
8 >
9 > To copy a file "myfile.txt" to /tmp and also change it's permissions,
10 > use the ";" separator:
11 >
12 > cp myfile.txt /tmp ; chmod 644 /tmp/myfile.txt
13 >
14 > That runs the first command (cp) and then blindly runs the second one.
15 >
16 >
17 >
18 >
19 > Sometimes you want to run the second command only if the first one
20 > succeeds (there's not much point in chmod'ing a file that didn't copy
21 > properly. "&&" does this:
22 >
23 > cp myfile.txt /tmp && chmod 644 /tmp/myfile.txt
24 >
25 > "&&" is boolean logic and a very common programming trick. I won't bore
26 > you with details - it gets complex and we'd have to deal with brash
27 > crazies like why true and false is the wrong way round the the rest of
28 > the world, but just know it this way:
29 >
30 > the second command (chmod) will only run if the first (cp) succeeded. If
31 > it failed, the chmod will not be be tried.
32 >
33 > Note that "&&" is definitely not the same thing as just one "&" - that
34 > is something completely different. Bash is full of such stuff, it's all
35 > done deliberately to mess with your head :-)
36 >
37 Thanks for the prompt reply and free lesson, I appreciate it:-)
38 Yes...this is exactly what I was looking for.
39
40 --
41 edwardunix@××××.com <edwardunix@××××.com>

Replies

Subject Author
Re: [gentoo-user] to nest commands Hinnerk van Bruinehsen <h.v.bruinehsen@×××××××××.de>