Read input from Keyboard, do not proceed if no input

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read input from Keyboard, do not proceed if no input
# 1  
Read input from Keyboard, do not proceed if no input


I am working on a script, which requests users to enter input.
Ex: read -p "Please enter your email id:" email

I don't want users skipping this entry, this has to be mandatory.I dont want to proceed without input.

I can do a check if variable $email is empty and proceed if not.But, i have around 20 inputs and it is hard to check on all the inputs and if user does not enter variable 19 i don't want to start from 1 again.

Please do the needful.

# 2  
I'm afraid you'll have to do the hard work and validate every single input. Maybe using a function?
# 3  
why don't you put the mandatory ones at the beginning? then throw into a while loop? this chokes off any continuation rather than repeating the loop.

Last edited by f77hack; 02-25-2016 at 04:24 PM.. Reason: added stuff
# 4  
If your idea of valid is just "not an empty string", you can loop on the request for each variable sequentially with something like:
while [ -z "$var1" ]
do	printf 'Enter variable 1 value: '
	read -r var1
	[ -z "$var1" ] && echo 'Variable 1 cannot be empty; try again.'
while [ -z "$var2" ]
do	printf 'Enter variable 2 value: '
	read -r var2
	[ -z "$var2" ] && echo 'Variable 2 cannot be empty; try again.'


instead of asking the user to enter all 20 variables and then verifying all of the values and restarting from the beginning if one of them had an error.
# 5  
Don, why doing so much double effort?
#	Variables
	# Expand the task list with words that describe the task
	declare TASK_LIST="name email"
	declare -A ANSWERS
#	Messages
	# Use the words from the task-list as string identifier
	MSG_STR_NAME="What is your name"
	MSG_STR_EMAIL="What is your email"
#	Action
	# Work the list
	for task in $TASK_LIST
		# Prepare the message string
		# Loop while input is empty
		while [ -z "${ANSWERS[$task]}" ]
			echo ${!str}
			read input
#	Your work to do....
	echo "Result values are:"
	for item in $TASK_LIST
		echo "$item :: ${ANSWERS[$item]}"

Hope this helps

Looks for me like:
0 ~/tmp $ bash aravindadla 
What is your name
What is your email

Result values are:
name :: Simon
email :: something

# 6  

Your options looks more feasible to me.
Will try them today and get back.

# 7  
How about another approach in a recent shell (e.g. bash 4)? Given file
First Name,FNAME  
Last Name,LNAME    

and a function declared as
getvar() { while [ ! ${!2} ]; do read -p "$1: " "$2"; done }

this would fill all the variables as requested with minor efforts.
while IFS=, read PR VAR <&3; do getvar "$PR" "$VAR"; echo $VAR"="${!VAR}; done 3< file
First Name: Joe
Last Name: Miller
Birthday: 12.7.88

Add validations and/or error checking to taste.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #65
Difficulty: Easy
In the TCP/IP model, end-to-end connectivity is provided from host-to-host in the transport layer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get input from keyboard with watch?

Hello, i`m trying to create an network monitoring script and i dont know how to make affect that script by pressing an key from keyboard and that script runs not in while or for or any other loop, but with bash command watch for example: i have created an file (for example check) with content... (0 Replies)
Discussion started by: bacarrdy
0 Replies

2. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

3. Homework & Coursework Questions

How to read user keyboard input inside the case?

I need to Write a shell script that allows some system-administration tasks to be preformed automatically from a menu-driven interface. with automated following tasks: Copy directory tree Delete files or directories Output Information (this part is done ) *Copy directory tree The “Copy... (2 Replies)
Discussion started by: femchi
2 Replies

4. Shell Programming and Scripting

Input keyboard keys upon program launch

Hey guys, first post! I want to write a script that will wait 1 second and then input the keys CTRL+ALT+J to the application i just opened. The program is dosbox, and thay key combination starts video recording :) I have no idea how to program this, can anyone help please? thanks in advance! (2 Replies)
Discussion started by: brunobliss
2 Replies

5. Shell Programming and Scripting

sed execution with input from keyboard

> sed '' Hello hi Hello output How hi output ^D How > sed should take each line as input, process and output the result. In the above scenario the input is passed from keyboard and the output of 'Hello' as you can see is displayed on the screen after 'hi' is passed as input but not as... (1 Reply)
Discussion started by: loggedin.ksh
1 Replies

6. Shell Programming and Scripting

Keyboard input question

How would I change up a script that currently has something like: bash script echo what's 1 2 3 4? then using read 1 2 3 4 I type 1 2 3 4. so in the script i can do stuff like echo $1 $2 $3 $4 and such... i was just doing echo "1 2 3 4"|bash script But was wondering how could I... (5 Replies)
Discussion started by: biopulse
5 Replies

7. Programming

Keyboard Input

Does anyone know how do you determine the user idle time of STDIN in order to log the user out for being idle too long. I would like to write a c program to do this but I it is not clear upon how to determine idle time from keyboard input. I have found that the "who.c" source file uses the last... (4 Replies)
Discussion started by: cpaquette
4 Replies

8. Programming

Detecting Keyboard Input without return

Hi, I need a way to detect the up and down arrow key inputs for my program. I do not want to wait for the return key to be entered(so that rules out getch() and family). Also I need to process several of these inputs in parallel, by servicing each request with a thread. Is that possible? ... (4 Replies)
Discussion started by: ravneetd
4 Replies

9. Shell Programming and Scripting

Don't show keyboard input on terminal

I am developing a script that will run with '/bin/ksh' shell. The script is intended to receive a password by keyboard input, but for security reasons I would like to hide what the user is typing. The keyboard input is being caught by 'read' command. exmaple : echo "Please type your new... (1 Reply)
Discussion started by: marianor31
1 Replies

10. Shell Programming and Scripting

suppressing keyboard input

Setup Info: This User Id and Password mention below are being used with the ISQL command to connect to a sybase database so they are likely to not be the same as those that were signed on from the session. Situation: Using a korn shell, the shell prompts for a User Id and Password. During the... (1 Reply)
Discussion started by: anthreedhr
1 Replies

Featured Tech Videos