I wrote:
> I am running Solaris 10 on a Sun SPARC system.
>
> I have a problem testing strings for equality within a Korn shell script
> and am hoping someone knows the cause of the problem and a simple fix.
>
> Consider two directories. One is empty and the other contains the following
> three files:
>
> PICT_01.jpg
> PICT_02.jpg
> PICT_03.jpg
>
> Here is a short ksh script:
>
> #! /bin/ksh
> LIST="PICT*"
> echo ${LIST}
> if [[ "${LIST}" = 'PICT*' ]] ; then
> echo "No files found."
> else
> echo "OK. Process files ..."
> fi
>
> When making the assignment LIST="PICT*", this will return 'PICT*' in the
> empty directory and 'PICT_01.jpg PICT_02.jpg PICT_03.jpg' in the directory
> with the three files. This is confirmed by echo on the third line of
And Barry Margolin <
bar...@alum.mit.edu> responded:
> No, this doesn't happen during the assignment. The wildcard expansion
> happens on the echo line, because you didn't quote the variable. Two
> transformations are done after unquoted variable expansion: wildcards
> and word splitting.
>
> If you do:
>
> echo "${LIST}"
>
> it should print 'PICT*' in both directories, proving that the assignment
> was the same.
>
> Try:
>
> LIST=$(echo PICT*)
Barry:
Thank you for the great information.
So filename expansion does not occur during variable assignment! After
30 years of UNIX programming, this is news to me. This explains a lot of
past confusion I have had with scripts. Where is this documented? I just
searched through my Kornshell book and the ksh(1) manual page and found no
discussion of this. It was always my understanding that expansion took
place when either freely expressed or within double quotes ("..."), and was
suppressed within single quotes ('...') and when the special character(s)
were escaped. What are the precise rules for expansion? I'm still finding
inconsistencies. For example, although tilde also modifies filename paths,
tilde expansion does not work the same way file expansion does.
In my directory with the three PICT* files, I just tried the following:
X="PICT*"
echo "$X" produces: PICT*
echo '$X' produces: $X
echo $X produces: PICT_01.jpg PICT_02.jpg PICT_03.jpg
X=PICT*
echo "$X" produces: PICT*
echo '$X' produces: $X
echo $X produces: PICT_01.jpg PICT_02.jpg PICT_03.jpg
X="~"
echo "$X" produces: ~
echo '$X' produces: $X
echo $X produces: ~
X=~
echo "$X" produces: /home/jeff
echo '$X' produces: $X
echo $X produces: /home/jeff
I then created three files in $HOME: PICT_AA.jpg PICT_BB.jpg PICT_CC.jpg
# echo ~/PICT*
/u/jeff/PICT_AA.jpg /u/jeff/PICT_BB.jpg /u/jeff/PICT_CC.jpg
OK, let's try this:
X="~/PICT*"
echo "$X" produces: ~/PICT*
echo '$X' produces: $X
echo $X produces: ~/PICT*
X=~/PICT*
echo "$X" produces: /home/jeff/PICT*
echo '$X' produces: $X
echo $X produces: /home/jeff/PICT_AA.jpg \
/home/jeff/PICT_BB.jpg \
/home/jeff/PICT_CC.jpg
During variable assignment unquoted filename characters are not expanded,
but the tilde character is expanded, unless there is a double-quoted string
with both a tilde and a filename wildcard character, in which then neither
is expanded. Is there a logic to all of this?
I also want to thank David B. Chorlian who wrote directly to me and suggested
replacing the test within my original script with the following:
Why not
if ls | grep -s PICT > /dev/null
echo "OK. Process files ..."
else
echo "No files found."
fi
which is a similar solution to the one offered above by Barry. I
appreciate both responses.
Regards,
--
Jeffery Small