On 01/03/2012 08:15 AM, lina wrote:
When I derive it to a field problem,
$ cat dm_1.xpm | while read line ; do IFS=$'\n' ; if [ ${#line} == 30 ];
then echo $line ; fi ; done
Useless use of cat. Use of $'\n', as well as == within [, are bash
extensions; there's talk of standardizing both of them in the next
version of POSIX, but they isn't portable yet. I have no idea why you
are changing $IFS, nor why you forget to reset it later, as it doesn't
affect anything in your 2-line sample; but assuming it was important,
I'll keep it as you wrote it. Also, you had insufficient quoting; you
probably want echo "$line" rather than echo $line. I would have written
this as:
while read line; do
IFS='
'
if [ ${#line} = 30 ]; then
echo "$line";
fi
done< dm_1.xpm
if I use:
$ cat dm_1.xpm | while read line ; do IFS=$'\n' ; if [ ${#line} == 30 -a
${line:0:1}== '"' ]; then echo $line ; fi ; done
bash: [: too many arguments
That's because you are invoking [ incorrectly - operators MUST be
separated by spaces, but since you joined ${line:0:1} and == as a single
argument, you ended up causing a syntax error. Also, [ -a is not
portable; POSIX explicitly recommends against using it. And ${line:0:1}
is a bash extension; you can portably get the same effect of comparing
the first byte via a case statement. I think you either want to fix
your version:
cat dm_1.xpm | while read line ; do IFS=$'\n' ;
if [ ${#line} == 30 -a ${line:0:1} == '"' ]; then echo $line ; fi ; done