Why does this script only run just partially?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

Why does this script only run just partially?

#1 Post by benny7440 »

I suspect that in reference to the above OSs, virtualized or not, kernel differences might have an impact. Command inclusion also might be an issue.

The script is very simple but it executes only partially; have no clue as to what might be happening.


echo "Elija un # entre 1 y 12 para un mes"
read num_mes
case $num_mes in
1 echo $num_mes ---> Enero
2 echo $num_mes ---> Febrero
3 echo $num_mes ---> Marzo
4 echo $num_mes ---> Abril
5 echo $num_mes ---> Mayo
6 echo $num_mes ---> Junio
7 echo $num_mes ---> Julio
8 echo $num_mes ---> Agosto
9 echo $num_mes ---> Septiembre
10 echo $num_mes ---> Octubre
11 echo $num_mes ---> Noviembre
12 echo $num_mes ---> Diciembre
echo "No has seguido bien las instrucciones!"
esac
exit 0


The output of the above is:
users-ibook-g4-667:Data user$ ./caselab-1.sh
Elija un # entre 1 y 12 para un mes
5
./caselab-1.sh: line 4: syntax error near unexpected token `echo'
./caselab-1.sh: line 4: ` 1 echo $num_mes ---> Enero'
users-ibook-g4-667:Data user$
echo "Elija un numero entre 1 y 12 para un mes"
read num_mes
case "$num_mes" in
"1") echo "$num_mes ---> Enero";;
"2") echo "$num_mes ---> Febrero";;
"3") echo "$num_mes ---> Marzo";;
"4") echo "$num_mes ---> Abril";;
"5") echo "$num_mes ---> Mayo";;
"6") echo "$num_mes ---> Junio";;
"7") echo "$num_mes ---> Julio";;
"8") echo "$num_mes ---> Agosto";;
"9") echo "$num_mes ---> Septiembre";;
"10") echo "$num_mes ---> Octubre";;
"11") echo "$num_mes ---> Noviembre";;
"12") echo "$num_mes ---> Diciembre';;
echo "No has seguido bien las instrucciones!";;
esac
exit 0


When running this version it gave me another error at line #16, probably due to ending it with "::" instead of a line-feed.
The thing is that the above results are coming from the iBook; if I go with the exact same scripts to Ubuntu Studio or Slacko it goes even a shorter distance from Start...

The file permissions are something that too gives some problems but those are manageable. Nonetheless, I'm certain that here's something very basic that's escaping my comprehension. If, taking into consideration those 3 scenarios established (Slacko & Ubuntu Studio, both virtualized, the iBook Gen 4), how to proceed in order to be able at least to debug scripts or still better, how to make scripts that are understandable by all three?

Thanks in advanced for any help on the above!
[/quote]
[b]Toshiba Satellite L555 / ACPI x64-based PC / Intel Core i5 / ~1.46 @ 3 Partitions GB HDD / 4 GB RAM / CD-DVD RW Drive.
[/b]

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#2 Post by Keef »

People who actually know what they are talking about will be along soon, but the following seems to work ok.

Code: Select all

#!/bin/sh

echo "Elija un numero entre 1 y 12 para un mes"
read num_mes
case "$num_mes" in
1) echo "$num_mes ---> Enero";;
2) echo "$num_mes ---> Febrero";;
3) echo "$num_mes ---> Marzo";;
4) echo "$num_mes ---> Abril";;
5) echo "$num_mes ---> Mayo";;
6) echo "$num_mes ---> Junio";;
7) echo "$num_mes ---> Julio";;
8) echo "$num_mes ---> Agosto";;
9) echo "$num_mes ---> Septiembre";;
10) echo "$num_mes ---> Octubre";;
11) echo "$num_mes ---> Noviembre";;
12) echo "$num_mes ---> Diciembre";;

*) echo "No has seguido bien las instrucciones!";;

esac
exit 0

Code: Select all

# ./benny
Elija un numero entre 1 y 12 para un mes
6
6 ---> Junio

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#3 Post by musher0 »

Hello benny

