Gentoo Archives: gentoo-user

From: Hinnerk van Bruinehsen <h.v.bruinehsen@×××××××××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] to nest commands
Date: Tue, 26 Nov 2013 10:52:27
Message-Id: 20131126105210.GB2553@TranscendTheRubicon.zedat.fu-berlin.de
In Reply to: Re: [gentoo-user] to nest commands by "edwardunix@live.com"
1 On Mon, Nov 25, 2013 at 11:45:39PM -0800, edwardunix@××××.com wrote:
2 > On Tue, 26 Nov 2013 01:16:45 +0200
3 > Alan McKinnon <alan.mckinnon@×××××.com> wrote:
4 >
5 > > You don't do it that way. I understand what you want to do, but your
6 > > description makes no sense.
7 > >
8 > > How you do it is by running two commands on one line, one after the other.
9 > >
10 > > To copy a file "myfile.txt" to /tmp and also change it's permissions,
11 > > use the ";" separator:
12 > >
13 > > cp myfile.txt /tmp ; chmod 644 /tmp/myfile.txt
14 > >
15 > > That runs the first command (cp) and then blindly runs the second one.
16 > >
17 > >
18 > >
19 > >
20 > > Sometimes you want to run the second command only if the first one
21 > > succeeds (there's not much point in chmod'ing a file that didn't copy
22 > > properly. "&&" does this:
23 > >
24 > > cp myfile.txt /tmp && chmod 644 /tmp/myfile.txt
25 > >
26 > > "&&" is boolean logic and a very common programming trick. I won't bore
27 > > you with details - it gets complex and we'd have to deal with brash
28 > > crazies like why true and false is the wrong way round the the rest of
29 > > the world, but just know it this way:
30 > >
31 > > the second command (chmod) will only run if the first (cp) succeeded. If
32 > > it failed, the chmod will not be be tried.
33 > >
34 > > Note that "&&" is definitely not the same thing as just one "&" - that
35 > > is something completely different. Bash is full of such stuff, it's all
36 > > done deliberately to mess with your head :-)
37 > >
38 > Thanks for the prompt reply and free lesson, I appreciate it:-)
39 > Yes...this is exactly what I was looking for.
40
41 There are some other options of "nesting" as well. You can use backticks "`" or
42 $(...) to run a command "inside" another. An example would be emerge `qlist -CI
43 x11-drivers` (or the equivalent emerge $(qlist -CI x11-drivers) ) . This would
44 run "qlist -CI x11-drivers" (lists installed packages of the category
45 x11-drivers) and use this output for emerge (which will effectively result in
46 reinstalling every package from the x11-drivers category).
47
48 WKR
49 Hinnerk

Attachments

File name MIME type
signature.asc application/pgp-signature

Replies

Subject Author
Re: [gentoo-user] to nest commands Randy Barlow <randy@×××××××××××××××××.com>