Different behaviour - Unix

This is a discussion on Different behaviour - Unix ; I know that maybe this is a stupid question, but I cannot understand why this 2 commands produces different output: script1.sh #!/usr/bin/sh ls -l /tmp exit 0 .... and ... script2.sh #!/usr/bin/sh cmd="ls -l /tmp" output=`$cmd` echo $output exit 0 ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: Different behaviour

  1. Different behaviour

    I know that maybe this is a stupid question, but I cannot understand
    why this 2 commands produces different output:

    script1.sh
    #!/usr/bin/sh
    ls -l /tmp
    exit 0

    .... and ...

    script2.sh
    #!/usr/bin/sh
    cmd="ls -l /tmp"
    output=`$cmd`
    echo $output
    exit 0

    How is possible to have the same output from script2.sh with \n and
    whatelse?!?

    Thanks in advance,
    Stefano


  2. Re: Different behaviour

    2006-11-2, 08:22(-08), magnanel@gmail.com:
    [...]
    > script1.sh
    > #!/usr/bin/sh
    > ls -l /tmp
    > exit 0
    >
    > ... and ...
    >
    > script2.sh
    > #!/usr/bin/sh
    > cmd="ls -l /tmp"
    > output=`$cmd`
    > echo $output
    > exit 0
    >
    > How is possible to have the same output from script2.sh with \n and
    > whatelse?!?

    [...]

    script2.sh
    #! /usr/bin/sh -
    cmd() { ls -l /tmp; }
    output=$(cmd; echo .)
    output=${output%.}
    printf %s "$output"

    (assuming /usr/bin/sh is a POSIX shell).

    --
    Stéphane

  3. Re: Different behaviour

    On 2006-11-02, magnanel@gmail.com wrote:
    > I know that maybe this is a stupid question, but I cannot understand
    > why this 2 commands produces different output:
    >
    > script1.sh
    > #!/usr/bin/sh
    > ls -l /tmp
    > exit 0
    >
    > ... and ...
    >
    > script2.sh
    > #!/usr/bin/sh
    > cmd="ls -l /tmp"
    > output=`$cmd`
    > echo $output
    > exit 0
    >
    > How is possible to have the same output from script2.sh with \n and
    > whatelse?!?


    echo "$output"

    Or, preferably:

    printf "%s\n" "$output"

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  4. Re: Different behaviour

    2006-11-2, 11:43(-05), Chris F.A. Johnson:
    > On 2006-11-02, magnanel@gmail.com wrote:
    >> I know that maybe this is a stupid question, but I cannot understand
    >> why this 2 commands produces different output:
    >>
    >> script1.sh
    >> #!/usr/bin/sh
    >> ls -l /tmp
    >> exit 0
    >>
    >> ... and ...
    >>
    >> script2.sh
    >> #!/usr/bin/sh
    >> cmd="ls -l /tmp"
    >> output=`$cmd`
    >> echo $output
    >> exit 0
    >>
    >> How is possible to have the same output from script2.sh with \n and
    >> whatelse?!?

    >
    > echo "$output"
    >
    > Or, preferably:
    >
    > printf "%s\n" "$output"


    Note that `$cmd` will remove *every* trailing newline character
    from the output of $cmd, while printf '%s\n' will restore only
    one.

    Hence the need of:

    output=`$cmd; echo .`
    output=${output%.}
    printf %s "$output"

    to work around it.


    --
    Stéphane

  5. Re: Different behaviour

    On 2006-11-02, Stephane CHAZELAS wrote:
    > 2006-11-2, 11:43(-05), Chris F.A. Johnson:
    >> On 2006-11-02, magnanel@gmail.com wrote:
    >>> I know that maybe this is a stupid question, but I cannot understand
    >>> why this 2 commands produces different output:
    >>>
    >>> script1.sh
    >>> #!/usr/bin/sh
    >>> ls -l /tmp
    >>> exit 0
    >>>
    >>> ... and ...
    >>>
    >>> script2.sh
    >>> #!/usr/bin/sh
    >>> cmd="ls -l /tmp"
    >>> output=`$cmd`
    >>> echo $output
    >>> exit 0
    >>>
    >>> How is possible to have the same output from script2.sh with \n and
    >>> whatelse?!?

    >>
    >> echo "$output"
    >>
    >> Or, preferably:
    >>
    >> printf "%s\n" "$output"

    >
    > Note that `$cmd` will remove *every* trailing newline character
    > from the output of $cmd, while printf '%s\n' will restore only
    > one.
    >
    > Hence the need of:
    >
    > output=`$cmd; echo .`
    > output=${output%.}
    > printf %s "$output"
    >
    > to work around it.


    Which will make no difference unless the last file name listed ends
    with one or more newlines. While it is a good technique to know,
    99% of the time it is unnecessary.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

+ Reply to Thread