PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > how to test for user input
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

how to test for user input

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 21h36   #1
littlehelphere@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to test for user input

I have a script and part of requires input from the user for time in
the form of h(hours), m(minutes) or d(days)
for example 1d eqautes to 1 day.
I would like to set up a test in ksh to verify the input is in
fact a numeric value followed by one of the letters above. Basically
if a user enter 1f I want the script to ask for input again. If the
user enters 1m then the script should go on. I understand this is a
if then statement but I cannot get what to test for. I have tried the
"||" but no luck. Thanks in advance.
  Réponse avec citation
Vieux 08/01/2008, 21h44   #2
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

littlehere@gmail.com wrote:
> I have a script and part of requires input from the user for time in
> the form of h(hours), m(minutes) or d(days)
> for example 1d eqautes to 1 day.
> I would like to set up a test in ksh to verify the input is in
> fact a numeric value followed by one of the letters above. Basically
> if a user enter 1f I want the script to ask for input again. If the
> user enters 1m then the script should go on. I understand this is a
> if then statement but I cannot get what to test for. I have tried the
> "||" but no luck. Thanks in advance.


Not, it's not an "if" statement, it's a "case" statement:

read time
case $time in
[0-9]*[hmd] ) echo "good" ;;
* ) echo "bad" ;;
esac

Regards,

Ed.
  Réponse avec citation
Vieux 08/01/2008, 21h52   #3
littlehelphere@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> littleh...@gmail.com wrote:
> > I have a script and part of requires input from the user for time in
> > the form of h(hours), m(minutes) or d(days)
> > for example 1d eqautes to 1 day.
> > I would like to set up a test in ksh to verify the input is in
> > fact a numeric value followed by one of the letters above. Basically
> > if a user enter 1f I want the script to ask for input again. If the
> > user enters 1m then the script should go on. I understand this is a
> > if then statement but I cannot get what to test for. I have tried the
> > "||" but no luck. Thanks in advance.

>
> Not, it's not an "if" statement, it's a "case" statement:
>
> read time
> case $time in
> [0-9]*[hmd] ) echo "good" ;;
> * ) echo "bad" ;;
> esac
>
> Regards,
>
> Ed.


Thanks - two quick questions
1) How do I set this up so if the test fails the user is forced to
enter a valid response?
2) Could this be an if/then statement or in a case like this is a case
statement the "best practice"?
  Réponse avec citation
Vieux 08/01/2008, 23h45   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

littlehere@gmail.com wrote:
> On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>littleh...@gmail.com wrote:
>>
>>> I have a script and part of requires input from the user for time in
>>>the form of h(hours), m(minutes) or d(days)
>>>for example 1d eqautes to 1 day.
>>> I would like to set up a test in ksh to verify the input is in
>>>fact a numeric value followed by one of the letters above. Basically
>>>if a user enter 1f I want the script to ask for input again. If the
>>>user enters 1m then the script should go on. I understand this is a
>>>if then statement but I cannot get what to test for. I have tried the
>>>"||" but no luck. Thanks in advance.

>>
>>Not, it's not an "if" statement, it's a "case" statement:
>>
>>read time
>>case $time in
>> [0-9]*[hmd] ) echo "good" ;;
>> * ) echo "bad" ;;
>>esac
>>
>>Regards,
>>
>> Ed.

>
>
> Thanks - two quick questions
> 1) How do I set this up so if the test fails the user is forced to
> enter a valid response?



time=""
while [ -z "$time" ]
do
printf "Enter a time in \"[0-9]*[hmd]\" format: " >&2
IFS= read -r time
case $time in
[0-9]*[hmd] ) : ;;
* ) printf "Invalid time \"%s\", try again.\n" "$time" >&2
time=""
;;
esac
done

> 2) Could this be an if/then statement or in a case like this is a case
> statement the "best practice"?


I wouldn't even think of trying that with an if/then. "case" is best
practice.

