LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 07-03-2006, 11:19 PM   #1
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Rep: Reputation: 34
bash script Want to capture return key and assign a value


Hi

I want to prompt a user to enter a value if he press enter I want to assign a default value in a if statement.

#!/bin/bash
echo -en "Enter a value : "
read T1

if [ "$T1" = "Return key" ]; then
I want to assign a default value to T1.
else
Take the value that the user entered to the T1 and continue.
fi


How am I supposed to do this

Regards

Asanka
 
Old 07-03-2006, 11:47 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
Hi,
if the user just inputs the return key, then you will have an empty string in your T1 variable. So, your code would end up like this:

Code:
	
bash script Want to capture return key and assign a value
Hi

I want to prompt a user to enter a value if he press enter I want to assign a default value in a if statement.

#!/bin/bash
echo -en "Enter a value : "
read T1

if [ "$T1" == "" ]; then
  T1="pepe"
fi
Consider two things:

1) To compare two strings you need to use the == operator (almost sure that it's best than just = )

2) The most important thing is that in your particular example, you dont need an else clause. If the user input something, then you dont want to change so you dont need to execute any code. If the user hasnt input a thing, then you will change and continue.

I hope this is useful

Cheers!
 
Old 07-03-2006, 11:49 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
EDIT: demon_vox's solution looks like it won't trip over spaces - I'd recommend == over -z if that's the case...

The following should do what you want:
Code:
#!/bin/sh

echo -en "Enter a value : "
read T1

if [ -z "$T1" ]; then
  T1="DEFAULT_VALUE"
  echo "User just pressed ENTER so T1 is now $T1"
else
  echo "User entered $T1"
fi
It will also say that the user just pressed ENTER if they enter a space and press ENTER. I think this is to do with the standard IFS being a space, but didn't have time to check it out.

Last edited by gilead; 07-03-2006 at 11:52 PM.
 
Old 07-04-2006, 12:48 AM   #4
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Original Poster
Rep: Reputation: 34
HI demon_vox

Thank you

Regards
Asanka
 
Old 07-04-2006, 12:56 AM   #5
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Original Poster
Rep: Reputation: 34
Thanks guys both works

regards
Asanka
 
Old 07-04-2006, 05:46 AM   #6
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Original Poster
Rep: Reputation: 34
Hi Guys

Now I can get all the parameters as I want, this is wat I want to do.

/usr/local/sbin/smbldap-useradd.pl $WINUS $MKHOME $PGROUP $PGROUP_ID $SGROUP $SGROUP_ID $PASSRE $UNAMES $LOSHELL $LOSHELL_D"

if I dont enter any value like PGROUP he leaves additional space.

is there a way like ot put these parameters in a loop and add each variable if that variable has a value a space separating each valid variable.

regards

Asanka
 
Old 07-04-2006, 05:56 AM   #7
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
xargs reads items from STDIN and passes them as arguments to an arbitrary command. So you can write a loop that prints those variable contents, and pipe the loop's output to "xargs /usr/local/sbin/smbldap-useradd.pl".
 
Old 07-04-2006, 06:36 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
You might want to use variable substitution:
from the info bash manual:
Quote:
`${PARAMETER:-WORD}'
If PARAMETER is unset or null, the expansion of WORD is
substituted. Otherwise, the value of PARAMETER is substituted.
 
Old 07-07-2006, 01:51 AM   #9
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Original Poster
Rep: Reputation: 34
Hay guys

How to give a black space in a shell script

Regards

Asanka
 
Old 07-07-2006, 02:38 AM   #10
procfs
Member
 
Registered: Jan 2006
Location: Sri Lanka
Posts: 504

Original Poster
Rep: Reputation: 34
Hi

echo -en \\t\\t\\t"Enter the secondary group y or n (Default no) : "
read SGROUP

if [ "$SGROUP" = "y" ]; then
SGROUP="-G"
echo -en \\t\\t\\t"Enter the secondary group (Numeric only): "
read SGROUP_ID
# echo "your value $SGROUP"
# echo "your value $SGROUP_ID"
else
SGROUP=""
fi

sleep 1


at the place
where else
SGROUP=""
fi

with SGROUP="" can I send distructive backspace and how to do this.

regards

Asanka
 
  


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
How do you assign my USB MIC(ak5370) as the capture device Dragonopolis VectorLinux 2 05-06-2006 06:10 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 05:56 PM
Bash return question merchtemeagle Programming 2 11-02-2005 11:36 AM
how to assign password for a user in shell script mtest Programming 10 10-29-2003 07:52 AM
return key on shell script chupacabra Programming 2 10-22-2002 01:11 PM


All times are GMT -5. The time now is 04:29 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration