1 |
On 26/11/2013 08:59, edwardunix@××××.com wrote: |
2 |
> Hello, |
3 |
> |
4 |
> My Bash skills are not that advanced, so |
5 |
> I am wondering if it is possible to nest one command inside in another command, not in a script,but on the command line,for instance |
6 |
> to copy a file to a different destination while changing permissons at the same time, all in one line. |
7 |
> |
8 |
|
9 |
|
10 |
You don't do it that way. I understand what you want to do, but your |
11 |
description makes no sense. |
12 |
|
13 |
How you do it is by running two commands on one line, one after the other. |
14 |
|
15 |
To copy a file "myfile.txt" to /tmp and also change it's permissions, |
16 |
use the ";" separator: |
17 |
|
18 |
cp myfile.txt /tmp ; chmod 644 /tmp/myfile.txt |
19 |
|
20 |
That runs the first command (cp) and then blindly runs the second one. |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
Sometimes you want to run the second command only if the first one |
26 |
succeeds (there's not much point in chmod'ing a file that didn't copy |
27 |
properly. "&&" does this: |
28 |
|
29 |
cp myfile.txt /tmp && chmod 644 /tmp/myfile.txt |
30 |
|
31 |
"&&" is boolean logic and a very common programming trick. I won't bore |
32 |
you with details - it gets complex and we'd have to deal with brash |
33 |
crazies like why true and false is the wrong way round the the rest of |
34 |
the world, but just know it this way: |
35 |
|
36 |
the second command (chmod) will only run if the first (cp) succeeded. If |
37 |
it failed, the chmod will not be be tried. |
38 |
|
39 |
Note that "&&" is definitely not the same thing as just one "&" - that |
40 |
is something completely different. Bash is full of such stuff, it's all |
41 |
done deliberately to mess with your head :-) |
42 |
|
43 |
|
44 |
-- |
45 |
Alan McKinnon |
46 |
alan.mckinnon@×××××.com |