Re: Bash script Slack >> Deb-Etch



On 2006-11-07, Handover Phist <jason@xxxxxxxxxxxxxxxxxxxxxx> wrote:
On 2006-11-07, Dean G. <dguttadauro@xxxxxxxx> wrote:

Handover Phist wrote:
I have a million little simple scripts doing chores for me, and I've
just switched a box from Slackware 11.0 to Debian Etch. The script in
question is this:

#!/bin/bash
LENGTH=6
while [ ${LENGTH} -ge 5 ]
do
/usr/games/fortune -aes > ~/.signature
echo >> ~/.signature
echo "http://www.websterscafe.com"; >> ~/.signature
LENGTH=`wc -l ~/.signature | cut -d " " -f 1`
done
exit 0


It worked in Slack, but in Etch repeats "wc -l ~/.signature | cut -d " "
-f 1" endlessly. I have no idea why, as the versions of BASH are the
same.

It is probably an issue with how cut works on the different distros.
Try

LENGTH=`wc -l ~/.signature | cut -d " " -wF -f 2`

What seems to be happening is that wc -l is putting preceding spaces in
the output, and cut is counting each one as a delimiter. The -wF treats
all whitespace as a delimter, and folds adjacent delimiters together.

Dean G.

You're onto something there, sorta. -wF chokes cut, and the switch isn't
mentioned in the man page, but changing -f 1 to -f 2 seems to produce
output. Now it seems the while statement is broken:

#!/bin/bash -v
LENGTH=6
while [ ${LENGTH} -ge 5 ]
do
/usr/games/fortune -aes > ~/.signature
echo >> ~/.signature
echo "http://www.websterscafe.com"; >> ~/.signature
LENGTH=`wc -l ~/.signature | cut -d " " -f 2`
done
wc -l ~/.signature | cut -d " " -f 2
bin/sig.gen: line 3: [: /home/jason/.signature: integer expression expected
exit 0

OK, I'm a bit silly. the -f 1 is there to return an integer.

jason@jason:~$ wc -l ~/.signature | cut -d " " -f 1
5
jason@jason:~$

That's tested to keep my sig 4 lines or shorter. Somehow in the script
${LENGTH} isn't being properly set, and the while loop is buggered.

--
.



Relevant Pages