Ed.
  Réponse avec citation
Vieux 09/01/2008, 11h31   #5
Stephane Chazelas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Tue, 08 Jan 2008 15:44:45 -0600, Ed Morton wrote:
[...]
> case $time in
> [0-9]*[hmd] ) echo "good" ;;
> * ) echo "bad" ;;
> esac

[...]

This would say that 0Zd is "good". Remember that the globbing
"*" is not the same as the regexp "*".

case $time in
("" | *[!hmd] | ? | *[!0-9]*?) echo >&2 bad;;
(*) echo good;;
esac

or:

if expr "x$time" : 'x[0-9]\{1,\}[hmd]$' > /dev/null; then
echo good
else
echo >&2 bad
fi

--
Stephane
  Réponse avec citation
Vieux 09/01/2008, 14h03   #6
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

Stephane Chazelas wrote:
> On Tue, 08 Jan 2008 15:44:45 -0600, Ed Morton wrote:
> [...]
>
>>case $time in
>> [0-9]*[hmd] ) echo "good" ;;
>> * ) echo "bad" ;;
>>esac

>
> [...]
>
> This would say that 0Zd is "good". Remember that the globbing
> "*" is not the same as the regexp "*".


Oh, yeah... Thanks.

> case $time in
> ("" | *[!hmd] | ? | *[!0-9]*?) echo >&2 bad;;
> (*) echo good;;
> esac


I'd probably write it as:

case $time in
*[!0-9]*? ) echo "bad" ;;
[0-9]*[hmd] ) echo "good" ;;
* ) echo "bad" ;;
esac

i.e. rather than explicitly specifying every bad pattern, just specify
up front those bad patterns that are a subset of the potentially good
patterns, then specify the good pattern(s), then let the final "*" catch
all other bad patterns.

Regards,

Ed.
  Réponse avec citation
Vieux 09/01/2008, 16h26   #7
littlehelphere@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> littleh...@gmail.com wrote:
> > On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>littleh...@gmail.com wrote:

>
> >>> I have a script and part of requires input from the user for time in
> >>>the form of h(hours), m(minutes) or d(days)
> >>>for example 1d eqautes to 1 day.
> >>> I would like to set up a test in ksh to verify the input is in
> >>>fact a numeric value followed by one of the letters above. Basically
> >>>if a user enter 1f I want the script to ask for input again. If the
> >>>user enters 1m then the script should go on. I understand this is a
> >>>if then statement but I cannot get what to test for. I have tried the
> >>>"||" but no luck. Thanks in advance.

>
> >>Not, it's not an "if" statement, it's a "case" statement:

>
> >>read time
> >>case $time in
> >> [0-9]*[hmd] ) echo "good" ;;
> >> * ) echo "bad" ;;
> >>esac

>
> >>Regards,

>
> >> Ed.

>
> > Thanks - two quick questions
> > 1) How do I set this up so if the test fails the user is forced to
> > enter a valid response?

>
> time=""
> while [ -z "$time" ]
> do
> printf "Enter a time in \"[0-9]*[hmd]\" format: " >&2
> IFS= read -r time
> case $time in
> [0-9]*[hmd] ) : ;;
> * ) printf "Invalid time \"%s\", try again.\n" "$time" >&2
> time=""
> ;;
> esac
> done
>
> > 2) Could this be an if/then statement or in a case like this is a case
> > statement the "best practice"?

>
> I wouldn't even think of trying that with an if/then. "case" is best
> practice.
>
> Ed.


That did it thanks - now this brings me to another question on using
the case script. Can I use a variable for a response. For example
lets say I want the operator to enter a valid user from a file such as
the passwd file.


Would something like this work? Thanks.
user=""
while [ -z "$user" ]
do
foobar=`awk -F: '{print $1}' /etc/passwd`
printf "valid users are $foobar - please enter one:" >&2
case $userin
$foobar) : ;;
*) printf "Invalid user \"%s\", try again.\n" "$user" >&2
user=""
;;
esac
done


  Réponse avec citation
