sh script problem

sh script problem

Post by CTE » Fri, 23 May 2003 05:53:26



I wrote a shell script to be a CGI script.
It takes all the pix in a specified directory, then output an HTML file
with each pictrue in "img" tags.
I use
set $(ls -1 $wwwdir/$pixdir)
where wwwdir is the website's root dir and pixdir is the directory the
pix are in.
That makes variables 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
one for each filename.
In the loop where it outputs IMG tags, it increments the variable number
by doing
x=$((x+1))
it then gets the current filename by:
eval curr='$'$x
it tells when it's at the end by using
if [ $curr != "" ]

it works fine with 1-8 pix
but 9 and up is bad
can you see the problem?

here it is:
With only 8, when it tries 9, that is empty because there is no such
variable.
But say theres 15. When it gets to 16 it basically takes the 1 and
interprets that as a variable and appends a 6.
How can I trap this, because it currently runs and endless loop and
hitting my server over and over with requests for nonexistant pix.
--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by Bit Twiste » Fri, 23 May 2003 06:10:35



> I wrote a shell script to be a CGI script.
> It takes all the pix in a specified directory, then output an HTML file
> with each pictrue in "img" tags.
> I use
> set $(ls -1 $wwwdir/$pixdir)
> where wwwdir is the website's root dir and pixdir is the directory the
> pix are in.
> That makes variables 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
> one for each filename.
> In the loop where it outputs IMG tags, it increments the variable number
> by doing
> x=$((x+1))
> it then gets the current filename by:
> eval curr='$'$x
> it tells when it's at the end by using
> if [ $curr != "" ]

> it works fine with 1-8 pix
> but 9 and up is bad
> can you see the problem?

I would use a different method, but if you get yours to work
then it works. It always helps to figger out where your wheel
falls off though.

To answery your question, create a little script for testing
and try adding something like this inside your loop.

  echo $1
  shift

 
 
 

sh script problem

Post by Vilmos Sot » Fri, 23 May 2003 06:13:10



> I wrote a shell script to be a CGI script.
> It takes all the pix in a specified directory, then output an HTML
> file with each pictrue in "img" tags.
> I use
> set $(ls -1 $wwwdir/$pixdir)
> where wwwdir is the website's root dir and pixdir is the directory the
> pix are in.

That seems convoluted. Wy don't you use a "for pix in dir/*"; do ... done
construct?

Quote:> In the loop where it outputs IMG tags, it increments the variable
> number by doing
> x=$((x+1))

You forgot an '$' from before the second x. It seems just like a typo.

Vilmos

 
 
 

sh script problem

Post by Wayne Polloc » Fri, 23 May 2003 06:20:08


What you need is "shift".  Each time through your loop, use "$1" so
you can eliminate that eval statement and the x=x+1 statement.
This is a standard situation with a standard solution:

        set -- $(ls -1 $wwwdir/$pixdir)
        while test "$1"
        do
                curr=$1
                ...
                shift
        done

The problem is that when there are more than 9 arguments positional
parameter 9 ("$9") gets set to all remaining arguments.  By using shift
the problem is avoided, since every call to shift re-assigns the
positional parameters by shifting one to the left.  So the old $2
becomes the new $1, the old $3 becomes the new $2, and so on.

Hope this helps!

-Wayne


> I wrote a shell script to be a CGI script.
> It takes all the pix in a specified directory, then output an HTML file
> with each pictrue in "img" tags.
> I use
> set $(ls -1 $wwwdir/$pixdir)
> where wwwdir is the website's root dir and pixdir is the directory the
> pix are in.
> That makes variables 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
> one for each filename.
> In the loop where it outputs IMG tags, it increments the variable number
> by doing
> x=$((x+1))
> it then gets the current filename by:
> eval curr='$'$x
> it tells when it's at the end by using
> if [ $curr != "" ]

> it works fine with 1-8 pix
> but 9 and up is bad
> can you see the problem?

> here it is:
> With only 8, when it tries 9, that is empty because there is no such
> variable.
> But say theres 15. When it gets to 16 it basically takes the 1 and
> interprets that as a variable and appends a 6.
> How can I trap this, because it currently runs and endless loop and
> hitting my server over and over with requests for nonexistant pix.
> --
> Chris Engel
> CTE Net
> http://ctenet.cjb.net/


 
 
 

sh script problem

Post by CTE » Fri, 23 May 2003 06:45:35




>>I wrote a shell script to be a CGI script.
>>It takes all the pix in a specified directory, then output an HTML file
>>with each pictrue in "img" tags.
>>I use
>>set $(ls -1 $wwwdir/$pixdir)
>>where wwwdir is the website's root dir and pixdir is the directory the
>>pix are in.
>>That makes variables 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
>>one for each filename.
>>In the loop where it outputs IMG tags, it increments the variable number
>>by doing
>>x=$((x+1))
>>it then gets the current filename by:
>>eval curr='$'$x
>>it tells when it's at the end by using
>>if [ $curr != "" ]

