Find command doesn't return in shell script.


 
Thread Tools Search this Thread
Operating Systems HP-UX Find command doesn't return in shell script.
# 1  
Old 04-01-2015
Find command doesn't return in shell script.

Hi All,

I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file.

However, this doesn't work as I believe the find command doesn't exit's inside the shell script. The output file to which the find command redirects remains empty, even though the find command when run on the shell, returns rows.
Code:
for j in `cat searchstring.out`;
do

find / -name *$j* > $HOME/match.out

for k in match.out ;

do
owner=$(ls -l $k | awk '{print $3}')

echo "File found is:- $k"
echo "Owner is:- $owner"

done
done

Can someone advise how this can be made to work? How does the find command store all output to a file?

Thanks in advance.

Last edited by vbe; 04-01-2015 at 01:34 PM.. Reason: Code tags next time please!
# 2  
Old 04-01-2015
The * must be evaluated by the find command, you must escape it from the shell.
Code:
find / -name \*$j\*

where the $j is still evaluated by the shell. If this is not wanted, use a " " escape.
Code:
find / -name "*$j*"

# 3  
Old 04-01-2015
Thanks, tried this. It doesn't help in writing the output from find to write to an output file however.
# 4  
Old 04-01-2015
A second error is
Code:
for k in match.out ;

You certainly want
Code:
for k in `cat match.out`

like you did in the outer loop.
But this is not ideal for many reasons. A more efficient and less risky version:
Code:
while read j
do
  find / -name "*$j*" |
  while read k
  do
    owner=$(ls -ld "$k" | awk '{print $3}')
    echo "File found is:- $k"
    echo "Owner is:- $owner"
  done
done < searchstring.out

(NB HP-UX find does not have -ls)
# 5  
Old 04-01-2015
Thanks.

This looks much cleaner now. But when I try this, the search string is read from the output file correctly but seems the output from find is not piped to the second while loop. So, neither do I get the file found, nor the owner displayed on console.

When I run the find manually on shell (using same search string), it indeed returns the results.

Any idea what could be more wrong here?

Please confirm.

Regards.

Last edited by Vipin Batra; 04-01-2015 at 08:47 AM..
# 6  
Old 04-01-2015
No, j is a variable.
The whole outer loop is fed from searchstring.out, see the redirection at the end!

The inner loop is fed from the find command. The | conects the find command with the loop.
# 7  
Old 04-01-2015
You're missing a \ after the |.

Another question, do you really want to use find to search throgough /?
Wouldnt locate be a bit faster?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. SuSE

Find command doesn't pipe the output as required.

Hi, I am using below code snippet to echo/display the files found (matching a pattern from searchstring.out file) and the corresponding owner. while read j do echo "Pattern to search is:- $j" find / -name "*$j*" |\ while read k do echo "File found is:- $k" owner=$(ls... (9 Replies)
Discussion started by: Vipin Batra
9 Replies

2. Shell Programming and Scripting

How to exit a shell script if a unix command does not return any value for 10 seconds?

Hi, Can anyone help me how to exit a shell script if a unix command inside does not return any value for 10 seconds? The scenarios is like this. I want to login to a application using shell script where the connection string is mentioned.but suppose this connection string is not... (10 Replies)
Discussion started by: arijitsaha
10 Replies

3. Shell Programming and Scripting

find command in shell script

Hi all, Please i need an explanation for the following statements ref_file=/tmp/cleanfiles export ref_file touch `TZ=WAT+2 date "+%Y%m%d%H%M"` $ref_file find . ! -name . -prune -type f ! -newer $ref_file -exec store_file.sh {} \; (1 Reply)
Discussion started by: anish_1982
1 Replies

4. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

5. Shell Programming and Scripting

Find command in Shell Script

hi I am a newbee in Shell scripting (hardly 7 days) I have to execute a shell script which looks like this #!/bin/sh var1=`date +"%Y%m%d"` echo $var1 find . -name "$var1*" -exec mv {} Delete/ \; the find command in the script is running independently but when kept in this script it is... (24 Replies)
Discussion started by: sweetnsourabh
24 Replies

6. Shell Programming and Scripting

Backing out of a script if command doesn't return any results?

Hello I have a script which emails identifies the user ID of a user and sends them an email. A user can enter part of the name of the person he/wants to send the email to. Then I use the ypcat command to identify the UID of that person. The problem I'm having, is building in an error trap... (1 Reply)
Discussion started by: Glyn_Mo
1 Replies

7. Solaris

df command on Sol8 machine doesn't return a result

I have a sun4u system running Solaris 8. I tried running the df command but it returns a blank result. Also I'm unable to collect an explorer from this system as the OS complains that the disk is full. What could be going on here? (10 Replies)
Discussion started by: dperry1973
10 Replies

8. Shell Programming and Scripting

Find command return type

Hi all does find command return anything if the file to be searched is not found? Like if I search from a file in a dir does it return false or null if the file is not found? Please suggests. (3 Replies)
Discussion started by: Veenak15
3 Replies

9. Shell Programming and Scripting

script to find whether shell command is available

I am running shell scripts on windows using Cygwin tool. In my shell scripts, i want to add an error check, that verify whether a certain command is available or not. For example if SED comamnd is not available in Cygwin, then it should exit with error message. How do i write such shell... (2 Replies)
Discussion started by: mmunir
2 Replies

10. UNIX for Dummies Questions & Answers

Shell script 'find' command

I have a script that has the following command: find /home/user -name test.dat The script works as desired when running normally. However, when I run the script preceding it with 'sh', it fails. Is there something I need to account for when preceding the execution of the script with 'sh'? (1 Reply)
Discussion started by: bsavitch
1 Replies
Login or Register to Ask a Question