Capturing multiple values from user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing multiple values from user input
# 1  
Old 01-11-2013
Capturing multiple values from user input

Hello,

I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user.

This is what my output looks like...
Code:
Old files to be deleted...
1 file1
2 file2

Then this bit of code is run after the above is output to keep or delete files.

Code:
echo "Would you like to keep any found  files? (y/n)"
read USER_ANSWER

while [ ${USER_ANSWER} == "y" ]
do
	echo "Type the number associated with the file(s) to be kept."
	read  USER_NUM
	if [ $USER_NUM -gt ${#FILES[@]} -o $USER_NUM -le 0 ]
	then
		echo -e "Invalid."
	else
		echo -e "\nKeeping: " ${FILEST[${USER_NUM}]}"\n"
		touch ${FILES[${USER_NUM}]}
		echo -e "Removing "${FILES[@]}
		break
	fi
done
if [ ${USER_ANSWER} == "n" ]
then
	echo -e "\nDeleting "${FILES[@]}
	rm -r  ${FILES[@]}
fi

And from this point I prompt the user and ask which files they want to keep if any, and they select a number from the above output and it keeps that file. But I want to make it so the user can input more than one number for keeping the files, like typing 1 2 3 4 if they want to keep those four files.

How do I do this? I'm new to bash the more explanation contained in the answer the more I can learn Smilie

Thanks
# 2  
Old 01-11-2013
How to do that really depends on how the rest of your program works. Could you post the whole thing?
# 3  
Old 01-11-2013
I don't typically write in bash so there are probably a lot of errors but if you could help me with the multiple variable user input that would help!

Code:
#Location variables

TIME=" -maxdepth 1 -mtime +31"
RIG="/results/fakedir/"
EXTERNAL="/media/ExternalArchive/"
HOME="/results/analysis/output/Home/"

#Determines total amount of files found by the find query

RIG_COUNT=$(find  ${RIG}${TIME} | wc -l)
EXTERNAL_COUNT=$(find ${EXTERNAL}${TIME} | wc -l)
HOME_COUNT=$(find ${HOME}${TIME} | wc -l)
let ID_COUNT=${RIG_COUNT}+${HOME_COUNT}
let TOTAL_COUNT=${RIG_COUNT}+${EXTERNAL_COUNT}+${HOME_COUNT}

#Finds files older than specified time

FIND_RIG=$(find ${RIG}${TIME})
FIND_EXTERNAL=$(find ${EXTERNAL}${TIME})
FIND_HOME=$(find ${HOME}${TIME})

#Puts the results of each of the "finds" into an array by its 
#basename and extracts the unique ID of "SN-##"
#The "*_A" array holds the basename of a file, excluding the full pathname
#The "*_ID" array stores the unique IDs of the runs. 

j=0
for a in ${FIND_RIG}
do
	RIG_PATH[j]=${a}
	RIG_ID[j]=`echo ${RIG_PATH[j]} | sed -e 's#.*\(SN2-\([0-9][0-9]\)\).*#\1#g'`;
	let j++
done

j=0
for b in ${FIND_EXTERNAL}
do
	EXTERNAL_PATH[j]=${b}
	EXTERNAL_ID[j]=`echo ${EXTERNAL_PATH[j]} | sed -e 's#.*\(SN2-\([0-9][0-9]\)\).*#\1#g'`;
	let j++
done

j=0
for c in ${FIND_HOME}
do
	HOME_PATH[j]=${c}
	HOME_ID[j]=`echo ${HOME_PATH[j]} | sed -e 's#.*\(SN2-\([0-9][0-9]\)\).*#\1#g'`;
	let j++
done

#Creates an array with all of the unique IDs in it.

OLDIFS="$IFS"
IFS=$'\n'
UNIQUERUNS=(`for i in "${RIG_ID[@]}" "${EXTERNAL_ID[@]}" "${HOME_ID[@]}" ; do echo "$i" ; done | sort -du`)
IFS="$OLDIFS"

#Checks to see if the unique ID exists in all directories.
#If the ID exists in all directories EXIST_COUNT will be 3.

for ((i=0; i<=${TOTAL_COUNT}; i++))
do
	RIG_EXIST[i]=$(find  "${RIG}"*"${UNIQUERUNS[i]}"* | wc -l)
	HOME_EXIST[i]=$(find  "${HOME}"*"${UNIQUERUNS[i]}"* | wc -l)
	EXTERNAL_EXIST[i]=$(find  "${EXTERNAL}"*"${UNIQUERUNS[i]}"* | wc -l)
	let EXIST_COUNT[i]=${RIG_EXIST[i]}+${HOME_EXIST[i]}+${EXTERNAL_EXIST[i]}
done

echo -e "\nRun(s) to be deleted."
echo -e "\n******************************\n"

N=1
for ((i=0; i<=${TOTAL_COUNT}; i++))
do
	if [ ${EXIST_COUNT[i]} = 3 ]
	then
		BASE=("${RIG}"*"${UNIQUERUNS[i]}"*)
		echo $N ${BASE##*/}
		I_COUNT[N]=${BASE}
		let N++
	fi
done
#echo ${I_COUNT[@]}
echo -e "\n******************************\n"

echo "Would you like to keep any found  files? (y/n)"
read USER_ANSWER

while [ ${USER_ANSWER} == "y" ]
do
	echo "Type the number associated with the run(s) to be kept."
	read  USER_NUM
	if [ $USER_NUM -gt ${#I_COUNT[@]} -o $USER_NUM -le 0 ]
	then
		echo -e "Invalid run."
	else
		echo -e "\nKeeping: " ${I_COUNT[${USER_NUM}]}"\n"
		#touch ${I_COUNT[${USER_NUM}]}
		echo -e "Removing "${I_COUNT[@]}
		break
	fi
done
if [ ${USER_ANSWER} == "n" ]
then
	echo -e "\nDeleting runs "${I_COUNT[@]}
	#rm -r  ${I_COUNT[@]}
fi

# 4  
Old 01-12-2013
If your shell offers it (bash and ksh do), use the selectbuiltin:
Code:
$ select A in a b c d; do echo $A, $REPLY; done
1) a
2) b
3) c
4) d
#? 1
a, 1
#? 1-4
, 1-4

and evaluate §REPLY.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Simple capturing of keyboard input without interruption

I would like to make a function or command that checks for keyboard input without interrupting the program and exits the program when a key is pressed (perhaps the 'q' key). The program below monitors and prints/executes commands upon a change in primary (mouse selection) or clipboard buffer. If... (4 Replies)
Discussion started by: bedtime
4 Replies

2. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Programming

Filling the class from values taken from user input

I have a program that accepts user input. For example I have mdacc that the user sets the value. I then have a class which stores the value set by the user. I use set_param to set the values in the class. I pass through it the list of user defines arguments from argv. What would be the opinion on... (0 Replies)
Discussion started by: kristinu
0 Replies

4. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

5. Shell Programming and Scripting

awk + gsub to search multiple input values & replace with located string + extra text

Hi all. I have the following command that is successfully searching for any one of the strings on all lines of a file and replacing it with the instructed value. cat inputFile | awk '{gsub(/aaa|bbb|ccc|ddd/,"1234")}1' > outputFile This does in fact replace any occurrence of aaa, bbb,... (2 Replies)
Discussion started by: dazhoop
2 Replies

6. Programming

How to accept multiple lines input from User in C?

Hi I want to accept multiple lines input with spaces from User and i have a working code like this. char sRes; char sReq; printf("Please enter request:"); scanf("%",sReq); /* Accept the input from user */ printf("\nPlease enter response:"); scanf("%",sRes); but the... (4 Replies)
Discussion started by: AAKhan
4 Replies

7. Shell Programming and Scripting

Capturing script output and input to log

Hi Is there a way that I can capture a shell script (both output and input) to a log file where I can analyze it? Thanks (6 Replies)
Discussion started by: nimo
6 Replies

8. Shell Programming and Scripting

Displaying default values when accepting input from user

Is there a way to display the default answer when accepting input from the user in the unix script.. e.g. ans="n" read $ans?"Enter y to continue n to exit:" altough ans contains n the message doesn't display the current contents on ans .. you get Enter y to continue n to exit: (8 Replies)
Discussion started by: flopster
8 Replies

9. UNIX for Dummies Questions & Answers

Capturing Input Parameters on Shell Script

i have this basic line of code that doesn't work. i simply want to get the input parameter strings but when the script is run it appears that the first parameter is assigning the value to the second parameter. #!/bin/sh pdir=`pwd` p1=$1 p2=$2 echo "directory: $pdir\n" echo "parameter... (2 Replies)
Discussion started by: wtolentino
2 Replies

10. UNIX for Dummies Questions & Answers

How to display values from user input array?

Hi all, I wrote a script that reads inputs from user and store in array named "input". The number of elements in the array is not fixed - determined only after user exit the while loop that reads the array values : x=1 echo "Enter first value" read input while } != "exit" ] do ... (1 Reply)
Discussion started by: luna_soleil
1 Replies
Login or Register to Ask a Question