Friedrich Dumont wrote:
> Dear Friends:
>
> I am new to bash programming. I been trying to write
> the following two scripts for 3 days now, and I still
> can't get them to worlk the way I want to. Where did I
> go wrong? What's missing or what should not have been
> in the script? I will be grateful to anyone who would
> help me to fix these most needed scripts?
>
> ---
>
> The first script, is in this format showExecFiles
> "string". Where showExecFiles is the name of the
> script and "string" a parameter. The script must
> output (on the screen) all the EXECUTABLE files of the
> path directories if they contain the agument string. I
> also want it to clearly indicate the directory and
> sub-directories being searched.
>
> For example: 'showExecFiles mail' to output:
>
> /usr/local/bin :
> ---------------
> /usr/local/bin/formail
> /usr/local/bin/mailstat
> /usr/local/bin/metamail
> /usr/local/bin/procmail
>
> /usr/bin :
> ---------
> /usr/bin/Rnmail
> /usr/bin/cmail
> ...
> etc.
>
> Here is my first script:
> #!/bin/bash
> for pathvar in `echo $PATH | sed "s/:/ /g"`;do
> echo $pathvar:
> find $pathvar -type f -name "*$1*" -maxdepth 1
> done
>
> This script should be fairly easy to write. However, I
> don't know how to check if the file is executable and
> also how to acces sub-directories and their files.
>
> ---
Hello !
Well It seems that this list is not very active now and further more
you'd be more advised to write to comp.unix shell, but anyway...
I just look to your first script but it can help for the second one i
suppose...
When in bash you want to "break" your PATH do this that way :
IFS=:
for pathvar in $PATH; do....
where "IFS" stands for "Internal Field Separator".
It appears that you now the "file" command so...What don't you use it
to search for executable files ? This command outputs "ASCII", "DATA" or
"EXECUTABLE" so grep can do the job.
The only problem is that the file command can read only one arg at a
time. So we must use the "xargs" command to feed "file" with one
argument at a time. All this mixed up give something like :
1 #!/bin/bash
2 IFS=:
3 for pathvar in $PATH; do
4 echo "$pathvar" :
5 echo "-------"
6 find $pathvar -type f -name "*$1*" | xargs file | grep
"executable"| awk '{print $1}'
7 done
For the sub-directories prb maybe you can increase the value of
maxdepth.
This script is not perfect...For exemple, when file is fed with a
directory without "file$1" it outputs its "usage message".
Anyway, i hope this helps...
Regards,
PG
This one for criticism purpose
Regards
--
Pascal Gosse
"2+2 = 5...pour d'assez grandes valeurs de 2."