Output of find command to variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output of find command to variable?
# 1  
Old 01-30-2013
Output of find command to variable?

Hi,

I'd like to assign the output of the find command to a variable.

What I need is to run the find command, and if it returns zero files, the program exits.

so i'm trying to assign the output of the find command to the $var1 variable....and then if this is less than one, I echo a message and exit.

So far I have:

Code:
find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; > $var1
                 
                 if [ "$var1" -lt 1 ]
                 then
                    echo "No files found"
                    exit     
                else
                sleep 1
                fi

i'm getting [: : integer expression expected
# 2  
Old 01-30-2013
Code:
var1=$( find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; | wc -l )
if [ $var1 -eq 0 ]
then
     echo "No files found"
     exit
else
     sleep 1
fi

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-30-2013
You showed us a script that isn't working, but you didn't really say what you wanted the script to do. The script bipinajith provided assumes that you just want to know if any files were found. I'm assuming that you have something generating log files, want to process the files found, and repeat the find after you've completed processing the files found by the 1st find until you have caught up with whatever is creating the log files. If that is what you want, try something like:
Code:
#!/bin/ksh
while [ $(find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; |
                tee find_out.$$ | wc -l) -ne 0 ]
do      # process found files
        while IFS="" read -r ls_out
        do
                printf "Processing ls output line:\n\t%s\n" "$ls_out"
                # ... ... ...
        done < find_out.$$
        sleep 1
done
rm find_out.$$
echo "No files found"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the output of a ISQL command in a variable?

I am trying to run a query which returns a sum value(a number). I want to get it in a variable so that i can refer to that variable in different places. when i am running the following command variable=`isql -Uuser -Sserver -Ppassword 1> select sum(count(*)) from xyz..abc where clm_id... (2 Replies)
Discussion started by: Sharma331
2 Replies

2. UNIX for Advanced & Expert Users

ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE. I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it. #! /bin/sh # Start the newest... (2 Replies)
Discussion started by: binary-ninja
2 Replies

3. Shell Programming and Scripting

how to save an output of a command in a variable

Hi, in shell script, i have the command swstart -p which returns an output. i want to store the output of this command into a variable. how i can do that excerpt from the script #!/usr/bin/ksh # # # # Program: swstart -p # # Description: Starts the sentinels on Slave server ... (4 Replies)
Discussion started by: lookinginfo
4 Replies

4. Shell Programming and Scripting

How to put output of one command into a variable

Hi, Let say I have these 3 files (state, list and myscript). I want to be able get the sample output like below when I run myscript. Any one know how to fix the code? TIA. ~~~~~~~~~~~~~~~ > cat /home/state CA > cat /home/list CA 100 50 20 AUS 120 61 10 > cat myscript... (6 Replies)
Discussion started by: joker_789us
6 Replies

5. Programming

Command output into a variable

Hi, with this command: cu -l /dev/ttyACM0 -s 9600 > name.txt I put the output of the port in a txt Is posible to do the same (or similar) in a var directly, inside a C program? cu -l /dev/ttyACM0 -s 9600 > variable ? I have trying this withs pipes, but i dont know how to... (6 Replies)
Discussion started by: daaran
6 Replies

6. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

7. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

8. Shell Programming and Scripting

get characters from output of a command in a variable

Hi, i have two questions, I am new to programming 1. I have an output of a command and i want to get some specific part of it in a variable. i am trying sr=`some comand xyz| grep 'Last Changed Rev:' | cut -c19-` now variable sr gets a end of line character at end. output of the command... (3 Replies)
Discussion started by: muaz
3 Replies

9. UNIX for Dummies Questions & Answers

ls command output to variable in script

Hi, I wrote a script to get the oldest file from a directory path (which is passed as a parameter to the script) ######################################################### XMLFILE_PATH={$1} cd $XMLFILE_PATH JPM_FILENAME = `(ls -tr User* | head -1)` #echo $JPM_FILENAME ###### END... (1 Reply)
Discussion started by: dsrookie
1 Replies

10. Shell Programming and Scripting

Command output to a variable.

With cut -c 8-13 myfile, I am getting some numeric value. In my shell script I am trying to assign something like this, var=cut -c 8-13 myfile But at the time of execution I am getting -c is not found. If I dont assign, then script executes well. Can we not simply use the value from one... (8 Replies)
Discussion started by: videsh77
8 Replies
Login or Register to Ask a Question