Bash: Variable type confusion
kwall@kurtwerks.com
kwall
Mon May 17 11:39:10 PDT 2004
On Sun, Oct 20, 2002 at 04:53:00PM -0400, Joel Hammer wrote:
> I have always thought that bash does the right thing when it evaluates
> variables.
> Until today.
> This bit of script works as expected:
> k=0
> for i in `dir -1 *jpg`
> do
> k=$((k+1))
> echo $k
> done
>
> It prints out the numbers from 1 to whatever the number of jpg's in the
> directory.
>
> However, this fails:
> k=0
> for i in `dir -1 *jpg`
> do
> k=$((k+1))
> [ "$k" -eq 10 ] && k=x <---The problem.
> echo $k
> done
>
> Here, it prints out 1 to 9 and then every tenth item is an x, followed by 1
> to 9 again.
Correct. [ "$k" -eq ] evaluates to true, so statement following
the && gets executed.
> What I am trying to do is put leading zeroes on my numbers less that 10.
> Attempting that, with [ "$k" -lt 10 ] && k=0"$k" give a base error at 8.
Try this:
----- cut here -----
k=0
for i in `dir -1 *`
do
k=$((k+1))
[ $k -lt 10 ] && echo "0"$k || echo $k
done
----- cut here -----
$ ./x.sh
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
$
> Any insight appreciated.
>
> Joel
>
>
> _______________________________________________
> Linux-users mailing list
> Linux-users at linux-sxs.org
> Unsubscribe/Suspend/Etc -> http://www.linux-sxs.org/mailman/listinfo/linux-users
--
Schnuffel, n.:
A dog's practice of continuously nuzzling in your crotch in
mixed company.
-- Rich Hall, "Sniglets"
More information about the Linux-users
mailing list