Check the syntax: to put other statements between
for and
do is not permitted. Based on your requirements, you have to simply check if the input is equal to 0 and eventually
break the loop:
Code:
#!/bin/bash
echo "Enter file name"
echo "You can enter maximum 3 file"
for (( i=1; i<=3; i++ ))
do
read -p "Enter file ${i}: " tmpfile
if [ "$tmpfile" = "0" ]
then
break
else
eval "file_$i=$tmpfile"
fi
done
echo file_1 is $file_1
echo file_2 is $file_2
echo file_3 id $file_3
As you can see, in this example I used a temporary variable "tmpfile" to read the user's input. Then based on the condition you can either exit the loop using the break statement or assign the value of tmpfile to a variable.
The assigment is done using indirect reference: the variable name contains the value of another variable ($1) so you have to use the eval statement to let the shell expand the variable name and then execute the command. See the Advanced Bash Scripting Guide,
here for more details.