Here is an edit of your 2nd script above:
#!/bin/sh
# num_mes.sh # par / by benny7440
# Source: http://murga-linux.com/puppy/viewtopic. ... ost#938605
# Corrected by musher0, 2017-01-08
####
echo "Elija un numero entre 1 y 12 para un mes"
read num_mes
case "$num_mes" in
"1") echo "$num_mes ---> Enero" ;;
"2") echo "$num_mes ---> Febrero" ;;
"3") echo "$num_mes ---> Marzo" ;;
"4") echo "$num_mes ---> Abril" ;;
"5") echo "$num_mes ---> Mayo" ;;
"6") echo "$num_mes ---> Junio" ;;
"7") echo "$num_mes ---> Julio" ;;
"8") echo "$num_mes ---> Agosto" ;;
"9") echo "$num_mes ---> Septiembre" ;;
"10") echo "$num_mes ---> Octubre" ;;
"11") echo "$num_mes ---> Noviembre" ;;
"12") echo "$num_mes ---> Diciembre" ;;
*) echo "No has seguido bien las instrucciones!" ;;
esac
exit 0
As you'll notice,

-- you needed to leave a space before ;; in the case...esac structure

-- your Diciembre line had a ' finishing the echo command, whereas a " was needed.

The echo command will work with ' or ", but you have to be consistent.
If you start your echo message with a ", you have to end it with a ".

-- your last echo line for the errors needed a *) to introduce it. The *)
characters in succession in a case...esac structure means "for all other
answers, do the following."


Also, you do not need to put the numbers inside quotes, but it is not
an error to do so. 1) 2) etc. will work just as well.

IHTH
Attachments
Proof.jpg
:-)
(30.73 KiB) Downloaded 305 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#4 Post by musher0 »

Hello again, benny.

I forgot to mention that a well-constructed, error-free, bash script will run
correctly on any distro that has a bash interpreter of +/- the same version.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#5 Post by benny7440 »

Thanks a lot for replying, Keef & musher0!

Your advises were right to the point: it just simply worked OK.

The instructions I'm following I'm afraid that are less than optimal with respect to DETAILS... Also, I've failed in not noticing the spaces needed before the --->::<--- fragments. The included debugger for scripts didn't captured the details or failed to be more correct in the expression used to point to the deviations.

I hope practice will be the only thing needed by my part to become less prone to such mistakes in the near future.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#6 Post by musher0 »

Hi benny.

My pleasure!

Keep at it! It will!
Remember the saying: "Practice makes perfect!" ;)

Don't hesitate to create a thread in this section for any other programming
question that may be puzzling you. I'm sure that forum members who know
the answer will be glad to give you a hand.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#7 Post by benny7440 »

Think I proclaimed victory before my enemy made a rendition plee!

It worked OK at the iBook but thought of trying it again at the virtualized machines (Slacko & U_Studio) for making sure they behaved in the same way and U_Studio differ. In Slacko & U_Studio had to re-edit the file because when sent via email some characters got lost. After that it worked at Slacko.