Vieux 09/01/2008, 17h23   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

littlehere@gmail.com wrote:
> On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>>littleh...@gmail.com wrote:
>>
>>>On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>>
>>>>littleh...@gmail.com wrote:

>>
>>>>> I have a script and part of requires input from the user for time in
>>>>>the form of h(hours), m(minutes) or d(days)
>>>>>for example 1d eqautes to 1 day.
>>>>> I would like to set up a test in ksh to verify the input is in
>>>>>fact a numeric value followed by one of the letters above. Basically
>>>>>if a user enter 1f I want the script to ask for input again. If the
>>>>>user enters 1m then the script should go on. I understand this is a
>>>>>if then statement but I cannot get what to test for. I have tried the
>>>>>"||" but no luck. Thanks in advance.

>>
>>>>Not, it's not an "if" statement, it's a "case" statement:

>>
>>>>read time
>>>>case $time in
>>>> [0-9]*[hmd] ) echo "good" ;;
>>>> * ) echo "bad" ;;
>>>>esac

>>
>>>>Regards,

>>
>>>> Ed.

>>
>>>Thanks - two quick questions
>>>1) How do I set this up so if the test fails the user is forced to
>>>enter a valid response?

>>
>>time=""
>>while [ -z "$time" ]
>>do
>> printf "Enter a time in \"[0-9]*[hmd]\" format: " >&2
>> IFS= read -r time
>> case $time in
>> [0-9]*[hmd] ) : ;;
>> * ) printf "Invalid time \"%s\", try again.\n" "$time" >&2
>> time=""
>> ;;
>> esac
>>done
>>
>>
>>>2) Could this be an if/then statement or in a case like this is a case
>>>statement the "best practice"?

>>
>>I wouldn't even think of trying that with an if/then. "case" is best
>>practice.
>>
>> Ed.

>
>
> That did it thanks


Please check the other part of this thread where Stephane points out the
hole and we discuss fixes.

> - now this brings me to another question on using
> the case script. Can I use a variable for a response. For example
> lets say I want the operator to enter a valid user from a file such as
> the passwd file.
>
>
> Would something like this work? Thanks.
> user=""
> while [ -z "$user" ]
> do
> foobar=`awk -F: '{print $1}' /etc/passwd`
> printf "valid users are $foobar - please enter one:" >&2
> case $userin
> $foobar) : ;;
> *) printf "Invalid user \"%s\", try again.\n" "$user" >&2
> user=""
> ;;
> esac
> done
>
>


I'd do that as:

user=""
sep=" "
foobar=`awk -F: -v sep="$sep" '{print sep $1 sep}' /etc/passwd`
while [ -z "$user" ]
do
printf "valid users are:\n%s\nplease enter one: " "$foobar" >&2
IFS= read -r user
case $foobar in
*${sep}${user}${sep}* ) echo "good" ;;
* ) echo "bad"; user="" ;;
esac
done

i.e. match the list against the specific value entered rather than
trying to find the value entered in the list (which would require a grep).

Regards,

Ed.
  Réponse avec citation
Vieux 09/01/2008, 17h26   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

Ed Morton wrote:

