conditional expression bafflingly doesn't work (sh-compatible shells)

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Ronan Mac Gora » Mon, 10 May 1999 04:00:00



Hi all.  

I have a problem with a shell script which has really stumped me.
The problem is so perplexing that I suspect that I am missing something
really obvious.  Even so, I'm willing to bear the embarrassment of
having that thing pointed out to me in a public forum.

I don't think it's a bug in the shell, because I've tested the script
with 3 different shells.

At the end of the message I show 2 scripts.  The problem is in the first.  
The second is just to test it.

The script is intended to be run from the .bash_profile, and run only
once for each xsession.  (That is, I don't want a separate invocation
for each xterm).  That is the reason for lines 1 to 5.

The basic idea is that I am going to start up a particular process for
each xterm window, and put the process number of each process as a
filename in ~/m/.procdir.  In other words, if the number of the process
is 315, there will be a file in ~/m/.procdir named 315.  Lines 10 and
11 of script 2 take care of this.

Script 1 loops infinitely.  In the loop, it sleeps for a period, then
sends signal 16 to all the processes which have left a filename in
~/m/.procdir.

When script 1 starts, script 2 won't yet have started.  So the conditional
statement on line 14 checks to see if there are any processes to send
signals to, and if not, it doesn't enter the body of the if-statement.

Here is the problem: the body of that if-statement never gets executed,
i.e.  "inside" never gets printed.  (The echo statements are there only for
the purpose of debugging).

I simply cannot figure out why this is.

Notes:

As I said, script 2 is just for debugging (although it forms a skeleton
for the program that script 1 will be sending signals to).  It echoes
its arguments when sent a signal.  This is so that I can start up several
instances of the program, with different command-line arguments, so that
I know which process is responding to the signal.

I know the basic idea works, because I've tested the response of script 2
manually.

If anyone wants to know why I am doing this, or thinks it is relevant to
the problem, I can supply more details.

I'm using bash, but I think there is little or nothing here which is
bash-specific.

Thanks for any and all help.

Ronan.

--------------------------------------------------
  1     trap 'rm ~/.DDD; exit 0' 1 15
  2    
  3     test -f ~/.DDD && exit 0
  4    
  5     > ~/.DDD
  6    
  7     cd ~/m/.procdir
  8    
  9     while :; do
 10       proclist=*
 11    
 12       echo proclist is $proclist
 13    
 14       if [ "$proclist" != '*' ]; then
 15         echo "inside"
 16         kill -16 $proclist
 17       fi
 18    
 19       sleep 1
 20     done
--------------------------------------------------
  1     args="$*"
  2    
  3     function f {
  4       echo $args
  5     }
  6    
  7     trap f 16
  8     trap 'rm $$; exit 0' 1 15
  9    
 10     cd ~/m/.procdir
 11     > $$
 12    
 13     while :; do
 14       :
 15     done
--------------------------------------------------

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Sreenivas Kothapall » Mon, 10 May 1999 04:00:00




: I have a problem with a shell script which has really stumped me.
<snip>
: Here is the problem: the body of that if-statement never gets executed,
: i.e.  "inside" never gets printed.  (The echo statements are there only
for
: the purpose of debugging).
<snip>
: --------------------------------------------------
:   1 trap 'rm ~/.DDD; exit 0' 1 15
:   2
:   3 test -f ~/.DDD && exit 0
:   4
:   5 > ~/.DDD
:   6
:   7 cd ~/m/.procdir
:   8
:   9 while :; do
:  10   proclist=*

        proclist=`ls`

:  11
:  12   echo proclist is $proclist
:  13
:  14   if [ "$proclist" != '*' ]; then

        if [ -n "$proclist" ]; then

:  15     echo "inside"
:  16     kill -16 $proclist
:  17   fi
:  18
:  19   sleep 1
:  20 done

--
S Kothapalli
------------------------------
To reply,  please remove "dont_" from my email address.

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Jan Wuyt » Tue, 11 May 1999 04:00:00



>...
>   8
>   9     while :; do
>  10       proclist=*
>  11

This assumes the * will expand to the file names in the
.procdir directory.

In the Bourne shell this is controlled by:      set -f
In the C-shell this is controlled by:           set noglob
In bash: Don't know since I don't know bash but
there has got to be a way to control this and apparently
in your case it is NOT enabled.  

Hope this helps
--

      Jan Wuyts

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by John DuBo » Tue, 11 May 1999 04:00:00




>>...
>>   8
>>   9     while :; do
>>  10       proclist=*
>>  11
>This assumes the * will expand to the file names in the
>.procdir directory.

>In the Bourne shell this is controlled by:  set -f
>In the C-shell this is controlled by:               set noglob
>In bash: Don't know since I don't know bash but
>there has got to be a way to control this and apparently
>in your case it is NOT enabled.  

Even with globbing turned on, proclist=* won't do what he wants.
You need to do e.g.:

set -- *
proclist=$*

        John
--

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Jan Wuyt » Wed, 12 May 1999 04:00:00



>...

> Even with globbing turned on, proclist=* won't do what he wants.
> You need to do e.g.:

