Carriage return in strings

  • Follow


Hello. Noob question no doubt.

I have this simple script that directs the output of a command to a
variable in posix (i'm doing this for a reason). I'd like to then print
the variable, but expect the print to look like the originally expected
output. I suspect somehow Carriage returns and/or linefeeds are lost
when when i set the variable. Here's the script:

#!/usr/bin/sh
vcdiffout=`cvs -d /avmed/cvs diff $1/$2`
count=`print $vcdiffout | wc -l`
if [ $count = 0 ]
then echo NO CVS DIFFERENCE
else
print $vcdiffout
fi


Problem is - that cvs output is sometimes multiple lines, and when it
is the print in the else shows as one line.
Thanks for any help or information.

0
Reply jason450 (43) 12/10/2004 4:02:57 PM

jason@cyberpine.com writes:

> Hello. Noob question no doubt.
>
> I have this simple script that directs the output of a command to a
> variable in posix (i'm doing this for a reason). I'd like to then print
> the variable, but expect the print to look like the originally expected
> output. I suspect somehow Carriage returns and/or linefeeds are lost
> when when i set the variable. Here's the script:
>
> #!/usr/bin/sh
> vcdiffout=`cvs -d /avmed/cvs diff $1/$2`
> count=`print $vcdiffout | wc -l`
> if [ $count = 0 ]
> then echo NO CVS DIFFERENCE
> else
> print $vcdiffout
> fi
>
>
> Problem is - that cvs output is sometimes multiple lines, and when it
> is the print in the else shows as one line.
> Thanks for any help or information.
>

You might want to use the feature of cvs diff, mentioned in its man page:

     If cvs is successful, it returns  a  successful  status;  if
     there  is an error, it prints an error message and returns a
     failure status.  The one exception to this is the  cvs  diff
     command.   It will return a successful status if it found no
     differences, or a failure status if there  were  differences
     or if there was an error. 

However, note also the following

                                Because this behavior provides no
     good way to detect errors, in the future it is possible that
     cvs  diff  will be changed to behave like the other cvs com-
     mands.


Anyway, you can always redirect the cvs diff output to a file and count the
number of lines there...

HTH, Dragan

-- 
Dragan Cvetkovic, 

To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
0
Reply Dragan 12/10/2004 4:16:12 PM


2004-12-10, 08:02(-08), jason@cyberpine.com:
> I have this simple script that directs the output of a command to a
> variable in posix (i'm doing this for a reason). I'd like to then print
> the variable, but expect the print to look like the originally expected
> output. I suspect somehow Carriage returns and/or linefeeds are lost
> when when i set the variable. Here's the script:
>
> #!/usr/bin/sh
> vcdiffout=`cvs -d /avmed/cvs diff $1/$2`
> count=`print $vcdiffout | wc -l`
> if [ $count = 0 ]
> then echo NO CVS DIFFERENCE
> else
> print $vcdiffout
> fi
[...]

Don't assume "print" to be available in "sh". print is a ksh or
zsh builtin function, so that code will only work if your sh is
a ksh (or zsh). printf is the command to use instead.

Your problem is that you forgot to quote your variables.

#! /usr/bin/sh -
vcdiffout=$(cvs -d /avmed/cvs diff "$1/$2")
  # beware that trailing empty lines are discarded
  # (that shouldn't happen for a cvs diff output though).

count=$(wc -l << EOF
$vcdiffout
EOF
)

if [ "$((count))" -eq 0 ]; then
  echo NO CVS DIFFERENCE
else
  cat << EOF
$vcdiffout
EOF

But actually, the whole script can be written as:

#! /usr/bin/sh -
cvs -d /avmed/cvs diff "$1/$2" && echo NO CVS DIFFERENCE

-- 
Stephane
0
Reply Stephane 12/10/2004 4:29:32 PM

Stephane CHAZELAS wrote:

> 2004-12-10, 08:02(-08), jason@cyberpine.com:
> 
>>I have this simple script that directs the output of a command to a
>>variable in posix ...

> Don't assume "print" to be available in "sh". print is a ksh or
> zsh builtin function  ...

For the record, he did say "... in posix ...", which presumably means in 
the posix shell.

-- 
Henry Townsend
0
Reply Heny 12/10/2004 4:41:34 PM

2004-12-10, 16:41(+00), Heny Townsend:
> Stephane CHAZELAS wrote:
>
>> 2004-12-10, 08:02(-08), jason@cyberpine.com:
>> 
>>>I have this simple script that directs the output of a command to a
>>>variable in posix ...
>
>> Don't assume "print" to be available in "sh". print is a ksh or
>> zsh builtin function  ...
>
> For the record, he did say "... in posix ...", which presumably means in 
> the posix shell.

Yes. And POSIX doesn't specify any print command, but specfifies
a printf command and an echo command (that it recommands not to
use).

Some Unices have a ksh derived shell as their /POSIX/
conformant shell, some haven't (may be a bash, zsh or ash
derived shell).

-- 
Stephane
0
Reply Stephane 12/10/2004 4:53:23 PM

4 Replies
391 Views

(page loaded in 0.061 seconds)

Similiar Articles:













7/23/2012 1:28:43 PM


Reply: