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
...
-
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
-
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
-
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
-
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
-
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