Operating System - HP-UX
1832069 Members
2824 Online
110034 Solutions
New Discussion

Re: value assignment problem in script

 
SOLVED
Go to solution
Franky Leeuwerck_1
Regular Advisor

value assignment problem in script

Hi everybody,

Best wishes for 2004 to all.

I have a small problem with the assignment of a value to a script variable.

When running the following expression in the shell :
echo "Dec 9 09" | awk '{printf "%s %2s %s", $1,$2,$3}'
I get the correct output :
Dec 9 09
Notice that there are 2 spaces between 'Dec' and '9' in the output.

However, running the statement in a script statement like this :
datum=`echo "Dec 9 09" | awk '{printf "%s %2s %s", $1,$2,$3}'`
echo $datum
Then I get this output :
Dec 9 09
Notice there is only 1 space between 'Dec' and '9' now.

Any suggestions ?

Thanks in advance,
Franky
3 REPLIES 3
john korterman
Honored Contributor
Solution

Re: value assignment problem in script

Hi,
try
# echo "$datum"

regards,
John K.
it would be nice if you always got a second chance
Elmar P. Kolkman
Honored Contributor

Re: value assignment problem in script

The problem with the ITRC is that multiple spaces are replaced by a single one, so it is not visible to us.

The problem is with the shell and the echo command...
Your variable datum is containing the correct value, but:
echo $datum
will be written out by the shell to
echo Dec 9 09
(with two spaces between Dec and 9)
which means echo will get 3 arguments: 'Dec', '9' and '09'.
Now echo displays the arguments with only 1 space between them. If you changed the 2 to a 3 in your printf statement, the result would have been the same.

To solve your problem, add quotes to the echo statement, to have the contents of $datum being one argument for echo:
echo "$datum"
Every problem has at least one solution. Only some solutions are harder to find.
Franky Leeuwerck_1
Regular Advisor

Re: value assignment problem in script

Thanks!

That is the solution for me !

Franky