.sh script / Check for file in two directories then cat the files

Hi,

Here is what i'm trying to do,

-Check in two directories for a user inputed filename
-Then cat all the version found of the file to the current screen

I am a total nembie to programming, here what i have done so far....

#!/bin/bash
#Check in /home/loonatic and /var/named/master
#If the file exist cat it.
dir1="/home/loonatic/"
dir2="/var/named/master/"
if [ ls ${dir1} ${dir2} ${filename} ] ; then
do [[ -f $dir1/$filename ]] && cat "$dir1/$filename"
do [[ -f $dir2/$filename ]] && cat "$dir2/$filename"
else
exit

If you can point me toward a solution or reading material that would be great.

Thanks

Try this:

#!/bin/bash

f="$1" #The filename is the argument of the script
[ -z "$1" ] && read -p'Input a file: ' f #No argument? We explicitly ask the user

DIR1="/home/loonatic/"
DIR2="/var/named/master/"

#while [ "$f" ]; do
	for d in "$DIR1" "$DIR2"; do
		[ -f "$d$f" ] && {
			echo "-> $d$f"
			cat "$d$f"
		}
	done
#	shift; f="$1"; done

exit 0

Usage: ./script [optional filename]
For the fun of it, uncomment the 2 commented lines to add support for multiples arguments: ./script file1 file2 file3...

1 Like

Thank you so much, you rock =)

I have a few scripts to build like that and you gave me a good fondation to start with.

Thanks again