Bash user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash user input
# 1  
Old 03-31-2010
Bash user input

Hi all, I currently have a script which uses read -p for user interaction. e.g.

Code:
read -p "New user? " user

Is it possible to have it so if the user enters nothing and just presses return it can resort to a specified value instead?

Thanks! Smilie
JayC89
# 2  
Old 03-31-2010
Code:
read -p "New user? " user

if [ "$user" == "" ]
then
echo "assign default value"
user=Default
else
echo "Ok"

fi

# 3  
Old 03-31-2010
Thank you for the prompt reply amitranjansahu, that worked a treat! Smilie

I'm also trying to get the script to accept varriables from the command line instead of running it and being prompted for the answers for example, instead of;

Code:
./test.sh
Whats the username:

I could just type

Code:
./test.sh username

And if the script isnt ran with the variables appended it still asks as it does at the moment.

Is that possible?
JayC89
# 4  
Old 03-31-2010
Code:
# Is there a parameter on the command line?
# If not then ask for the parameter. 
if [ ! "$1""X" = "X" ]
then
        user="$1"
else
        read -p "New user? " user
fi
#
if [ "${user}" == "" ]
then
        echo "assign default value"
        user="Default"
else
        echo "Ok"
fi


Last edited by methyl; 03-31-2010 at 09:24 AM..
# 5  
Old 03-31-2010
Quote:
Originally Posted by amitranjansahu
Code:
read -p "New user? " user

if [ "$user" == "" ]
then
echo "assign default value"
user=Default
else
echo "Ok"

fi

Alternatively, you can simply use $user in the following way:
Code:
${user:-default_value}

That will return the value of $user if it is set and not null; otherwise, it will return "default_value".

Regards,
Alister
# 6  
Old 03-31-2010
Code:
user="$1"   # take arg
# if empty then ask
[ "$user" = "" ] && read -p "New user? " user
# if value is empty, set some default
[ "$user" = "" ] && user="somedefaultvalue"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Question: HowTo Exit Script with User Input While Process is Running Mid-Loop?

Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
Discussion started by: hilltop_yodeler
9 Replies

2. Shell Programming and Scripting

BASH - read does not wait for user input in some circumstances

Hello. I am running 2 scripts : script_1 and script_2 These scripts are run as root Script 2 contains : #!/bin/bash # # ~/bin/script_2 # E_BAD_PARAM=115 # date2stamp () { date --date "$1" +%Y-%m-%d___%H:%M:%S } # USER_NAME=$1 NB_PARAM=$# PARAM0=$0 (2 Replies)
Discussion started by: jcdole
2 Replies

3. Shell Programming and Scripting

Using read to prompt for editable user input in Bash 3

Below is a simple script to prompt for user input while suggesting an editable default value at the prompt: shortname=user1 read -e -i $shortname -p "Please enter the username you would like to add: " input USERNAME="${input:-$shortname}" Please enter the username you would like to add:... (3 Replies)
Discussion started by: woodson2
3 Replies

4. Shell Programming and Scripting

[bash] how is proper way to validate user input

hi all, i have a script that need user input provide all variables that needed to complete a job. this is my current script: echo "type file source and it full path :" read INPUTFILE if || ; then echo "ERROR: you didn't enter a file source or file source is not... (2 Replies)
Discussion started by: makan
2 Replies

5. Shell Programming and Scripting

Help with Bash user input

I am starting to learn how to use bash and I would like the script to do the following: Asks the user for his/her name Asks the user for one number Asks the user for another number Then it adds the two numbers, Also multiply the two numbers I have the part where you get the name, and I... (3 Replies)
Discussion started by: boyboy1212
3 Replies

6. Shell Programming and Scripting

Bash Script verify user input is not empty and is equal to a value

I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry. I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Discussion started by: spartiati
4 Replies

7. Shell Programming and Scripting

BASH Varible nesting and user input

Well, I think I've managed to take two different issues and conglomerate them into and embarrasing mess. #!/bin/bash # Set some variables dir1=/path/that/isnt/variable/$variabledir/dir/ dir2=/path/that/isnt/variable/$variabledir/important/"$variabledir"-subdirectory/path/ echo "Gimme... (7 Replies)
Discussion started by: karlp
7 Replies

8. UNIX for Dummies Questions & Answers

BASH validate user input

Hey, im trying to validate a user input and need some help. The input needs to be just a single letter. Im using a case to so this eg: read answer case $answer in *) echo "OK" ;; *) echo "This is a number" read answer ;; *) echo... (2 Replies)
Discussion started by: 602chrislys
2 Replies

9. Shell Programming and Scripting

BASH: Any Way to Get User Input Without Requiring Them to Hit the Enter Key?

I'm working on making a menu system on an HP-UX box with Bash on it. The old menu system presents the users with a standard text menu with numbers to make selections. I'm re-working the system and I would like to provide something more akin to iterative search in Emacs. I have a list of 28... (2 Replies)
Discussion started by: deckard
2 Replies

10. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies
Login or Register to Ask a Question