LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell-script question about getting a number as a variable (https://www.linuxquestions.org/questions/programming-9/shell-script-question-about-getting-a-number-as-a-variable-424578/)

stormrider_may 03-13-2006 08:20 PM

Shell-script question about getting a number as a variable
 
I have a little script. In it, i have to get a page where will be a number, that is very important to me.

like this...

Code:

lynx -dump http://192.168.0.2/venda_atual.php > temp.php
The problem is that when i see temp.php i get something like this.

Code:

  1670
Thatīs the number, but it have three spaces in front on it. Well, now let me put the script here and you will see my problem.

Code:

links http://192.168.0.2/venda.php
lynx -dump http://192.168.0.2/venda_atual.php > atual.tmp
venda=īhead -1 atual.tmpī
lynx -dump http://192.168.0.2/venda2.php?codigo=$venda > temp.php
lp temp.php
imprimir=$(
        dialog  --yesno '\n Deseja imprimir a venda novamente?'\
                --stdout \
                --backtitle 'Demorgan Unifomes LTDA.' \
                0 0 )
if [ $imprimir = 0 ]; then
      lp temp.php
fi

The problem is the second time when lynx run. It gets three spaces on the url, and thatīs a problem because it wonīt work at all.
What can i do?

Also, are there any syntax error in my script?

gilead 03-13-2006 09:08 PM

If your shell is set up to have extglob on with shopt -s extglob, the following works:
Code:

$ venda="  1670"
$ echo "${venda##+([[:space:]])}"
1670
$

I haven't checked your script for errors...

stormrider_may 03-13-2006 09:49 PM

Well, looks like shell script hates me a lot.

This script donīt work

Code:

links http://192.168.0.2/programa/venda.php
lynx -dump http://192.168.0.2/venda_atual.php > atual.tmp
venda1=$(head -1 atual.tmp)
venda=${venda1##+([[:space:]])}
lynx -dump http://192.168.0.2/venda2.php?codigo=$venda > temp.php
lp temp.php
while : ; do
imprimir=$(
        dialog --stdout              \
              --title 'Demorgan Uniformes LTDA.'  \
              --menu 'Deseja imprimir a venda novamente?' \
              0 0 0                  \
              1 'Sim' \
              2 'Nco'  )
case "$imprimir" in
        1) break ;;
        2) /usr/bin/lp temp.php ;;
esac
done

Why those error happens?

Code:

Can't Access `file://localhost/root/1732%0D%0D'
Alert!: Unable to access document.

lynx: Can't access startfile
lp: unable to print file: client-error-bad-request
'usr/bin/programa_venda: line 15: syntax error near unexpected token `in
'usr/bin/programa_venda: line 15: `case "$imprimir" in


gilead 03-13-2006 09:55 PM

Can you try it again after running shopt -s extglob at a bash prompt? If the extglob option isn't set, the +( ) part of the substitution won't be recognised.

stormrider_may 03-13-2006 10:02 PM

I tried it, still not working... :(

paulsm4 03-13-2006 10:02 PM

Please try this:
Code:

links http://192.168.0.2/venda.php
lynx -dump http://192.168.0.2/venda_atual.php > atual.tmp
venda=`head -1 atual.tmp|awk '{print $1}'`
lynx -dump http://192.168.0.2/venda2.php?codigo=$venda > temp.php
lp temp.php
imprimir=$(
        dialog  --yesno '\n Deseja imprimir a venda novamente?'\
                --stdout \
                --backtitle 'Demorgan Unifomes LTDA.' \
                0 0 )
if [ $imprimir = 0 ]; then
      lp temp.php
fi

PS:
"set -x" can be your friend, too. for example:
Code:

links http://192.168.0.2/venda.php
lynx -dump http://192.168.0.2/venda_atual.php > atual.tmp
set -x
venda=`head -1 atual.tmp|awk '{print $1}'`
set +x

lynx -dump http://192.168.0.2/venda2.php?codigo=$venda > temp.php
lp temp.php
imprimir=$(
        dialog  --yesno '\n Deseja imprimir a venda novamente?'\
                --stdout \
                --backtitle 'Demorgan Unifomes LTDA.' \
                0 0 )
if [ $imprimir = 0 ]; then
      lp temp.php
fi


stormrider_may 03-13-2006 10:35 PM

Thanks for the help :)
Iīm still getting some errors. I hope you can help me.

Code:

request id is epsonlx300-9 (1 file(s))
: command not foundenda: line 10:

Error: Expected at least 3 tokens for --yesno, have 1.
Use --help to list options.


/usr/bin/programa_venda: line 12: --stdout: command not found
/usr/bin/programa_venda: line 13: --backtitle: command not found
/usr/bin/programa_venda: line 14: 0: command not found
/usr/bin/programa_venda: line 14: syntax error: unexpected end of file


paulsm4 03-13-2006 11:41 PM

The first error tells you the basic problem:
Code:

Error: Expected at least 3 tokens for --yesno, have 1.
Use --help to list options.

So, for example, instead of:
Code:

dialog  --yesno '\n Deseja imprimir a venda novamente?' \
                --stdout \
                --backtitle 'Demorgan Unifomes LTDA.' \
0 0

... you need something like:
Code:

dialog  --yesno '\n Deseja imprimir a venda novamente?' 0 0 \
                --stdout \
                --backtitle 'Demorgan Unifomes LTDA.'

That is, you need "dialog --yesno <MYTEXT> <X> <Y>" all together.

stormrider_may 03-14-2006 05:21 AM

paulsm4, it is complaining about it again

Code:

Error: Expected no more than 3 tokens for --yesno, have 4.
Use --help to list options.


stormrider_may 03-14-2006 08:18 AM

Solved. Thanks for the help guys. The problem was in the text editor i was using. Somehow joe was fucking my script. When i changed to nano, everything got perfect.
I think this could be a bug on joe with portuguese language and simbols like "į".


All times are GMT -5. The time now is 09:28 PM.