bash doubt

View: New views
11 Messages — Rating Filter:   Alert me  

bash doubt

by L.V.Gandhi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a script as follows
lvgandhi@lvghomepc:~$ cat bin/getlstocks
#!/bin/bash
i=0
while [ $i -ne 20 ]
do
        for trv in $(cat temp)
        do
                i=$i+1
                grep $trv stock/nsedata/2008/05/20080512.txt >> lstock
        done
done
when I run I get error as
integer expression expected
How to correct it

--
L.V.Gandhi
http://lvgandhi.tripod.com/
linux user No.205042


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Jordi Gutiérrez Hermoso-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 13/05/2008, L. V. Gandhi <lvgandhi@...> wrote:
> I have a script as follows
[snip]
>                 i=$i+1
[snip]
>  How to correct it

Your counter is wrong. $i + 1 means to treat i as a string and to
append the string "1" to it. If you want to do arithmetic in bash, you
have to put it inside brackets. i = $[i+1].

Btw, an easier way to do a for loop of this sort in bash is to do "for
i in `seq 20`" (notice the backticks).

If you want to do a lot of text manipulation, it may feel more natural
to use Perl (or at least it feels more natural to me).

- Jordi G. H.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Douglas A. Tutty :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, May 13, 2008 at 08:05:16PM -0500, Jordi Guti?rrez Hermoso wrote:
 
> If you want to do a lot of text manipulation, it may feel more natural
> to use Perl (or at least it feels more natural to me).

Or Python.  Or Ada.

Almost anything but sh.

Doug.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Todd A. Jacobs :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, May 14, 2008 at 06:21:07AM +0530, L.V.Gandhi wrote:

>                 i=$i+1

This syntax is broken. Any of these alternatives will work:

    - let i=$i+1
    - let i+=1
    - i=$(( i + 1 ))

Basically, you need to let bash know that $i is an integer, and not a
string.

--
"Oh, look: rocks!"
        -- Doctor Who, "Destiny of the Daleks"


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Michelle Konzack-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Am 2008-05-14 06:21:07, schrieb L.V.Gandhi:
> I have a script as follows
> lvgandhi@lvghomepc:~$ cat bin/getlstocks
> #!/bin/bash
> i=0
> while [ $i -ne 20 ]
> do
>         for trv in $(cat temp)
>         do
>                 i=$i+1
                  i=$((i+1))

Thanks, Greetings and nice Day
    Michelle Konzack
    Systemadministrator
    24V Electronic Engineer
    Tamay Dogan Network
    Debian GNU/Linux Consultant


--
Linux-User #280138 with the Linux Counter, http://counter.li.org/
##################### Debian GNU/Linux Consultant #####################
Michelle Konzack   Apt. 917                  ICQ #328449886
+49/177/9351947    50, rue de Soultz         MSN LinuxMichi
+33/6/61925193     67100 Strasbourg/France   IRC #Debian (irc.icq.com)


signature.pgp (196 bytes) Download Attachment

Re: bash doubt

by L.V.Gandhi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, May 14, 2008 at 8:54 AM, Todd A. Jacobs <nospam@...> wrote:

> On Wed, May 14, 2008 at 06:21:07AM +0530, L.V.Gandhi wrote:
>
>>                 i=$i+1
>
> This syntax is broken. Any of these alternatives will work:
>
>    - let i=$i+1
>    - let i+=1
>    - i=$(( i + 1 ))
>
> Basically, you need to let bash know that $i is an integer, and not a
> string.
>
Now I changed this to
rm -f ~/lstock
i=0
for trv in $(cat temp)
do
        while [ $i -ne 20 ]
        do
                i=$[i+1]
                grep $trv stock/nsedata/2008/05/20080512.txt >> lstock
        done
done
I get lstock only 0 byte file.
Where am I wrong

--
L.V.Gandhi
http://lvgandhi.tripod.com/
linux user No.205042


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Ansgar Esztermann-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


> Where am I wrong

By the way, you might want to call your script as
bash -x scriptname

This will run the script in trace mode, much facilitating debugging.  
You might also set PS4 to something sensible first. Personally, I  
prefer PS4='+$0 l $LINENO: '


A.
- --
Ansgar Esztermann
DV-Systemadministration
Max-Planck-Institut für biophysikalische Chemie, Abteilung 105

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)

iEYEARECAAYFAkgr37kACgkQYmVfELZx7BpkwwCgos6hRSOzpj9PgBzAzpkDyVzN
94QAoO6iqIMnGtH87c2FxuSpTELKtUoY
=wR08
-----END PGP SIGNATURE-----


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Javier Barroso-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Thu, May 15, 2008 at 2:58 AM, L. V. Gandhi <lvgandhi@...> wrote:

Now I changed this to
rm -f ~/lstock
i=0
for trv in $(cat temp)
do
       while [ $i -ne 20 ]
       do
               i=$[i+1]
               grep $trv stock/nsedata/2008/05/20080512.txt >> lstock
       done
done
I get lstock only 0 byte file.
Hi,
which is the "temp" file content ?

why are you using $i loop? , trv doesn't change inside it ...




Re: bash doubt

by Javier Barroso-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



On Wed, May 14, 2008 at 3:05 AM, Jordi Gutiérrez Hermoso <jordigh@...> wrote:
On 13/05/2008, L. V. Gandhi <lvgandhi@...> wrote:
> I have a script as follows
[snip]
>                 i=$i+1
[snip]
>  How to correct it

Your counter is wrong. $i + 1 means to treat i as a string and to
append the string "1" to it. If you want to do arithmetic in bash, you
have to put it inside brackets. i = $[i+1].
 
You should use i=$((i+1))

In man bash:
 The old format $[expression] is deprecated and will be removed in upcoming versions of bash.

Re: bash doubt

by Bernardo Dal Seno :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/5/15 L. V. Gandhi <lvgandhi@...>:
> Where am I wrong

With the knowledge of the content of your files it is easier to
answer.  Running the script in trace mode is the best thing to do.

Anyway, I see that there are two problems here (which may or may not
be related to your problem):

> for trv in $(cat temp)

If expressions in `temp' contain spaces, they will be broken; this
would be better:
 while read trv; do
   ....
 done < temp


>                grep $trv stock/nsedata/2008/05/20080512.txt >> lstock

$trv should be quoted, like this:
  grep "$trv" ...


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...


Re: bash doubt

by Osamu Aoki :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, May 13, 2008 at 08:05:16PM -0500, Jordi Gutiérrez Hermoso wrote:

> On 13/05/2008, L. V. Gandhi <lvgandhi@...> wrote:
> > I have a script as follows
> [snip]
> >                 i=$i+1
> [snip]
> >  How to correct it
>
> Your counter is wrong. $i + 1 means to treat i as a string and to
> append the string "1" to it. If you want to do arithmetic in bash, you
> have to put it inside brackets. i = $[i+1].

Although it should work.... why not use POSIX style which bas accept
too.

i=$((i+1))


For speed of shell script, it is faster to use dash and better to be
portable.

> Btw, an easier way to do a for loop of this sort in bash is to do "for
> i in `seq 20`" (notice the backticks).

Yep. I agree.

Also with xargs command instead of shell loop, you should get faster
script.

> If you want to do a lot of text manipulation, it may feel more natural
> to use Perl (or at least it feels more natural to me).

True.


--
To UNSUBSCRIBE, email to debian-user-REQUEST@...
with a subject of "unsubscribe". Trouble? Contact listmaster@...