Reputation: 17
In a directory, there are many files. I need a script, in which it will ask filename, if file exist it will display the content of file else it will print file does not exist. In my code it is taking filename and displaying content of file but when I am giving wrong filename it is giving wrong output.
#! /bin/bash
echo "Enter the file name : "
read filename
for file in "."
do
if [[ $filename == $file ]]
then
echo "you entered : $filename"
echo "The content of file is : "
cat $filename
else
echo "File does not exist"
fi
done
Upvotes: 0
Views: 83
Reputation: 184
instead of
if [[ $filename == $file ]]
use
if [ -f "$filename" ]
... and it will work
BTW what is the content of $file?
Upvotes: 1