Find not working right


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find not working right
# 1  
Old 11-21-2013
Find not working right

Code:
if [[ "$1" == "" ]]
 then

leftarray=($(find . -type l -printf "%p\n" 2>/dev/null))
rightarray=($(find . -type l -printf "%l\n" 2>/dev/null))

for var in "${rightarray[@]}"
do
   maximumarray[$index]=`echo "$var" | tr -dc "/" | wc -c | tr -d " "`
   index=$(($index+1))
done
#############
for numbers in "${maximumarray[@]}"
do
   if [[ $numbers > $MAX  ]]
   then
   MAX=$numbers
   fi
done
#############
for var in "${rightarray[@]}"
do

   component=`echo "$var" |  tr -dc "/" | wc -c | tr -d " "`
        if [[ $component -eq $MAX  ]]
        then
        echo "Output: '${leftarray[$temp]} -> ${rightarray[$temp]}'"
        fi
   temp=$(($temp + 1))
done
fi

Here's the problem When running script without arguments or switches it should search through all directories and subdirectories of directory where you are. It does not work and I do not know why and it drives me crazy.

This script should find the symbolic link with longest number of "/" in -printf "%l\n" and output it. It should work when there are more links with same number of "/"

this script have also switch -d <number> and it has the same code as above but in find i use also -maxdepth and it works fine so I do not know why this does not work.

Any help ?
# 2  
Old 11-21-2013
You don't say what happens when it doesn't work.
You don't say what shell you're using and you don't say what OS you're using.

So, as a VERY wild guess, try changing:
Code:
#############
for numbers in "${maximumarray[@]}"
do
   if [[ $numbers > $MAX  ]]

to:
Code:
#############
MAX=0
for numbers in "${maximumarray[@]}"
do
   if [[ $numbers -gt $MAX ]]

I don't see why -depth would change the results, but using > instead of -gt performs a string comparison instead of a numeric comparison. So, for example 2 > 12 is true while 2 -gt 12 is false.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-21-2013
Quote:
Originally Posted by Don Cragun
You don't say what happens when it doesn't work.
You don't say what shell you're using and you don't say what OS you're using.

So, as a VERY wild guess, try changing:
Code:
#############
for numbers in "${maximumarray[@]}"
do
   if [[ $numbers > $MAX  ]]

to:
Code:
#############
MAX=0
for numbers in "${maximumarray[@]}"
do
   if [[ $numbers -gt $MAX ]]

I don't see why -depth would change the results, but using > instead of -gt performs a string comparison instead of a numeric comparison. So, for example 2 > 12 is true while 2 -gt 12 is false.

i changed > with -gt and it works thank you
i am using bash2 and running on unix free bsd server
and also max=o is at the beginning of the script
this is only a part of script

but i have another problem

my script must pass through tester

all test are okay except when running script like this
./skript.csh dirctory directory directory

it should search all these three directories and find the longest links it has the same code as above only first if have test -d "$1" if the first argument is directory and in find is "$@" insted of "." and there are two test in these test the tester run script with more directories and first test is ok but second not and i dont know why any idea where can be problem. also there are directories and links with special characters in name or spaces but script work with them good
# 4  
Old 11-21-2013
Quote:
Originally Posted by xpukm
i changed > with -gt and it works thank you
i am using bash2 and running on unix free bsd server
and also max=o is at the beginning of the script
this is only a part of script

but i have another problem

my script must pass through tester

all test are okay except when running script like this
./skript.csh dirctory directory directory

it should search all these three directories and find the longest links it has the same code as above only first if have test -d "$1" if the first argument is directory and in find is "$@" insted of "." and there are two test in these test the tester run script with more directories and first test is ok but second not and i dont know why any idea where can be problem. also there are directories and links with special characters in name or spaces but script work with them good
You have shown us what your code looks like when your script is invoked with no arguments (or with the 1st argument set to an empty string).

You have implied what your code looks like with the 1st argument is "-d".