> littlehere@gmail.com wrote:
>
>> On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>
>>> littleh...@gmail.com wrote:
>>>
>>>> On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>>
>>>
>>>>> littleh...@gmail.com wrote:
>>>
>>>
>>>>>> I have a script and part of requires input from the user for time in
>>>>>> the form of h(hours), m(minutes) or d(days)
>>>>>> for example 1d eqautes to 1 day.
>>>>>> I would like to set up a test in ksh to verify the input is in
>>>>>> fact a numeric value followed by one of the letters above. Basically
>>>>>> if a user enter 1f I want the script to ask for input again. If the
>>>>>> user enters 1m then the script should go on. I understand this is a
>>>>>> if then statement but I cannot get what to test for. I have tried
>>>>>> the
>>>>>> "||" but no luck. Thanks in advance.
>>>
>>>
>>>>> Not, it's not an "if" statement, it's a "case" statement:
>>>
>>>
>>>>> read time
>>>>> case $time in
>>>>> [0-9]*[hmd] ) echo "good" ;;
>>>>> * ) echo "bad" ;;
>>>>> esac
>>>
>>>
>>>>> Regards,
>>>
>>>
>>>>> Ed.
>>>
>>>
>>>> Thanks - two quick questions
>>>> 1) How do I set this up so if the test fails the user is forced to
>>>> enter a valid response?
>>>
>>>
>>> time=""
>>> while [ -z "$time" ]
>>> do
>>> printf "Enter a time in \"[0-9]*[hmd]\" format: " >&2
>>> IFS= read -r time
>>> case $time in
>>> [0-9]*[hmd] ) : ;;
>>> * ) printf "Invalid time \"%s\", try again.\n" "$time" >&2
>>> time=""
>>> ;;
>>> esac
>>> done
>>>
>>>
>>>> 2) Could this be an if/then statement or in a case like this is a case
>>>> statement the "best practice"?
>>>
>>>
>>> I wouldn't even think of trying that with an if/then. "case" is best
>>> practice.
>>>
>>> Ed.

>>
>>
>>
>> That did it thanks

>
>
> Please check the other part of this thread where Stephane points out the
> hole and we discuss fixes.
>
>> - now this brings me to another question on using
>> the case script. Can I use a variable for a response. For example
>> lets say I want the operator to enter a valid user from a file such as
>> the passwd file.
>>
>>
>> Would something like this work? Thanks.
>> user=""
>> while [ -z "$user" ]
>> do
>> foobar=`awk -F: '{print $1}' /etc/passwd`
>> printf "valid users are $foobar - please enter one:" >&2
>> case $userin
>> $foobar) : ;;
>> *) printf "Invalid user \"%s\", try again.\n" "$user" >&2
>> user=""
>> ;;
>> esac
>> done
>>
>>

>
> I'd do that as:
>
> user=""
> sep=" "
> foobar=`awk -F: -v sep="$sep" '{print sep $1 sep}' /etc/passwd`
> while [ -z "$user" ]
> do
> printf "valid users are:\n%s\nplease enter one: " "$foobar" >&2
> IFS= read -r user
> case $foobar in
> *${sep}${user}${sep}* ) echo "good" ;;
> * ) echo "bad"; user="" ;;
> esac
> done
>
> i.e. match the list against the specific value entered rather than
> trying to find the value entered in the list (which would require a grep).
>
> Regards,
>
> Ed.


Caveat - this will give false positives if the user enters globbing wild
cards such as "*". A better approach is probably to offer a numbered
list of names and ask the users to enter the corresponding number rather
than typing the full name.

Ed.
  Réponse avec citation
Vieux 09/01/2008, 17h54   #10
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Wed, 09 Jan 2008 11:26:23 -0600, Ed Morton wrote:

> Ed Morton wrote:
>
>> littlehere@gmail.com wrote:
>>
>>> On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>>
>>>> littleh...@gmail.com wrote:
>>>>
>>>>> On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>>>>
>>>>
>>>>>> littleh...@gmail.com wrote:


>>> - now this brings me to another question on using the case script.
>>> Can I use a variable for a response. For example lets say I want the
>>> operator to enter a valid user from a file such as the passwd file.
>>>
>>>
>>> Would something like this work? Thanks. user=""
>>> while [ -z "$user" ]
>>> do
>>> foobar=`awk -F: '{print $1}' /etc/passwd` printf "valid users are
>>> $foobar - please enter one:" >&2 case $userin
>>> $foobar) : ;;
>>> *) printf "Invalid user \"%s\", try again.\n" "$user" >&2 user=""
>>> ;;
>>> esac
>>> done
>>>
>>>
>>>

