Help script shell find fichier


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help script shell find fichier
# 8  
Old 03-14-2015
Quote:
Originally Posted by georg2014
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

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:
Code:
    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:
Code:
    if [ "$parent_dir" = "$dir" ]
    then
        printf 'File "%s" not found.\n' "$file"
        cat /data/test/recept/EON/default/default."${file##*.}"
        exit 0
    fi

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