Show us what code you're using when there are 3 arguments given that name directories.

And, PLEASE, never name a bash script with the filename extension ".csh". The .csh filename extension, by convention, should only be used to name C shell scripts. The Bourne shell (and extensions of the Bourne shell like bash and ksh) are very different from the C shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find with name not working from crontab

find without -name works fine. find with -name '*' works from interactive bash, but not from cron. mgr@someplace:~$ crontab -l | grep wsl 17 09 * * * /usr/bin/find /wsbj/logs/mgr/webServiceLogs -type f -mtime +30 > /home/mgr/wsl_find.out 17 09 * * * /usr/bin/find /wsbj/logs/mgr/webServiceLogs... (11 Replies)
Discussion started by: CarloM
11 Replies

2. Shell Programming and Scripting

Working with FIND command

Hi , In /home/etc/files path ran the following command find . -name 'ABC*' | wc -l The output of the above command is 25 as expected In path /home path ran the following command find . -name '/home/etc/files/ABC*' | wc -l The output of the abvoe command is 0 . Why the above... (3 Replies)
Discussion started by: smile689
3 Replies

3. UNIX for Dummies Questions & Answers

Find with Prune not working

Hi I am trying to list all files in every subdirectory from a given location. However, I realise that 1 folder will have files that I am not interested in. This is using a .csh file to execute I have tried different scripts but to no avail. My current incarnation is below. Would someone be... (4 Replies)
Discussion started by: wonderbison
4 Replies

4. UNIX for Advanced & Expert Users

Find and delete not working

Hi, I have a .ksh script which finds all the directories older than 84 days and tries to housekeep. Below is the command used find * -depth -type d -ctime +84 -exec rm -rf {} \; The above command lists all the directories ie child and parent directory in descending order which are more... (0 Replies)
Discussion started by: annamalai77
0 Replies

5. Shell Programming and Scripting

find command not working

Hi all, find command not working for me in a perticular directory.The same command is working fine in any other directory. Following is the command i issued: find . -type f -print my question is , is it possbile to disable a command only for a perticular directory ??...of course... (4 Replies)
Discussion started by: panyam
4 Replies

6. Shell Programming and Scripting

find | gawk --- NOT WORKING

Hi every1:) I have a problem with find and Pipe to gawk. Find -name '*.txt' -> works fine.. Gives all the txt file under home directory and sub directories But when I use it like this (see below). Find -name '*.txt' | gawk -F '\t' "script" :confused: It doesn't work.... (22 Replies)
Discussion started by: Needhelp2
22 Replies

7. Shell Programming and Scripting

how to find in which shell i am working..

Hi Friends, I am new for this site.i have a small query.plz let me know the command to find in which shell presently i am working. (2 Replies)
Discussion started by: pineapple
2 Replies

8. Shell Programming and Scripting

find/if statement not working

Hi guys: I am trying to delete multiple files in a folder with different names. Below is the script that I was trying, but it doesn't work ************************** #!/bin/ksh DATE=`date '+20%y%m%d'` DEL_DIR=<dir where files have to be deleted> let DATE2=$(($DATE - 2)) let DATE1=$(($DATE... (12 Replies)
Discussion started by: geomonap
12 Replies

9. UNIX for Advanced & Expert Users

find command not working

Hi, Having issues with the . parameter in the find command. Issuing "find . -name \*.pl" gives me find: cannot open .: No such device I got it working by substituting . with *, so "find * -name \*.pl" gives the correct listing. bin/test.pl "which find" lists /bin/find. Anybody... (7 Replies)
Discussion started by: jabrady
7 Replies

10. Shell Programming and Scripting

How to find our working compiler

Hi, I am using Turbo Linux and needs to install Samba on it. But it said that could not find gcc. My question is how to find out which compiler is working on my system. I tried echo $CC but it gave me nothing. How can I set environmental compiler to c. Thanks in advance. (8 Replies)
Discussion started by: s_aamir
8 Replies
Login or Register to Ask a Question