>> I'd do that as:
>>
>> user=""
>> sep=" "
>> foobar=`awk -F: -v sep="$sep" '{print sep $1 sep}' /etc/passwd` while [
>> -z "$user" ]
>> do
>> printf "valid users are:\n%s\nplease enter one: " "$foobar" >&2
>> IFS= read -r user
>> case $foobar in
>> *${sep}${user}${sep}* ) echo "good" ;; * ) echo
>> "bad"; user="" ;; esac
>> done
>>
>> i.e. match the list against the specific value entered rather than
>> trying to find the value entered in the list (which would require a
>> grep).
>>
>> Regards,
>>
>> Ed.

>
> Caveat - this will give false positives if the user enters globbing wild
> cards such as "*". A better approach is probably to offer a numbered
> list of names and ask the users to enter the corresponding number rather
> than typing the full name.
>
> Ed.


And the correct way to do this is with a "select" statement.
user=""
validusers=$( sed 's/:.*//' /etc/passwd)
PS3='Enter a number, or 0 to abort'
select user in $validusers
do
echo "user is '$user', REPLY is '$REPLY'"
if [ 0 = "$REPLY" ] ; then break ; fi
case "$user" in
"") continue;;
*) break ;;
esac
done

  Réponse avec citation
Vieux 09/01/2008, 19h09   #11
littlehelphere@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Jan 9, 12:54 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
> On Wed, 09 Jan 2008 11:26:23 -0600, Ed Morton wrote:
> > Ed Morton wrote:

>
> >> littleh...@gmail.com wrote:

>
> >>> On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>>> littleh...@gmail.com wrote:

>
> >>>>> On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> >>>>>> littleh...@gmail.com wrote:
> >>> - now this brings me to another question on using the case script.
> >>> Can I use a variable for a response. For example lets say I want the
> >>> operator to enter a valid user from a file such as the passwd file.

>
> >>> Would something like this work? Thanks. user=""
> >>> while [ -z "$user" ]
> >>> do
> >>> foobar=`awk -F: '{print $1}' /etc/passwd` printf "valid users are
> >>> $foobar - please enter one:" >&2 case $userin
> >>> $foobar) : ;;
> >>> *) printf "Invalid user \"%s\", try again.\n" "$user" >&2 user=""
> >>> ;;
> >>> esac
> >>> done

>
> >> I'd do that as:

>
> >> user=""
> >> sep=" "
> >> foobar=`awk -F: -v sep="$sep" '{print sep $1 sep}' /etc/passwd` while [
> >> -z "$user" ]
> >> do
> >> printf "valid users are:\n%s\nplease enter one: " "$foobar" >&2
> >> IFS= read -r user
> >> case $foobar in
> >> *${sep}${user}${sep}* ) echo "good" ;; * ) echo
> >> "bad"; user="" ;; esac
> >> done

>
> >> i.e. match the list against the specific value entered rather than
> >> trying to find the value entered in the list (which would require a
> >> grep).

>
> >> Regards,

>
> >> Ed.

>
> > Caveat - this will give false positives if the user enters globbing wild
> > cards such as "*". A better approach is probably to offer a numbered
> > list of names and ask the users to enter the corresponding number rather
> > than typing the full name.

>
> > Ed.

>
> And the correct way to do this is with a "select" statement.
> user=""
> validusers=$( sed 's/:.*//' /etc/passwd)
> PS3='Enter a number, or 0 to abort'
> select user in $validusers
> do
> echo "user is '$user', REPLY is '$REPLY'"
> if [ 0 = "$REPLY" ] ; then break ; fi
> case "$user" in
> "") continue;;
> *) break ;;
> esac
> done


OK - I went through the code and ran it an understand the concept.
However, I am having some difficulty in implementing it. Here is my
situation.

I have a flat file containing a hostname with service
ie -
for host alpha
alpha.cpu
alpha.memory
alpha.io
......
I go through the file and strip of the hostname leaving only the
available services. I then want the user to choose one service to get
details. I tried the following and while the output, etc is in place
the check does not seem to be working. Regardless of the info entered
it moves on to next step
....
service=""
SERVICE=`ls -1 /path_to_file/$server.* | awk -F\/ '{print $NF}'| awk -
F. '{print $2}'` #(this gets the info per host)
while [ -z "$service" ]
do
echo
echo "******Please choose a service:*****
$SERVICE
You may also choose all as well"
printf "Enter a valid service: " >&2
IFS= read -r service
case $service in
${SERVICE} ) : ;;
* ) printf "Invalid service \"%s\", try again.\n" "$service" >&2
service=""
;;
esac
done
....

Thanks once again. Also, can you provide a brief explanation on
what I am doing wrong here - for my own benefit.
  Réponse avec citation
Vieux 09/01/2008, 20h26   #12
littlehelphere@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Jan 9, 2:09 pm, littleh...@gmail.com wrote:
> On Jan 9, 12:54 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:
>
>
>
> > On Wed, 09 Jan 2008 11:26:23 -0600, Ed Morton wrote:
> > > Ed Morton wrote:

>
> > >> littleh...@gmail.com wrote:

>
> > >>> On Jan 8, 6:45 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> > >>>> littleh...@gmail.com wrote:

>
> > >>>>> On Jan 8, 4:44 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:

>
> > >>>>>> littleh...@gmail.com wrote:
> > >>> - now this brings me to another question on using the case script.
> > >>> Can I use a variable for a response. For example lets say I want the
> > >>> operator to enter a valid user from a file such as the passwd file.

>
> > >>> Would something like this work? Thanks. user=""
> > >>> while [ -z "$user" ]
> > >>> do
> > >>> foobar=`awk -F: '{print $1}' /etc/passwd` printf "valid users are
> > >>> $foobar - please enter one:" >&2 case $userin
> > >>> $foobar) : ;;
> > >>> *) printf "Invalid user \"%s\", try again.\n" "$user" >&2 user=""
> > >>> ;;
> > >>> esac
> > >>> done

>
> > >> I'd do that as:

>
> > >> user=""
> > >> sep=" "
> > >> foobar=`awk -F: -v sep="$sep" '{print sep $1 sep}' /etc/passwd` while [
> > >> -z "$user" ]
> > >> do
> > >> printf "valid users are:\n%s\nplease enter one: " "$foobar" >&2
> > >> IFS= read -r user
> > >> case $foobar in
> > >> *${sep}${user}${sep}* ) echo "good" ;; * ) echo
> > >> "bad"; user="" ;; esac
> > >> done

>
> > >> i.e. match the list against the specific value entered rather than
> > >> trying to find the value entered in the list (which would require a
> > >> grep).

>
> > >> Regards,

>
> > >> Ed.

>
> > > Caveat - this will give false positives if the user enters globbing wild
> > > cards such as "*". A better approach is probably to offer a numbered
> > > list of names and ask the users to enter the corresponding number rather
> > > than typing the full name.

>
> > > Ed.

>
> > And the correct way to do this is with a "select" statement.
> > user=""
> > validusers=$( sed 's/:.*//' /etc/passwd)
> > PS3='Enter a number, or 0 to abort'
> > select user in $validusers
> > do
> > echo "user is '$user', REPLY is '$REPLY'"
> > if [ 0 = "$REPLY" ] ; then break ; fi
> > case "$user" in
> > "") continue;;
> > *) break ;;
> > esac
> > done

