I am trying to write a simple bash script that displays the contents of files.
#!/bin/bash
echo 'Input the path of a file or directory...'
read File
if [ -e $File ] && [ -f $File ] && [ -r $File ]
then
echo 'Displaying the contents of the file '$File
cat $File
elif [ -d $File ] && [ -r $File ]
then
echo 'Displaying the contents of the directory '$File
for FILE in `ls -R $File`
do
cd $File/$FILE
echo 'Displaying the contents of the file '$FILE
cat $FILE
done
else
echo 'Oops... Cannot read file or directory !'
fi
The user should input a file or directory path. If the user inputs a file the program displays it with cat. If the user inputs a directory it should display the content of all files including those in the subdirectories. That part of the program doesn't work very well. I would like to get a result that doens't display errors like 'No such file or directory' but only the content of files. Can you help me ? Thanks in advance.