How to concatenate two (mor more) commands on one line?

  • Follow


Assume I want to execute 2 commands. The second command should only processed if the first was finished successfully.

Furthermore the concatenation should take place on one line WITHOUT an "If" condition.

I though something like the following should match the conditions

"command 1" && "command 2"

But this doesn't work. E.g. if commands contains some parameter which in turn wrapped in double quotes e.g.

"ls -l "/user/home/user/sub dir name" " && "....

gives an error

How else can I achieve command concatenation?

Peter

2
Reply peter_ha 1/31/2010 9:06:10 AM

* 2010-01-31 09:06 (UTC), Peter Hanke wrote:

> I though something like the following should match the conditions
>
> "command 1" && "command 2"
>
> But this doesn't work. E.g. if commands contains some parameter which
> in turn wrapped in double quotes e.g.
>
> "ls -l "/user/home/user/sub dir name" " && "....
>
> gives an error

Do not use quotes around the commands, just

    command1 arg1 arg2 && command2 arg1 arg2

    ls -l "/home/user/sub dir name" && command

will do. If you need to group some commands use {}:

    command1 && { command2; command3; command4; }
0
Reply Teemu 1/31/2010 9:15:57 AM


On 2010-01-31, Peter Hanke <peter_ha@andres.net> wrote:
> Assume I want to execute 2 commands. The second command should only processed if the first was finished successfully.
>
> Furthermore the concatenation should take place on one line WITHOUT an "If" condition.
>
> I though something like the following should match the conditions
>
> "command 1" && "command 2"
>
> But this doesn't work. E.g. if commands contains some parameter which in turn wrapped in double quotes e.g.
>
> "ls -l "/user/home/user/sub dir name" " && "....
>
> gives an error
>
> How else can I achieve command concatenation?
>
> Peter
>

Just use
command1 && command2 
without the quotes.

peter
0
Reply Peter 1/31/2010 10:08:47 AM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

On Dom 31 Ene 2010 06:06,
Peter Hanke wrote:

> Assume I want to execute 2 commands. The second command should only
> processed if the first was finished successfully.
> 
> Furthermore the concatenation should take place on one line WITHOUT an
> "If" condition.
> 
> I though something like the following should match the conditions
> 
> "command 1" && "command 2"
> 
> But this doesn't work. E.g. if commands contains some parameter which in
> turn wrapped in double quotes e.g.

  OK, no problem, you can use round parenthesis to do
that task.

> 
> "ls -l "/user/home/user/sub dir name" " && "....

  Surely it does... try:

( ls -l "/user/home/user/sub dir name" ) \
 && ( ls -l "/user/home/user/another sub dir name" )


  Also, you can do:

( ( ls -l "/user/home/user/sub dir name" ) \
 && ( ls -l "/user/home/user/another sub dir name" ) )


  Just play a bit with those parenthesis... ;)

> 
> gives an error
> 
> How else can I achieve command concatenation?
> 
> Peter

Best regards,
- -- 
| Daniel Molina <dmw [at] coder [dot] cl> |
| IT Consulting & Software Development    |
| Phone: +56 2 9790277 | http://coder.cl/ |
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iQIcBAEBCgAGBQJLZY9tAAoJEHxqfq6Y4O5Nm/4P/1SUIEF6U/dB+W/IZsRQyKxO
es/X4n9q672kBG4esNHnrEtS1jinoH4pYiuK7mN6CdvHMKkViNU+3znu4TrPVw9x
OKCFEaHAVLkQcto008Iqjp4URq32hAfds6SsMDW8+3CLWnjPNkXtd+SzF+a2zk4x
uddSj5EviND7GY8/C4f4rdGFXCdgJnWrZGoscV2BzQPBKC0634cQX3LZd5p093+g
7Dsgg5ztuf6a16N3zu4SXvJ6fuzK7XQDY6HgZdHFO21R6PJRzeb+dpVsQj0hW8ox
Jd03t/3c1pt8H/k2bX2KaEImpgHjWj9wth6MksfCdqbuAnijGQ/pYrVMxE2ooyWh
RWr/RikoIdRkMDJ38f+/osVj+TUk76dGMAsOmTEWhJcqm/CLUMEZVDP/Ct5t8rFD
gHfn3zljh3x0Qg1eloc2uvAFfQV6NW0cnx1N8ojmh+5/7sDCjLJk/ULvfKEs64QC
i84Ythk/QiX37TZjcoj6mj+C+FwPui5i57HkyowkDRg34AYj9/TNv50OHKLYZWgA
fKxlaxvplAwbvTOeUFNQAwpZ5i4oLOWMMTEw9hj3qHG48xyjAz2I4qb1hpxaCRhg
z2MOqWl40PjgTFPmLeGyWw/4PhUuYUw3TxYZt81RXqEkJufPjl3wkSGmg49/Fy3V
QnkhvijWOSBvdoC4AhDn
=mDcW
-----END PGP SIGNATURE-----