>
> OK - I went through the code and ran it an understand the concept.
> However, I am having some difficulty in implementing it. Here is my
> situation.
>
> I have a flat file containing a hostname with service
> ie -
> for host alpha
> alpha.cpu
> alpha.memory
> alpha.io
> .....
> I go through the file and strip of the hostname leaving only the
> available services. I then want the user to choose one service to get
> details. I tried the following and while the output, etc is in place
> the check does not seem to be working. Regardless of the info entered
> it moves on to next step
> ...
> service=""
> SERVICE=`ls -1 /path_to_file/$server.* | awk -F\/ '{print $NF}'| awk -
> F. '{print $2}'` #(this gets the info per host)
> while [ -z "$service" ]
> do
> echo
> echo "******Please choose a service:*****
> $SERVICE
> You may also choose all as well"
> printf "Enter a valid service: " >&2
> IFS= read -r service
> case $service in
> ${SERVICE} ) : ;;
> * ) printf "Invalid service \"%s\", try again.\n" "$service" >&2
> service=""
> ;;
> esac
> done
> ...
>
> Thanks once again. Also, can you provide a brief explanation on
> what I am doing wrong here - for my own benefit.


Oh one other thing I would like the operator to be able to pick
multiple options, or an all option as well.
  Réponse avec citation
Vieux 10/01/2008, 06h33   #13
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to test for user input

On Wed, 09 Jan 2008 12:26:04 -0800, littlehere wrote:

> On Jan 9, 2:09 pm, littleh...@gmail.com wrote:
>> On Jan 9, 12:54 pm, Icarus Sparry <use...@icarus.freeuk.com> wrote:


>> > And the correct way to do this is with a "select" statement. user=""
>> > validusers=$( sed 's/:.*//' /etc/passwd) PS3='Enter a number, or 0 to
>> > abort'
>> > select user in $validusers
>> > do
>> > echo "user is '$user', REPLY is '$REPLY'" if [ 0 = "$REPLY" ]
>> > ; then break ; fi case "$user" in
>> > "") continue;;
>> > *) break ;;
>> > esac
>> > done

>>
>> OK - I went through the code and ran it an understand the concept.
>> However, I am having some difficulty in implementing it. Here is my
>> situation.
>>
>> I have a flat file containing a hostname with service ie -
>> for host alpha
>> alpha.cpu
>> alpha.memory
>> alpha.io
>> .....
>> I go through the file and strip of the hostname leaving only the
>> available services. I then want the user to choose one service to get
>> details. I tried the following and while the output, etc is in place
>> the check does not seem to be working. Regardless of the info entered
>> it moves on to next step
>> ...
>> service=""
>> SERVICE=`ls -1 /path_to_file/$server.* | awk -F\/ '{print $NF}'|
>> awk -F. '{print $2}'`
>> #(this gets the info per host) while [ -z "$service" ]
>> do
>> echo
>> echo "******Please choose a service:***** $SERVICE
>> You may also choose all as well"
>> printf "Enter a valid service: " >&2
>> IFS= read -r service
>> case $service in
>> ${SERVICE} ) : ;;
>> * ) printf "Invalid service \"%s\", try again.\n" "$service" >&2
>> service=""
>> ;;
>> esac
>> done
>> ...
>>
>> Thanks once again. Also, can you provide a brief explanation on
>> what I am doing wrong here - for my own benefit.

>
> Oh one other thing I would like the operator to be able to pick multiple
> options, or an all option as well.


Select is rather orientated to picking a single option at a time,
although it has an implicit loop. It is easy to add an "all" option, just
add it on the end of the list.

Once you have your variable with a list of services in it, e.g. SERVICES,
you can write something like

PS3="Choose the number of the next service you would like to add"
select s in all $SERVICES end
do
case $s in
all) services=$SERVICES ; break ;;
end) break ;;
"") echo "invalid response '$REPLY'" ;;
*) services="$services $s" ;;
esac
done

At this point you have a list of services chosen in the "$services"
variable, with a leading space if they were selected one by one.

You can alter PS3 in the loop to show what you already have if you want.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h33.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,34521 seconds with 21 queries