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 |
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:
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! |
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 |
HI demon_vox
Thank you Regards Asanka |
Thanks guys both works
regards Asanka |
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 |
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".
|
You might want to use variable substitution:
from the info bash manual: Quote:
|
Hay guys
How to give a black space in a shell script Regards Asanka |
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. |