how do I run a shell command?

Asked by pham

Hi,

I am so new to this UNIX. I am trying to do some practice by using Linux book but confused with it. Here is the one I am trying to practice:

Copy the following shell that completes the following:
#!/bin/bash
echo Select a country from the following list:
echo America, Italy, France, Germany
read country

case $country in
  [Aa]merica ) echo Ford; echo Perhaps;;
   [Ii]taly ) echo Ferarri;echo no;;
   [Ff]rance ) echo Peugeot ;echo no way;;
   [Gg]ermany ) echo Porsche ;echo yes;;

esac

Run the shell from above.

I DID THAT. PLEASE SEE BELOW:

ubuntu@ubuntu:~$ #!/bin/bash
ubuntu@ubuntu:~$ echo Select a country from the following list:
Select a country from the following list:
ubuntu@ubuntu:~$ echo America, Italy, France, Germany
America, Italy, France, Germany
ubuntu@ubuntu:~$ read country

ubuntu@ubuntu:~$ case $country in
> [Aa]merica ) echo Ford; echo Perhaps;;
> [Ii]taly ) echo Ferarri; echo no;;
> [Ff]rance ) echo Peugeot; echo no way;;
> [Gg]ermany ) echo Porsche; echo yes;;
> esac
ubuntu@ubuntu:~$

BUT I STOP AS I DON'T KNOW HOW WILL I RUN IT, CAN YOU PLEASE HELP? ALSO, ANOTHER QUESTION IS HOW DO I SAVE IT? I DON'T SEE ANY SAVE ICON FROM THE TERMINAL I OPENED.

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Solved by:
marcobra (Marco Braida)
Solved:
Last query:
Last reply:

This question was reopened

  • by pham
Revision history for this message
Jayson Rowe (jayson.rowe) said :
#1

It looks like the book is wanting you to put that text in a text editor, and save the file as a shell script (*.sh).

You would then open a terminal window and type: sh command.sh to execute the command.

HTH.

Revision history for this message
pham (mprabsatt) said :
#2

Thanks for the advice. All of them I need to copy and paste it on the text editor then save it there? Sorry I am slow please explain it at the beginning so I can practice it again. But how do I run it? Do I run from text editor or run it on the terminal?

Revision history for this message
Jayson Rowe (jayson.rowe) said :
#3

I'll try one more time.

Open up the text editor of your choice. Paste the following into the empty file:

#!/bin/bash
echo Select a country from the following list:
echo America, Italy, France, Germany
read country

case $country in
  [Aa]merica ) echo Ford; echo Perhaps;;
   [Ii]taly ) echo Ferarri;echo no;;
   [Ff]rance ) echo Peugeot ;echo no way;;
   [Gg]ermany ) echo Porsche ;echo yes;;

esac

Now, save that file as command.sh

Close the text editor.

Open up a terminal, and navigate to the directory where you saved 'command.sh'

in the terminal type: sh command.sh

Answer the question, and you'll get an output.

Here are screenshots for clarification:

http://www.lamerc.com/uploads/command_gedit.png
http://www.lamerc.com/uploads/command_term.png

Revision history for this message
pham (mprabsatt) said :
#4

Hi Jayson,

Thank you very much. I am doing it backwards that's why it's not working. Now, I know that I need to start at the text editor then terminal. I will try it now. I really want to learn Linux because I found it very very interesting but I really need to do a lot of practice and readings so I can get familiar with it. It's really when I don't have that programming skills. : )

pham

Revision history for this message
pham (mprabsatt) said :
#5

Does anybody know why this "[Aa]merica )" is in both caps and lower case from the sample above?

Revision history for this message
aditirex (adi-tirex) said :
#6

To match both America and america cases . It's a small regexp.

Revision history for this message
pham (mprabsatt) said :
#7

Thanks. On the previous problem. I followed Jayson instructions step by step but when I opened terminal, typed sh command.sh , it doesn't want to run. I don't know what I am doing wrong.

Revision history for this message
Best marcobra (Marco Braida) (marcobra) said :
#8

Please try to make your bash script file executable type:

chmod 700 command.sh

then to execute it

./command.sh

For your Bash shell i suggest you:
http://www.linuxcommand.org/
http://www.gnu.org/software/bash/manual/bashref.html
http://www.tldp.org/LDP/abs/html/

Hope this helps

Revision history for this message
pham (mprabsatt) said :
#9

Thanks but it didn't work. I tried something else and it worked. Instead of using chmod 700 command.sh , I put chmod +x command then execute it. Thank you for the three of you helped me figure this out. THANKS A LOT GUYS.

Pam