Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Problem with ksh test operator
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeffery Small  
View profile  
 More options Aug 24 2012, 11:36 am
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: j...@cjsa.com (Jeffery Small)
Date: Fri, 24 Aug 2012 15:36:51 GMT
Local: Fri, Aug 24 2012 11:36 am
Subject: Problem with ksh test operator
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
the script.  The subsequent test attempts to trap the condition where no
matching files are found.  However, this fails, and in both directories
the script reports "No files found."

The reason for this is that the ksh [[ operator treats the second string
as a pattern and not a literal string, apparently even when enclosed in
single quotes.  From the ksh(1) man page under Conditional Expressions:

        string = pattern     True, if string matches pattern.

I have tried to backslash escape the "*" in the pattern without success.
If you change the line to:

        if [[ "${LIST}" = 'PICT\*' ]] ; then

then the test always fails in both directories.  Apparently the backslash
is being taken as a literal rather than an escape character.  I have also
tried every combination of single and double quotes, with nothing working.
If there is a trick to get the [[ operator to compare two literal strings
rather than a pattern, I would really appreciate hearing about it.

So I read the test(1) manual page and it reports the following:

        string1 = string2           True if the strings string1  and
                                    string2 are identical.

Yes, this is what I want!  So I change:

        if [[ "${LIST}" = 'PICT*' ]] ; then
to
        if [ "${LIST}" = 'PICT*' ] ; then

but this changes nothing.  OK, maybe '[' is treated internal to ksh.  So
I then tried #!/bin/sh (no good) and then change the line to:

        if /bin/test "${LIST}" = 'PICT*' ; then

Nope.  How about:

        if $(/bin/test "${LIST}" = 'PICT*')  ; then

Still no success.  So what's the trick?  Do I have to write my own external
string comparison routine in some other language such as perl(1)?

This seems like such a no-brainer, but I have lost considerable time going
down dead ends here.  Doesn't this seem like a bug in the ksh handling of
the test operator -- especially when calling the external /bin/test?

Any insights would be appreciated.  Thanks.
--
Jeffery Small


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Aug 24 2012, 5:02 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: Barry Margolin <bar...@alum.mit.edu>
Date: Fri, 24 Aug 2012 17:02:55 -0400
Local: Fri, Aug 24 2012 5:02 pm
Subject: Re: Problem with ksh test operator

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 Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeffery Small  
View profile  
 More options Aug 25 2012, 6:59 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: j...@cjsa.com (Jeffery Small)
Date: Sat, 25 Aug 2012 22:59:45 GMT
Local: Sat, Aug 25 2012 6:59 pm
Subject: Re: Problem with ksh test operator

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Aug 25 2012, 8:24 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: Barry Margolin <bar...@alum.mit.edu>
Date: Sat, 25 Aug 2012 20:24:03 -0400
Local: Sat, Aug 25 2012 8:24 pm
Subject: Re: Problem with ksh test operator

In article <M9C2JL....@cjsa.com>, j...@cjsa.com (Jeffery Small) wrote:
> 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?

Variables often hold pathnames, it's common to want to use tilde there.
But good programming practice is to quote these variables when you use
them (in case the filename contains whitespace or wildcards), so the
tilde has to be expanded during the assignment.

And for the sake of variables like PATH, which contain a list of
colon-separated pathnames, tilde is expanded either at the beginning of
the value or immediately following ':'.

I'm not sure why wildcards aren't expanded during assignment, I guess
it's just a decision Bourne made, and we're stuck with it for
compatibility.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeffery Small  
View profile  
 More options Aug 26 2012, 2:33 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: j...@cjsa.com (Jeffery Small)
Date: Sun, 26 Aug 2012 18:33:23 GMT
Local: Sun, Aug 26 2012 2:33 pm
Subject: Re: Problem with ksh test operator

Thanks for all ther information, Barry.

Regards,
--
Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Combs  
View profile  
 More options Sep 2 2012, 11:26 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: dkco...@panix.com (David Combs)
Date: Mon, 3 Sep 2012 03:26:09 +0000 (UTC)
Local: Sun, Sep 2 2012 11:26 pm
Subject: Re: Problem with ksh test operator

1: ALL of the ksh variable-expansion stuff discussed in
   this thread; what if any parts of it do NOT work
   the same in sh (Bourne)?

2: The order in which things get expanded: I recall seeing
   a discussion of that somewhere, the rules laid out in
   detail.  But I forget where I saw it.

   Any recollection of where that might be?

Thanks!

David


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeffery Small  
View profile  
 More options Sep 3 2012, 8:09 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: j...@cjsa.com (Jeffery Small)
Date: Tue, 4 Sep 2012 00:08:43 GMT
Local: Mon, Sep 3 2012 8:08 pm
Subject: Re: Problem with ksh test operator

dkco...@panix.com (David Combs) writes:
>1: ALL of the ksh variable-expansion stuff discussed in
>   this thread; what if any parts of it do NOT work
>   the same in sh (Bourne)?

David:

I haven't done any test between sh(1) and ksh(1), but I assume that they
work similarly regarding these types of expansions.  My confusion was not
realizing that filename expansion did not occur during initial assignment.
If there are differences, I would also like to know what they are.

>2: The order in which things get expanded: I recall seeing
>   a discussion of that somewhere, the rules laid out in
>   detail.  But I forget where I saw it.

>   Any recollection of where that might be?

No, a comprehensive source for this information was never mentioned, but
I would love to see this too. I did not find it in the manual pages or in
my Korn Shell book.  Does anyone else reading this thread know of a place
online, or a book on shell programming that discusses this?

Regards,
--
Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Davies  
View profile  
 More options Sep 4 2012, 9:00 am
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: Chris Davies <chris-use...@roaima.co.uk>
Date: Tue, 04 Sep 2012 13:38:30 +0100
Local: Tues, Sep 4 2012 8:38 am
Subject: Re: Problem with ksh test operator

Jeffery Small <j...@cjsa.com> wrote:
>        Why not
>        if ls | grep -s PICT > /dev/null

You can simply that further by testing the exit status of ls directly
(no need for the grep):

    if ls -d PICT* >/dev/null 2>&1
    then
        ...

Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Sep 4 2012, 11:09 am
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: Barry Margolin <bar...@alum.mit.edu>
Date: Tue, 04 Sep 2012 11:09:19 -0400
Local: Tues, Sep 4 2012 11:09 am
Subject: Re: Problem with ksh test operator

In article <M9stqJ....@cjsa.com>, j...@cjsa.com (Jeffery Small) wrote:
> dkco...@panix.com (David Combs) writes:
> >2: The order in which things get expanded: I recall seeing
> >   a discussion of that somewhere, the rules laid out in
> >   detail.  But I forget where I saw it.

> >   Any recollection of where that might be?

> No, a comprehensive source for this information was never mentioned, but
> I would love to see this too. I did not find it in the manual pages or in
> my Korn Shell book.  Does anyone else reading this thread know of a place
> online, or a book on shell programming that discusses this?

I haven't gone through it enough to tell how clear it is, but I expect
the POSIX Shell specification describes it:

http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeffery Small  
View profile  
 More options Sep 4 2012, 11:18 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: j...@cjsa.com (Jeffery Small)
Date: Wed, 5 Sep 2012 03:17:58 GMT
Local: Tues, Sep 4 2012 11:17 pm
Subject: Re: Problem with ksh test operator

Barry, thanks for the link.  I'll take a closer look at this.

Regards,
--
Jeff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Combs  
View profile  
 More options Sep 18 2012, 10:15 pm
Newsgroups: comp.unix.solaris, comp.unix.misc, comp.unix.questions
From: dkco...@panix.com (David Combs)
Date: Wed, 19 Sep 2012 02:15:41 +0000 (UTC)
Local: Tues, Sep 18 2012 10:15 pm
Subject: Re: Problem with ksh test operator

It *may* have been something by Kernighan.  I'll have to go look.

David


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »