Abhishek Kumar
Abhishek Kumar

Reputation: 17

shell program to display content of a file else print file not present in a directory based on user will input filename

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

Answers (1)

Tuxinose
Tuxinose

Reputation: 184

instead of

if [[ $filename == $file ]]

use

if [ -f "$filename" ]

... and it will work

BTW what is the content of $file?

Upvotes: 1

Related Questions