Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Multiple Commands in Single Line

  1. #1
    Join Date
    Aug 2006
    Location
    Berkshire, England
    Beans
    116

    Multiple Commands in Single Line

    I know ; and && can be used to put multiple commands into a single line, but they don't always work.

    How do you have a single line command were it runs to programs at the same time even if the other one runs or fails to run.

    For example:
    Code:
    conky ; avant-window-navigator
    or the && variant will only run the command that is first (either conky or AWN depending on which is first)

    Also when replacing window managers (eg metacity --replace) the commands stop there and don't continue.

    How can I get round this?

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Multiple Commands in Single Line

    Code:
    command &
    Will run the command in the background and bring you back to the prompt, so you can do

    Code:
    command1 &
    command2 &
    command3 &
    and so on...
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Jun 2006
    Location
    vietnam
    Beans
    211

    Re: Multiple Commands in Single Line

    something like
    Code:
    $ command1 & ; command2 &
    ought to work too, if you really want it to be a one-liner.

  4. #4
    Join Date
    May 2005
    Location
    Somewhere south of Milan
    Beans
    216

    Re: Multiple Commands in Single Line

    try this:

    (command1 &);(command2 &)

    that should spawn two concurrent processes

  5. #5
    Join Date
    May 2007
    Beans
    946

    Re: Multiple Commands in Single Line

    Quote Originally Posted by ynnhoj View Post
    something like
    Code:
    $ command1 & ; command2 &
    ought to work too, if you really want it to be a one-liner.
    No.
    Code:
    ls & ; ls &
    bash: syntax error near unexpected token `;'

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Multiple Commands in Single Line

    Use parentheses, as garba suggested.
    「明後日の夕方には帰ってるからね。」


  7. #7
    Join Date
    Sep 2005
    Location
    Montreal Quebec Canada
    Beans
    82
    Distro
    Ubuntu 6.06

    Re: Multiple Commands in Single Line

    I've always used the pipe character which is | for putting multiple commands on the same line. It worked for me in linux before. And when I used to type commands in AIX when I was in the command line. Give that a try as well as an alternative.

    Compucore

  8. #8
    Join Date
    Jun 2006
    Location
    vietnam
    Beans
    211

    Re: Multiple Commands in Single Line

    Quote Originally Posted by init1 View Post
    No.
    Code:
    ls & ; ls &
    bash: syntax error near unexpected token `;'
    i did
    Code:
    leafpad & ; mirage & ; gtkpod &
    and it was a-okay. but my shell is zsh, not bash -- perhaps that explains it?

    edit: and fer the record, the example you posted works fine for me as well

  9. #9
    Join Date
    Jun 2006
    Location
    vietnam
    Beans
    211

    Re: Multiple Commands in Single Line

    Quote Originally Posted by Compucore View Post
    I've always used the pipe character which is | for putting multiple commands on the same line. It worked for me in linux before. And when I used to type commands in AIX when I was in the command line. Give that a try as well as an alternative.

    Compucore
    pipe is a bit of a different story, though -- it will redirect the output of one program into another. for example, to read the listing of a directory with many files, page-by-page, you could do
    Code:
    ls | more
    which, of course, is considerably different from
    Code:
    ls & ; more &
    (or something along those lines)
    Last edited by ynnhoj; August 22nd, 2007 at 02:39 AM. Reason: ampersands :(

  10. #10
    Join Date
    May 2010
    Beans
    7
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Multiple Commands in Single Line

    Using this:
    command1; command2

    Will execute both commands, whatever append after the first one.

    If you want to execute 1 command, than execute a second one only if the first one succeed, you have to use this:
    command1 && command2

    Explanation:
    The && is the binary AND operator. When you use it with commands, the line is evaluate as a Boolean expression. To be TRUE (succeed), the expression must have both parts TRUE. If the first one is FALSE (the command fail), the system do not need to evaluate the second one since "FALSE && whatever" is always FALSE.

    NOTE: You can use this with multiple commands:
    command1 && command2 && command3 && command4
    You can also mix it with the OR operator (two pipes ||), but it make code very difficult to understand. If you want to execute something more complex, use the if statement.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •