help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Simple echo is printing the wrong value


From: Bill Gradwohl
Subject: [Help-bash] Simple echo is printing the wrong value
Date: Tue, 3 Apr 2012 20:46:25 -0600

I can't explain the first output line of every loop, especially when compared with the second of every loop.

The lengths are correct, but the first line gives the wrong value of x.

Having the file * appear sorted above dot was also a surprise. I routinely nuked the top 2 lines for certain purposes assuming they were always dot and double dot. Now I know that's wrong.

#!/bin/bash

mkdir ./notEmpty

touch ./notEmpty/*
touch ./notEmpty/notAsterisk
ls -al ./notEmpty

declare loopPass=0
for x in ./notEmpty/*; do
   ((++loopPass))
   echo Length of x=${#x}
   echo Loop 1 pass ${loopPass} discovered - ${x}
   echo Loop 1 pass ${loopPass} discovered - '<'${x}'>'
done
echo
cd notEmpty
loopPass=0
for x in ./*; do
   ((++loopPass))
   echo Length of x=${#x}
   echo Loop 2 pass ${loopPass} discovered - ${x}
   echo Loop 2 pass ${loopPass} discovered - '<'${x}'>'
done
echo
loopPass=0
for x in *; do
   ((++loopPass))
   echo Length of x=${#x}
   echo Loop 3 pass ${loopPass} discovered - ${x}
   echo Loop 3 pass ${loopPass} discovered - '<'${x}'>'
done
echo
loopPass=0
for x in "*"; do
   ((++loopPass))
   echo Length of x=${#x}
   echo Loop 4 pass ${loopPass} discovered - ${x}
   echo Loop 4 pass ${loopPass} discovered - '<'${x}'>'
done
echo
cd ..
rm -rf ./notEmpty


Produces:
address@hidden Testing# ./junk
total 8
-rw-r--r--. 1 root root    0 Apr  3 20:38 *
drwxr-xr-x. 2 root root 4096 Apr  3 20:38 .
drwxr-xr-x. 3 root root 4096 Apr  3 20:38 ..
-rw-r--r--. 1 root root    0 Apr  3 20:38 notAsterisk
Length of x=12
Loop 1 pass 1 discovered - ./notEmpty/* ./notEmpty/notAsterisk
Loop 1 pass 1 discovered - <./notEmpty/*>
Length of x=22
Loop 1 pass 2 discovered - ./notEmpty/notAsterisk
Loop 1 pass 2 discovered - <./notEmpty/notAsterisk>

Length of x=3
Loop 2 pass 1 discovered - ./* ./notAsterisk
Loop 2 pass 1 discovered - <./*>
Length of x=13
Loop 2 pass 2 discovered - ./notAsterisk
Loop 2 pass 2 discovered - <./notAsterisk>

Length of x=1
Loop 3 pass 1 discovered - * notAsterisk
Loop 3 pass 1 discovered - <*>
Length of x=11
Loop 3 pass 2 discovered - notAsterisk
Loop 3 pass 2 discovered - <notAsterisk>

Length of x=1
Loop 4 pass 1 discovered - * notAsterisk
Loop 4 pass 1 discovered - <*>


BTW - If I add the length information to the echo that is printing the wrong value of X, the output changes for some of them, but not all.

--
Bill Gradwohl


reply via email to

[Prev in Thread] Current Thread [Next in Thread]