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
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
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)
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)
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)
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)
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)
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)
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)
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)
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)