Help script shell find fichier

Hello,
I am looking for a shell script that can
1- take as input a variable, like "server.cpu"
2- do a search for that variable in a directory that contains subdirectories.
The search will start at the last subdirectory working up to the top level if I can not find the file
3- exemple : variable aix1.cpu
directory: / DIR / SDIR1 / SDIR2 / SDIR3 / SDIR4 /
ls /DIR/SDIR1/SDIR2/SDIR3/SDIR4/aix1.cpu (if I can find the file in / SDIR4 / so I end program then displays or I look in the /DIR/SDIR1/SDIR2/SDIR3/aix1.cpu (if I find the file so the program end if not find I look in /DIR/SDIR1/SDIR2/aix1.cpu (ditto ..) if I do not find the file I need to display a file that is default.cpu located in the / DIR /
thank you for your help
George

Is this a homework assignment?

What operating system and shell are you using?

What have you tried to solve this problem?

1 Like

No, it's not a homework
i have AIX 6.1

OK. I repeat:
What shell are you using?

What have you tried to solve this problem?

i like to-have a target file for every servername services ( to supervise the servers)

---------- Post updated at 02:58 AM ---------- Previous update was at 02:57 AM ----------

Korn shell

---------- Post updated at 03:03 AM ---------- Previous update was at 02:58 AM ----------

the problem is that today I have a single file for all servers "aix.CPU", I want to change that I want to put for each server a file.cpu ( this for check services )

The following script seems to do what you want:

#!/bin/ksh
IAm="${0%%*/}"
if [ $# -ne 2 ]
then	printf 'Usage: %s directory file\n' "$IAm"
	exit 1
fi
dir="$1"
file="$2"
while :
do	if [ -e "$dir/$file" ]
	then	printf 'File "%s" found in directory "%s"\n' "$file" "$dir"
		exit 0
	fi
	parent_dir="$(dirname "$dir")"
	if [ "$parent_dir" = "$dir" ]
	then	printf 'File "%s" not found.\n' "$file"
		exit 1
	fi
	dir="$parent_dir"
done
1 Like

hi, thks for you help it work 's
I add the cat to find the default fich if not found in any directroy
thks

 #!/bin/ksh
IAm="${0%%*/}"
if [ $# -ne 2 ]
then    printf 'Usage: %s directory file\n' "$IAm"
    exit 1
fi
dir="$1"
file="$2"
while :
do    if [ -e "$dir/$file" ]
    then    printf 'File "%s" found in directory "%s"\n' "$file" "$dir"
     cat $dir/$file
        exit 0
    fi
    parent_dir="$(dirname "$dir")"
    if [ "$parent_dir" = "$dir" ]
    then    printf 'File "%s" not found.\n' "$file"
    ext=`echo ${file} | awk -F"." '{ print ($2) }'`
    cat /data/test/recept/EON/default/default.$ext
     exit 0
    fi
    dir="$parent_dir"
done

I'm glad it is working for you, but please consider the following comments...

Note that if #!/path/to/interpreter does not appear at the start of the 1st line in your file (with no leading spaces or tabs), your script will be run by whatever the system you're on uses as a default; not by the interpreter you list in this comment ( /bin/ksh in this example).

If you get into the habit of using consistent indentation for all of your code blocks, you will save yourself MANY headaches in the future when you are trying to figure out why some line of code isn't running when you thought it would, and it will make debugging much easier when the shell tells you have a missing ) , } , done , esac , or fi .

And, using awk to get the filename extension out of a string variable is a very slow, expensive way to perform a standard shell variable expansion. Try changing:

    if [ "$parent_dir" = "$dir" ]
    then    printf 'File "%s" not found.\n' "$file"
    ext=`echo ${file} | awk -F"." '{ print ($2) }'`
    cat /data/test/recept/EON/default/default.$ext
     exit 0
    fi

to:

    if [ "$parent_dir" = "$dir" ]
    then
        printf 'File "%s" not found.\n' "$file"
        cat /data/test/recept/EON/default/default."${file##*.}"
        exit 0
    fi