Reputation: 415
I'm trying to build a script that ask for user to type the name of the file, and once written, it simply shows what is inside this file, entirely.
So for instance,
Let's say I have a directory located in, /home/evaluation/, which contains severals files :
In /home/evaluation/file01,
Lorem ipsum dolor sit amet.
In /home/evaluation/file02,
Lorem ipsum sit amet.
In /home/evaluation/file03,
Lorem ipsum dolor
I'm looking forward to build a script that will ask me to write, the file name, I want to read, and once this file written, it will show all its content.
So if I type : file01, it will show me :
Lorem ipsum dolor sit amet.
Else, if the file doesn't exist in the directory, then it shall be written : "no file found".
Upvotes: 0
Views: 287
Reputation: 16758
Try this and see if you get what you are looking for
#!/bin/bash
echo Enter file name # ask for the file name
read fileName # get the a file name from the user and store it in fileName
if [ ! -f $fileName ] ; then # check to see if the file exists
echo "File not found" # print a message if it does not
exit # all done so exit
fi
# cat the file if we are still here , ie the file exists
cat $fileName
if []
in bash defines a test so
and -f file_name
checks to see if the file exists and is a regular file
so [ ! -f $fileName ]
will be true if the file does not exist, so then the message will be printed, otherwise the contents will be printed
Upvotes: 2