>>it works fine with 1-8 pix
>>but 9 and up is bad
>>can you see the problem?

> I would use a different method, but if you get yours to work
> then it works. It always helps to figger out where your wheel
> falls off though.

> To answery your question, create a little script for testing
> and try adding something like this inside your loop.

>   echo $1
>   shift

shift! Thats it!
I'll use that to switch variables and then just use $0.
(I do already spit out debug info like that though.)

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by CTE » Fri, 23 May 2003 06:46:53




>>I wrote a shell script to be a CGI script.
>>It takes all the pix in a specified directory, then output an HTML
>>file with each pictrue in "img" tags.
>>I use
>>set $(ls -1 $wwwdir/$pixdir)
>>where wwwdir is the website's root dir and pixdir is the directory the
>>pix are in.

> That seems convoluted. Wy don't you use a "for pix in dir/*"; do ... done
> construct?

Good idea. Ill use it if I have trouble with Sybren's idea.

Quote:

>>In the loop where it outputs IMG tags, it increments the variable
>>number by doing
>>x=$((x+1))

> You forgot an '$' from before the second x. It seems just like a typo.

It doesn't help or hurt when I do that, but I just realized that it is
kinda strange that it works without.

Quote:> Vilmos

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by CTE » Fri, 23 May 2003 06:48:19



> What you need is "shift".  Each time through your loop, use "$1" so
> you can eliminate that eval statement and the x=x+1 statement.
> This is a standard situation with a standard solution:

>    set -- $(ls -1 $wwwdir/$pixdir)
>    while test "$1"
>    do
>            curr=$1
>            ...
>            shift
>    done

> The problem is that when there are more than 9 arguments positional
> parameter 9 ("$9") gets set to all remaining arguments.  By using shift
> the problem is avoided, since every call to shift re-assigns the
> positional parameters by shifting one to the left.  So the old $2
> becomes the new $1, the old $3 becomes the new $2, and so on.

> Hope this helps!

Well I'm using it, as I just got the idea to do that from Sybren's reply.

> -Wayne


[SNIP]

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by CTE » Fri, 23 May 2003 06:55:50


OK, it works now:


> I wrote a shell script to be a CGI script.
> It takes all the pix in a specified directory, then output an HTML file
> with each pictrue in "img" tags.
> I use
> set $(ls -1 $wwwdir/$pixdir)
> where wwwdir is the website's root dir and pixdir is the directory the
> pix are in.
> That makes variables 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11...
> one for each filename.
> In the loop where it outputs IMG tags, it increments the variable number
> by doing
> x=$((x+1))

Doesn't do this anymore.

Quote:> it then gets the current filename by:
> eval curr='$'$x

Uses:
curr=$1
in place of this

ADDITION:
uses
shift
to swithc variables.

Quote:> it tells when it's at the end by using
> if [ $curr != "" ]

> it works fine with 1-8 pix
> but 9 and up is bad
> can you see the problem?

well now there isn't one

Quote:

> here it is:
> With only 8, when it tries 9, that is empty because there is no such
> variable.
> But say theres 15. When it gets to 16 it basically takes the 1 and
> interprets that as a variable and appends a 6.
> How can I trap this, because it currently runs and endless loop and
> hitting my server over and over with requests for nonexistant pix.

works fine in both cases.

If anyone has an interest in the code, I'll be happy to post it. It's
going to be at http://ctenet0.sf.net/ soon though.

Thanks everyone for the quick replies.

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by Wayne Polloc » Fri, 23 May 2003 13:35:17




> > What you need is "shift".  Each time through your loop, use "$1" so
> > you can eliminate that eval statement and the x=x+1 statement.
> > This is a standard situation with a standard solution:

> >       set -- $(ls -1 $wwwdir/$pixdir)
> >       while test "$1"
> >       do
> >               curr=$1
> >               ...
> >               shift
> >       done

> > The problem is that when there are more than 9 arguments positional
> > parameter 9 ("$9") gets set to all remaining arguments.  By using shift
> > the problem is avoided, since every call to shift re-assigns the
> > positional parameters by shifting one to the left.  So the old $2
> > becomes the new $1, the old $3 becomes the new $2, and so on.

> > Hope this helps!
> Well I'm using it, as I just got the idea to do that from Sybren's reply.

> > -Wayne

Actually in this situation Vilimos' solution is simpler:

        for curr in $(ls -1 $wwwdir/$pixdir)
        do
                ...
        done

-Wayne

 
 
 

sh script problem

Post by CTE » Sun, 25 May 2003 12:29:48





>>>What you need is "shift".  Each time through your loop, use "$1" so
>>>you can eliminate that eval statement and the x=x+1 statement.
>>>This is a standard situation with a standard solution:

>>>      set -- $(ls -1 $wwwdir/$pixdir)
>>>      while test "$1"
>>>      do
>>>              curr=$1
>>>              ...
>>>              shift
>>>      done

