LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash script Want to capture return key and assign a value (https://www.linuxquestions.org/questions/programming-9/bash-script-want-to-capture-return-key-and-assign-a-value-460746/)

procfs 07-03-2006 10:19 PM

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

demon_vox 07-03-2006 10:47 PM

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!

gilead 07-03-2006 10:49 PM

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.

procfs 07-03-2006 11:48 PM

HI demon_vox

Thank you

Regards
Asanka

procfs 07-03-2006 11:56 PM

Thanks guys both works

regards
Asanka

procfs 07-04-2006 04:46 AM

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

spirit receiver 07-04-2006 04:56 AM

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".

jschiwal 07-04-2006 05:36 AM

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.


procfs 07-07-2006 12:51 AM

Hay guys

How to give a black space in a shell script

Regards

Asanka

procfs 07-07-2006 01:38 AM

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


All times are GMT -5. The time now is 11:21 AM.