I want to ask the user to enter an X amount of file names. I want to put those names into an array and then loop back through them to verify they are in the directory. 1st- How would I assign the value to an array and what is the correct syntax. 2nd- how would i reference that array after I assigned it to check to see if the file exist. Thanks.
something like
#!/bin/bash
i=0
while true # read user input
do
read -p "enter file name (blank if finished) : "
[ -z "$REPLY" ] && break
FILE[$i]=$REPLY
((i++))
done
for ((i=0; i<${#FILE[@]}; i++)) # check if files exist
do
F=${FILE[$i]}
if [ -f "$F" ]
then echo "File $F exists"
else echo "File $F does not exist"
fi
done
Maybe it's better - and sure it's easier - to check files before :
#!/bin/bash
i=0
while true # read user input and check files
do
read -p "enter file name (blank if finished) : " F
[ -z "$F" ] && break
[ -f "$F" ] || { echo "File does not exist" && continue; }
FILE[$i]=$F
((i++))
done
for ((i=0; i<${#FILE[@]}; i++)) # print files
do
echo -e "File number $i\t: ${FILE[$i]}"
done
Thanks. Your example helped.