>>>The problem is that when there are more than 9 arguments positional
>>>parameter 9 ("$9") gets set to all remaining arguments.  By using shift
>>>the problem is avoided, since every call to shift re-assigns the
>>>positional parameters by shifting one to the left.  So the old $2
>>>becomes the new $1, the old $3 becomes the new $2, and so on.

>>>Hope this helps!

>>Well I'm using it, as I just got the idea to do that from Sybren's reply.

>>>-Wayne

> Actually in this situation Vilimos' solution is simpler:

>    for curr in $(ls -1 $wwwdir/$pixdir)
>    do
>            ...
>    done

> -Wayne

Whatever.
I don't care.
I'm just glad it works. :-)
Thanks though.

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by CTE » Sun, 25 May 2003 12:56:25





[SNIP]

>> Actually in this situation Vilimos' solution is simpler:

>>     for curr in $(ls -1 $wwwdir/$pixdir)
>>     do
>>         ...
>>     done

>> -Wayne

> Whatever.
> I don't care.
> I'm just glad it works. :-)
> Thanks though.

Oh no
I jst tried my script with filenames with spaces.
Since set separates by spaces, it won't work.
Is there any way I can make it separate by newline characters?
The man page didn't offer much help on this.
TIA
PS: I would have replied to my first post, but it expired.

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by Sybren Stuve » Sun, 25 May 2003 17:40:56


CTE enlightened us with:

Quote:> I jst tried my script with filenames with spaces.
> Since set separates by spaces, it won't work.

Use something along the lines of:

find -type f -print0 | xargs blablabla

or

find -type f -exec blabla "{}" \;

Sybren
--
| Somebody ought to cross ball point pens with coat hangers so that |
| the pens will multiply instead of disappear.                      |
\-------------------------------------------------------------------/
If you have to use Outlook, fix it: http://flash.to/oe-quotefix/

 
 
 

sh script problem

Post by CTE » Mon, 26 May 2003 01:20:45



> CTE enlightened us with:

>>I jst tried my script with filenames with spaces.
>>Since set separates by spaces, it won't work.

> Use something along the lines of:

> find -type f -print0 | xargs blablabla

> or

> find -type f -exec blabla "{}" \;

> Sybren

OK thanks, I'll check it out.

--
Chris Engel
CTE Net
http://ctenet.cjb.net/

 
 
 

sh script problem

Post by Vilmos Sot » Sat, 31 May 2003 01:32:31



> Oh no
> I jst tried my script with filenames with spaces.
> Since set separates by spaces, it won't work.
> Is there any way I can make it separate by newline characters?

Check the "IFS" variable. That is, Internal Field Separator.
By default, it is set to space, tab, and newline. Reset it to
a newline only:

IFS="
"

Here is a small example:

$ touch hello world "it is"
$ ls -l
total 0
-rw-r--r--    1 vilmos   vilmos          0 May 29 09:29 hello
-rw-r--r--    1 vilmos   vilmos          0 May 29 09:29 it is
-rw-r--r--    1 vilmos   vilmos          0 May 29 09:29 world
$ for x in *; do ls $x; done
hello
ls: it: No such file or directory
ls: is: No such file or directory
world
$ IFS="

Quote:> "

$ for x in *; do ls $x; done
hello
it is
world
$

Quote:> The man page didn't offer much help on this.

It is there, but I acknowledge, it is not obvious.

Another thing you can do is to put quotation marks around vars.

$ for x in *; do ls $x; done
hello
ls: it: No such file or directory
ls: is: No such file or directory
world
$ for x in *; do ls "$x"; done
hello
it is
world
$

Vilmos

 
 
 

1. ksh script --> sh script problems

i am a newish unix user and need some advice converting a ksh script
to a sh script.

the script reads, in part

        .
        .
        .
        while read -u0 stdinline
                do
                # place stdin line into sequentially numbered file
                echo $stdinline >> $seq.tmp

                # if this is end of message(has ZZZZ), start a new one
                if [[ stdinline = ZZZZ* ]]
                        then
                        .
                        .
                        .

i have changed the "read" to "read stdinline" for the while loop test,
but i do not understand how change the "[[" test in the if statement
to test the stdinline for the ZZZZ pattern (i would like to test for
starting with ZZZZ).

any suggestions anyone???

thanks
    leofa

2. Drivers for the DTC SCSI controllers?

3. sh script problem

4. Simple Socket Programming Examples? Help

5. !/bin/sh script problem

6. EVM C6201 under Linux ?

7. Execute a sh script under perl and sh: sh script; perl script?

8. test please ignore

9. ASCII 128-255 in sh script: sh strips bit 7!!!

10. make sh-script compatible between SUN-sh and bash

11. script or command to find all scripts (ksh, sh, perl, etc) in a workspace

12. how to handle usig csh scripts from within bash/sh scripts?

13. How to execute csh script in sh script ?