I'm sorry but on Solaris 2.4, 2.5 and HP-UX 10.20
the C-shell shows (transcript on HP-UX)
---------
35 bthh21:wuytsj> cd /tmp
36 bthh21:wuytsj> set brol=*
37 bthh21:wuytsj> echo $brol
26045.ps 7012.ps 7808.ps 85.sh 8603.ps flexlmdaemon.log last_uuid
uom.tar
---------
The bourne/Korn shell shows: (transcript on Solaris)
---------
$ cd /tmp
$ brol=*
$ echo $brol
dev ps ps1 ps_data reboot
$
---------

So, if "proclist=* won't do what he wants", its got to be a 'bash'
'feature' ;-) but as I said, I don't know bash.

Cheers

--

      Jan Wuyts

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Ken Pizzi » Wed, 12 May 1999 04:00:00



>> Even with globbing turned on, proclist=* won't do what he wants.
>> You need to do e.g.:

>I'm sorry but on Solaris 2.4, 2.5 and HP-UX 10.20
>the C-shell shows (transcript on HP-UX)
>---------
>35 bthh21:wuytsj> cd /tmp
>36 bthh21:wuytsj> set brol=*
>37 bthh21:wuytsj> echo $brol
>26045.ps 7012.ps 7808.ps 85.sh 8603.ps flexlmdaemon.log last_uuid
>uom.tar
>---------
>The bourne/Korn shell shows: (transcript on Solaris)
>---------
>$ cd /tmp
>$ brol=*
>$ echo $brol
>dev ps ps1 ps_data reboot
>$
>---------

>So, if "proclist=* won't do what he wants", its got to be a 'bash'
>'feature' ;-) but as I said, I don't know bash.

Your tests are poorly constructed.  For example, try this
Bourne shell script (with true Bourne, ksh, bash, zsh, ash,
or whatever other Bourneish shell you like):
   brol=*
   cd /tmp;  echo $brol
   cd $HOME; echo $brol
   echo "$brol"

Note how this is much different from the csh/tcsh interpretation
(which better matches the original intent in this thread, at least
as I understood the intent):
   set brol=*
   cd /tmp;  echo $brol
   cd $HOME; echo $brol
   echo "$brol"

                --Ken Pizzini

 
 
 

conditional expression bafflingly doesn't work (sh-compatible shells)

Post by Dan Merc » Wed, 12 May 1999 04:00:00





>>...

>> Even with globbing turned on, proclist=* won't do what he wants.
>> You need to do e.g.:

> I'm sorry but on Solaris 2.4, 2.5 and HP-UX 10.20
> the C-shell shows (transcript on HP-UX)
> ---------
> 35 bthh21:wuytsj> cd /tmp
> 36 bthh21:wuytsj> set brol=*
> 37 bthh21:wuytsj> echo $brol

That's because

   echo $brol

is evaluated as

   echo *

If you:

   echo "$brol"

you will get "*" as a response.  If you still don't believe me,
try the following:

   cd /tmp
   mkdir A B
   touch A/abc A/def B/ghi C/jkl
   cd A
   brol=*
   cd ../B
   echo $brol

Now,  if the "brol=*" worked,  echo $brol should give you "abc def".
It will,  however,  give you "ghi jkl".

--
Dan Mercer

Quote:> 26045.ps 7012.ps 7808.ps 85.sh 8603.ps flexlmdaemon.log last_uuid
> uom.tar
> ---------
> The bourne/Korn shell shows: (transcript on Solaris)
> ---------
> $ cd /tmp
> $ brol=*
> $ echo $brol
> dev ps ps1 ps_data reboot
> $
> ---------

> So, if "proclist=* won't do what he wants", its got to be a 'bash'
> 'feature' ;-) but as I said, I don't know bash.

> Cheers

Opinions expressed herein are my own and may not represent those of my employer.
 
 
 

1. Using 'sh' shell script and conditionals

Running: sh -x myscript
with the line if [ -s file-diff ]
then
        some commands
else
fi

when the diff file IS empty, it still goes to then

I then tried to change strategy with: until [ file-diff = 0 ]
do
        some commands
done

Still no luck.  It continues down to "do".

I've tried until [ file-old = file-new ] (where new copy could be
identical to the old), etc.

How can I either:

set up a conditional where it WILL see that either

        - the contents of the files are identical (i.e. file-old =
file-new)

        or

        - the file-diff, which would be zero bytes due to no difference is
equal to zero

in either case, forcing the loop to stop at the UNTIL and never reach DO
until the either the diff is greater than zero length or showing the old
and new files are different?

Thanks much.

--
-------------------------

Scott Ehrlich Consulting                http://www.shore.net/~sehrlich  
Amateur Radio Callsign: wy1z

2. Remote install on a cycle 5 (Sparc clone)

3. Makefile doesn't work when SHELL=/bin/sh

4. Where to buy PowerPoint filter for Applix?

5. Question on conditional expressions in Bourne shell acripts

6. AMD K62, Celeron, compatibliltiy and Celeron Overclocking

7. Problem using grep through the unix shell: doesn't accept a regular expression in filename

8. Unziping files ? how do you do it?

9. Why doesn't this work (sh) and what's the (t)csh version?

10. Re. bffcreate -X doesn't work Re: bffcreate -X Doesn't Work

11. ATI Expression STILL doesn't work

12. ``M-x shell'' command under emacs doesn't quite work.

13. 'more' doesn't work with non-login shells