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

Thread: Remove newline between commands

  1. #1
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Remove newline between commands

    If I have a file called testfile with the following contents:

    Code:
    one two three
    and I want to combine reading a part of the file with echo to make stdout look like:

    Code:
    one five
    I might try something like:

    Code:
    echo -n $(cat testfile | grep -o 'one'); echo ' five'
    the 'echo -n' being necessary to prevent the newline. The problem with doing it this way is that if I wanted to only have "echo ' five'" run if the "cat testfile" section has an exit code of zero, it can't be done (as far as I can tell).

    Is there a better way of suppressing a newline between commands? Basically the equivalent of "echo -n" but for all commands? Like maybe temporarily telling bash to disable newlines between commands?
    Last edited by terminator14; March 17th, 2013 at 12:58 AM.

  2. #2
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Remove newline between commands

    Code:
    awk '/one/ { print "one five" }' testfile 2>/dev/null
    Last edited by prodigy_; March 16th, 2013 at 06:00 PM.

  3. #3
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,236
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Remove newline between commands

    Hi terminator14.

    You can use the variable $? to access the exit code of the last command. In order to use it in your example, you would have to separate your commands a little bit. For example:
    Code:
    tmp=$(grep -o 'one' testfile); if [ $? = 0 ]; then echo 'one five'; else echo 'five'; fi
    Does that help?
    Regards.

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Remove newline between commands

    Quote Originally Posted by terminator14 View Post
    The problem with doing it this way is that if I wanted to only have "echo ' five'" run if the "cat testfile" section has an exit code of zero, it can't be done (as far as I can tell).
    From the POSIX standard:
    If there is no command name, but the command contained a command substitution, the command shall complete with the exit status of the last command substitution performed.
    So, this one should work, too.
    Code:
    tmp=`grep -o one testfile` && echo $tmp five
    Last edited by schragge; March 16th, 2013 at 06:23 PM.

  5. #5
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: Remove newline between commands

    Quote Originally Posted by papibe View Post
    Hi terminator14.

    You can use the variable $? to access the exit code of the last command. In order to use it in your example, you would have to separate your commands a little bit. For example:
    Code:
    tmp=$(grep -o 'one' testfile); if [ $? = 0 ]; then echo 'one five'; else echo 'five'; fi
    Does that help?
    Regards.
    Thanks, but while the example I gave of cat stdout being piped to grep can be combined into a single command, the purpose of doing it as 2 commands was because the real problem I face (which I have greatly simplified in this thread) cannot have commands combined into a single one. While something like:

    Code:
    test@pc$ tmp=$(false)
    test@pc$ echo $?
    1
    with a single command works great, something with 2 commands does not:

    Code:
    test@pc$ tmp=$(false | true)
    test@pc$ echo ${PIPESTATUS[0]}
    0
    It outputs the error code of the last command run - in this case the command 'true', not the first command run (false).

    Quote Originally Posted by prodigy_ View Post
    Code:
    awk '/one/ { print "one five" }' testfile 2>/dev/null
    I haven't had much occasion to use awk, but as far as I can tell, this finds the word 'one' in the file testfile and if successful, prints "one five". I appreciate the effort, but this appears to have nothing to do with combining the output of 2 programs with no newline.

    Quote Originally Posted by schragge View Post
    From the POSIX standard:So, this one should work, too.
    Code:
    tmp=`grep -o one testfile` && echo $tmp five
    Same as the first example, && only handles the last command's exit code, and setting a variable to a command's output appears to remove all exit codes except the last one.

    You guys are focusing too much on how many different ways a file can be read (with grep, with awk) and missing the point. Let me try a different example:

    Code:
    ls -1 | head -1; [[ ${PIPESTATUS[0]} -eq 0 ]] && echo ' <-- this is a file or dir'
    This will print '' <-- this is a file or dir'' ONLY IF the ls command (not the head command) is successful. The only problem is that the output is on 2 different lines because a newline is created at ";". I need the output to be on the same line.
    Last edited by terminator14; March 16th, 2013 at 11:08 PM.

  6. #6
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Remove newline between commands

    Quote Originally Posted by terminator14 View Post
    combining the output of 2 programs with no newline
    Code:
    #!/bin/bash
    a=$(command 1)
    if [ "$?" == "0" ]; then
        b=$(command 2)
        echo $a $b | tr '\n' ' '
    fi
    Last edited by prodigy_; March 16th, 2013 at 11:31 PM.

  7. #7
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: Remove newline between commands

    The only working solution I've found is to run the original command twice:

    Code:
    tmp=$(ls -1 2>&1 | head -1); echo -n $tmp; ls -1 | head -1 &>/dev/null; [[ ${PIPESTATUS[0]} -eq 0 ]] && echo ' <-- this is a file or dir'
    But not only is it running the command twice (which is dumb), it's also needlessly complicated and ugly.

  8. #8
    Join Date
    May 2008
    Location
    Edmonton, Canada
    Beans
    Hidden!

    Re: Remove newline between commands

    Quote Originally Posted by prodigy_ View Post
    Code:
    #!/bin/sh
    a=$(command 1)
    b=$(command 2)
    echo $a $b | tr '\n' ' '
    echo
    Code:
    a=$(ls -1 | head -1)
    b=$(echo ' <-- this is a file or dir')
    echo $a $b
    This does in fact work, but the

    Code:
    a=$(ls -1 | head -1)
    part doesn't allow the exit code of the first part of the pipe (ls -1) to be viewed. So:

    Code:
    a=$(ls -1 | head -1)
    EXIT_CODE=${PIPESTATUS[0]}
    b=$(echo ' <-- this is a file or dir')
    echo $a $b
    echo $EXIT_CODE
    will always produce an EXIT_CODE variable of 0, no matter if 'ls -1' exited with an exit code of 0 or not.
    Last edited by terminator14; March 16th, 2013 at 11:34 PM.

  9. #9
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Remove newline between commands

    Code:
    lsout=`ls -1`; lsstat=$?; headout=`head -1 <<<"$lsout"`; ((lsstat))||echo "$headout <-- this is a file or dir"
    Last edited by schragge; March 17th, 2013 at 12:58 AM.

  10. #10
    prodigy_ is offline May the Ubuntu Be With You!
    Join Date
    Mar 2008
    Beans
    1,219

    Re: Remove newline between commands

    First, $PIPESTATUS doesn't work the way you think it works. Second, if you want to check the exist status of a command, you should run the command, (optionally) assign its output to a variable and check $?. Any post-processing you can do on the contents of the variable. See my previous post.

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
  •