Help script shell find fichier


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help script shell find fichier
# 1  
Old 03-14-2015
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
# 2  
Old 03-14-2015
Is this a homework assignment?

What operating system and shell are you using?

What have you tried to solve this problem?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 03-14-2015
No, it's not a homework
i have AIX 6.1
# 4  
Old 03-14-2015
OK. I repeat:
What shell are you using?

What have you tried to solve this problem?
# 5  
Old 03-14-2015
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 )
# 6  
Old 03-14-2015
The following script seems to do what you want:
Code:
#!/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

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 03-14-2015
hi, thks for you help it work 's
I add the cat to find the default fich if not found in any directroy
thks


Code:
 #!/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


Last edited by Don Cragun; 03-14-2015 at 05:17 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and Replace in Shell script

Friends, I have more than 1000 lines in text file which needs to be converted as UPPERCASE by adding _ com.sun.url=www.sun.com com.ssl.port=808 com.ui.path=/apps/ssi Expected output com.sun.url=_COM.SUN.URL_ com.ssl.port=_COM.SSL.PORT_ com.ui.path=_COM.UI.PATH_ Thanks in... (4 Replies)
Discussion started by: baluchen
4 Replies

2. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

3. Shell Programming and Scripting

How to find out the shell of the shell script?

Hello My question is: How to find out the shell of the shell script which we are running? I am writing a script, say f1.sh, as below: #!/bin/ksh echo "Sample script" From the first line, we can say this script will run in ksh. But, how can we prove it? Can we print anything inside... (6 Replies)
Discussion started by: guruprasadpr
6 Replies

4. Shell Programming and Scripting

Shell Script Find in File

Right, noob to shell scripting, playing a round for practice, wrote the following but it doesn't seem to work as expected, how could I fix/improve this script? #!/bin/bash #set -v #set -x case $# in 1) echo Searching for $1 in '*'; find . -iname '*' 2>/dev/null | xargs grep "$1" -sl... (3 Replies)
Discussion started by: Pezmc
3 Replies

5. AIX

Ajout colonne calcuée ds un fichier .txt

Mon but est de créer un script ksh ou commande aix permettant ceci. J'ai un fichier .txt organisé par bloc d'informations, chaque bloc débute par 000 et ce comme suit : 000... 001... 003... 004... 000... 001... 003... 004... . . .Mon but est d'ajouter une colonne en fin de chaque ligne afin... (2 Replies)
Discussion started by: zainab2006
2 Replies

6. Shell Programming and Scripting

Shell script to find files

Hi Experts, I am trying to write a shell script that should 1. Find files with name (ab030.txt,Ab030.TXT,AB030.TXT,ab030.TXT,AB030.txt) 2. If any of the above found, rename it to AB030.TXT Thanks. (4 Replies)
Discussion started by: welldone
4 Replies

7. Shell Programming and Scripting

c shell script help with find

Okie here is my problem, 1. I have a directory with a ton of files. 2. I want to first get an input on how many days ago the files were created. 3. I will take those files and put it into another file 4. Then I will take the last # from each line and subtract by 1 then diff the line from the... (1 Reply)
Discussion started by: bigboizvince
1 Replies

8. UNIX for Advanced & Expert Users

find command from a shell script

Hi experts, I have a shell script (korn shell on aix) where I am giving find command with file options which are read from a configuration file. For some reason I am getting an error find: 0652-017. I have put set -x in the shell script and the command looks okay. If I cut it and paste it in the... (6 Replies)
Discussion started by: kodermanna
6 Replies

9. Solaris

Fichier d'erreur

bonjour Ou puis je trouver les fichiers d'erreur (l'équivalent du errpt sous Aix) sous Sun Solaris ? merci (5 Replies)
Discussion started by: pascalbout
5 Replies

10. Shell Programming and Scripting

shell script to find files

I have a directory which contains files with different kinds of extensions .everyday a file with .log gets added to it .i want to extract the file with .log extension which is created with todays date . thanks in advance (2 Replies)
Discussion started by: naren_samba2005
2 Replies
Login or Register to Ask a Question

Featured Tech Videos