In U_Studio it fails to recognize all the ¨Good¨ conditions (1-12) no matter how I write them: with & without quoting the numbers (i.e., ¨3¨). Everything is out of the constraints. The only valid alternative is *) or ¨*¨). BTW, don´t know why at U_Studio for writing the characters `, ´ and ¨ I have to press the key twice. Could this might be pointing to a problem of another kind that might influence the behavior of the script? No clue after using the cat command nor with a regular text editor at U_Studio.

The most recent code is:

echo "Elija un numero entre 1 y 12 para un mes"
read num_mes
case ¨$num_mes¨ in
1) echo ¨$num_mes ---> Enero¨ ;;
2) echo ¨$num_mes ---> Febrero¨ ;;
3) echo ¨$num_mes ---> Marzo¨ ;;
4) echo ¨$num_mes ---> Abril¨ ;;
5) echo ¨$num_mes ---> Mayo¨ ;;
6) echo ¨$num_mes ---> Junio¨ ;;
7) echo ¨$num_mes ---> Julio¨ ;;
8) echo ¨$num_mes ---> Agosto¨ ;;
9) echo ¨$num_mes ---> Septiembre¨ ;;
10) echo ¨$num_mes ---> Octubre¨ ;;
11) echo ¨$num_mes ---> Noviembre¨ ;;
12) echo ¨$num_mes ---> Diciembre¨ ;;
*) echo "No has seguido bien las instrucciones!" ;;
esac
exit 0


The corresponding output is:

Elija un numero entre 1 y 12 para un mes
0
No has seguido bien las instrucciones!
Elija un numero entre 1 y 12 para un mes
5
No has seguido bien las instrucciones!
Elija un numero entre 1 y 12 para un mes
12
No has seguido bien las instrucciones!
Elija un numero entre 1 y 12 para un mes
13
No has seguido bien las instrucciones!
bennyhome@Bernabe-VirtualBox:~$


I´ve zoomed-in in order to inspect the code really close to check for some mispelled something but can´t find a reason for this behavior. I´m sorry that I´ve to ask twice at the same problem, although for a ¨different¨ reason.

Note: Have no idea how that emoticon substituted 8) in the code part above...; I see it at the Preview page. Can´t take that out of there??? Be sure it isn´t part of my script problem.

Edit: After posting noticed that the emoticon thing replicated at the note paragraph. Apparently, every trial of writing the common symbols of {eight and the closing parenthesis} or one of them triggers at the webpage that emoticon. Hope it´s just a quirky thing without any real importance and not a problem with U_Studio because I like this a lot.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#8 Post by musher0 »

Hello again.

I can't help you with your U_Studio version since I don't have U_Studio.

However I notice that you have not included the header of the script,
like keef and I have done in our versions of your script.

The first line, something like this:

Code: Select all

#!/bin/bash
OR

Code: Select all

#!/bin/ash
OR

Code: Select all

#!/bin/dash
is important in that it tells which interpreter/language to use to run
the script.

IIRC, and please correct me if I'm wrong, the default interpreter on
Ubuntus is dash. Puppies do not offer it, generally; we have bash and
ash, though.

Maybe check if dash is the default interpreter in your Ubuntu, and if such
is the case, maybe check the dash manual for the proper syntax for the
case...esac structure in dash.

Best I can do.

~~~~~~

As to the 8) for 8 and ), don't worry about it. Next time, try quoting your
code as code (5th button on top of the message editor window). Click
twice on that button and insert your script between the first [ code ]
bracket and the 2nd [ /code ] bracket.

(I put the spaces intentionally, to explain. There are no spaces in fact
between the word and the bracket.)

For ex.:
8) versus

Code: Select all

8)
Do you see the difference?

IHTH. BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#9 Post by Keef »

benny7440

I think you will solve your problem by typing the code in by hand in a text editor (geany, leafpad, beaver etc) and not a word processor. Copy and paste, especially from a web page can bring in hidden characters and junk that cause problems. I did a copy and paste of your code, and got the same errors as you. Copying mine did work though.
Start from scratch and I think you will be ok.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#10 Post by MochiMoppel »

benny7440 wrote:BTW, don´t know why at U_Studio for writing the characters `, ´ and ¨ I have to press the key twice. Could this might be pointing to a problem of another kind that might influence the behavior of the script?
You bet. Maybe U_Studio wants to protect you from using them :lol:. What do you need them for? None of these 3 characters are quotes, they are accents and umlaut marks, stuff you normally would put on top of vowels. I assume that pressing the key twice makes sense only after vowels, which may create accented characters and umlauts.
I´ve zoomed-in in order to inspect the code really close to check for some mispelled something but can´t find a reason for this behavior
OK, let's zoom, and now inspect the code *really* close: In line 3 you use correct double quotes (ASCII 34). My editor's syntax highlighting marks strings surrounded by double quotes with an orange color. This is one way to make sure that I use valid quotation marks.

Now look at line 5: What you (or U_Studio?) pass off as quotes are in fact diaeresis characters (ASCII 168). The command interpreter (bash?) treats ¨$num_mes¨ as a 10 characters long string and not as a variable, and as none of your 12 numbers in the case statement matches this funny string, only the catch all '*' pattern matches and you see the error message.
Attachments
quotes_vs_diaeresis.png
(14.74 KiB) Downloaded 218 times

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

Good catch MochiMoppei! :)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#12 Post by Keef »

Mochi to the rescue!

User avatar
benny7440
Posts: 440
Joined: Mon 20 Apr 2009, 04:23

#13 Post by benny7440 »

Thanks for replying, musher0, Keef & MochiMoppel!

I'm sorry to have taken so much time to reply back, the thing is that I was down in my enthusiasm due to too many failures & absurdly too elemental. So I decided to go way back & restarted many chapters back from the point I was at the time of posting. Every day saw the email there reminding me that there was an unfinished business to be dealt around it; but wasn't the moment in time to do it.

Decided to reply today because you deserve to know what has been happening & I'm approaching the point of detachment. This probably will mean other questions emanating from my part (hopefully more mature ones)!

Post Reply