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...
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.
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!
#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=$(find "${RIG}"*"${UNIQUERUNS}"* | wc -l)
HOME_EXIST=$(find "${HOME}"*"${UNIQUERUNS}"* | wc -l)
EXTERNAL_EXIST=$(find "${EXTERNAL}"*"${UNIQUERUNS}"* | wc -l)
let EXIST_COUNT=${RIG_EXIST}+${HOME_EXIST}+${EXTERNAL_EXIST}
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} = 3 ]
then
BASE=("${RIG}"*"${UNIQUERUNS}"*)
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