0
Reply Daniel 1/31/2010 2:10:53 PM

On 2010-01-31, Peter Hanke <peter_ha@andres.net> wrote:
> Assume I want to execute 2 commands. The second command should only processed if the first was finished successfully.
>
> Furthermore the concatenation should take place on one line WITHOUT an "If" condition.
>
> I though something like the following should match the conditions
>
> "command 1" && "command 2"
>
> But this doesn't work. E.g. if commands contains some parameter which in turn wrapped in double quotes e.g.
>
> "ls -l "/user/home/user/sub dir name" " && "....

Why are you wrapping the command in qoutes? That says to regard the
whole thing as one string, and there exits no command which is the
string "ls -l" 

Just do 
ls -l /usr/home/user/sub dir name && rm name
or whatever you want ( what is that dir name supposed to be about?)


>
> gives an error
>
> How else can I achieve command concatenation?
>
> Peter
>
0
Reply unruh 1/31/2010 5:01:37 PM

peter_ha@andres.net (Peter Hanke) writes:

> Assume I want to execute 2 commands. The second command should only processed if the first was finished successfully.
>
> Furthermore the concatenation should take place on one line WITHOUT an "If" condition.
>
> I though something like the following should match the conditions
>
> "command 1" && "command 2"

In BASH, that might work, if you use *no arguments* and there are *no
trailing spaces* inside quotes.

The problem is that it treats what is between quotes as a piece, and
does not split it into smaller pieces as it usually does when there are
no quotes.

If you quote the command name and its arguments in one big string, it
will look for an executable named after that big string.

>
> But this doesn't work. E.g. if commands contains some parameter which in turn wrapped in double quotes e.g.
>
> "ls -l "/user/home/user/sub dir name" " && "....
>
> gives an error

Here it does the same thing because the left side is concatenated into
one string, and it looks for something named "ls -l /user/home/user/sub
dir name ", instead of just "ls". 

The problem isn't "recursive quoting", because it's not recursive, those
quotes are all handled at the same level. (Recursive would be if you had
escaped the quotes surrounding the part which was already quoted before
quoting the whole command.)

> How else can I achieve command concatenation?

This I don't need to answer because it was already answered in other
post :-)

-- 
Nuno J. Silva
gopher://sdf-eu.org/1/users/njsg
0
Reply nunojsilva 1/31/2010 6:59:27 PM

In article <4b654802$0$6574$9b4e6d93@newsspool3.arcor-online.net>,
 peter_ha@andres.net (Peter Hanke) wrote:

> Assume I want to execute 2 commands. The second command should only processed 
> if the first was finished successfully.
> 
> Furthermore the concatenation should take place on one line WITHOUT an "If" 
> condition.
> 
> I though something like the following should match the conditions
> 
> "command 1" && "command 2"
> 
> But this doesn't work. E.g. if commands contains some parameter which in turn 
> wrapped in double quotes e.g.
> 
> "ls -l "/user/home/user/sub dir name" " && "....
> 
> gives an error
> 
> How else can I achieve command concatenation?
> 
> Peter

Don't quote the commands, it's just:

command 1 && command 2

e.g.

ls -l "/usr/home/user/sub dir name" && ...

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
0
Reply Barry 2/1/2010 1:24:37 AM

6 Replies
4278 Views

(page loaded in 0.003 seconds)

Similiar Articles:













7/20/2012 1:35:20 PM


Reply: