LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-22-2006, 06:59 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Rep: Reputation: 46
Please explain the way the program works.


#!/bin/sh
select COMPONENT in comp1 comp2 comp3 all none
do
case $COMPONENT in
comp1|comp2|comp3) CompConf $COMPONENT ;;
all) CompConf comp1
CompConf comp2
CompConf comp3
;;
none) break ;;
*) echo "ERROR: Invalid selection, $REPLY." ;;
esac
done



[The name of the program is '' temp29 ''.
--------------------------------------------
[nissanka@c83-250-108-234 ~]$ ./temp29
1) comp1
2) comp2
3) comp3
4) all
5) none
#?
-----------------------------------

I have some questions on the above program.
The words 'select' , ' COMPONENT' and ' none' are difficult to understand. Are they standard words like '' if '' and '' case '' ?

I guess the words 'comp1' , 'comp2' , ' comp3 ' are variables.

It seems the word ''COMPONENT'' too is a variable.

What is it doing? Could you please tell me the way it works?
 
Old 03-22-2006, 07:37 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Yes, COMPONENT is a bash variable. It is set to a string by the "select" command. The terms "comp1 comp2 comp3 all none" are values that COMPONENT can be set to.
 
Old 03-22-2006, 07:43 AM   #3
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
'select' tells the script to present the user with a list of choices he should pick in. The general syntax is:
Code:
select varname in choice1 choice2 choice3 choice4 ....
do
<perform some action possibly based on $varname value>
break
done
where choice1 2 3 4.... are the string values that will be presented to the user. These values can (should in my opinion) be quoted.

Here's a very simple example to illustrate:
Code:
#!/bin/bash
echo "please select an option (enter the corresponding number)"
select userChoice in "blue" "white" "red" "green" "dark orange"
do
echo "you've chosen the option: $userChoice (which was choice number $REPLY)"
break
done
This should put you on rails to understand the rest of your script as you seem to understand what the 'case' expression is used for.

Last edited by zeitounator; 03-22-2006 at 07:47 AM.
 
Old 03-22-2006, 07:47 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
also check the tldp, Advanced Bash-Scripting Guide: Chapter 10. Loops and Branches for stuff on select and case.
 
Old 03-22-2006, 07:51 AM   #5
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
I got wonderful answers from you all. Thank you very much. I am trying to learn the shell scripting using some online resources. I am running Mandriva Linux 2006 [64bit version].

I will post more and more. Of course I post when I have difficulties in understanding.
 
Old 03-22-2006, 08:03 AM   #6
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,641

Original Poster
Rep: Reputation: 46
By the way this word 'select' is unique to scripting languages. Am I right?

I have forgotten C++ and C programming. You wouldn't find it in other programming languages.
---------------------------------------------

In the given question, what is the meaning of this single parenthesis?

Can I remove it?

comp1|comp2|comp3)

all)

----------------

I know '' | '' this pipe sign means either. The paranthesis is difficult to understand.
 
Old 03-22-2006, 08:18 AM   #7
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
you can't remove this parenthesis, it is the delimeter for the 'case' options you are testing for.

Try to rewrite your script with some more indentation, documentation and cosmetic and it all becomes clearer:
Code:
#!/bin/sh

# Ask the user to select a component
select COMPONENT in "comp1" "comp2" "comp3" "all" "none"
do
    case $COMPONENT in

        "comp1"|"comp2"|"comp3")
           CompConf $COMPONENT
           ;;

        "all")
           CompConf comp1
           CompConf comp2
           CompConf comp3
           ;;

        "none")
           break
           ;;
        
        *) 
           echo "ERROR: Invalid selection, $REPLY." 
           ;;
    esac
done

Last edited by zeitounator; 03-22-2006 at 08:34 AM.
 
Old 03-22-2006, 06:17 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,511

Rep: Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415Reputation: 2415
select appears in various langs: shell, perl, sql etc ...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain how dd works and what it does ohovus Linux - Newbie 4 12-13-2005 08:48 AM
Please explain to me how VDR works with DVB? jimdaworm Linux - Software 2 09-04-2004 04:06 PM
how i can remove program completly ? (explain inside) SlackwareMan Linux - Software 4 08-01-2004 08:53 PM
Gtk-Warning but program still works... I close konsole, program closes Laptop2250 Linux - Software 2 11-14-2003 11:18 PM
Can anyone explain how to install a program hextex Linux - Newbie 1 04-17-2001 03:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration