Unable to set Global variable outside while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to set Global variable outside while loop
# 8  
Old 05-09-2015
Quote:
Originally Posted by mohtashims
In that case let me just post my case and what i m trying to do.

I m in a directory that has a few sub directories.

I need to grep for a string in all files in all sub directories starting with the sub directory with the latest time stamp.

If grep string is found i need to break off and stop the search [this is why i was using break].

If the desired string is not found I need to grep for the string in the other sub directories.

After the process completes I should display the results of the grep if the desired search string has been found in any of the files or else Display a message that "No records Found"

I hope you can give me an idea as my expertise did not help.
This leaves a few details to be explained: you effectively search for the first match in each subdirectory, yes? Or do you want to stop the search completely after the first found match?

Supposing it is the first, here is a sketch. Because your inoput was sketchy at best the script is, too. You will have to fill in some parts. Note that the search pattern might not work if it contains too much regexp. In this case you will have to report back:

Code:
typeset -i lFound=0
typeset    fDir=""
typeset    fFile=""
typeset    chSearch="<yoursearchpattern>"

<process producing list of dirs, one per line> |\
while read fDir ; do
     lFound=0
     ls "$fDir" | while read fFile ; do
          if [ lFound -eq 0 ] ; then
               grep "$chSearch" "${fDir}/${fFile}"
               if [ $? -eq 0 ] ; then
                    lFound=1
               fi
          fi
     done
done

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 9  
Old 05-09-2015
Quote:
Originally Posted by mohtashims
In that case let me just post my case and what i m trying to do.

I m in a directory that has a few sub directories.

I need to grep for a string in all files in all sub directories starting with the sub directory with the latest time stamp.

If grep string is found i need to break off and stop the search [this is why i was using break].

If the desired string is not found I need to grep for the string in the other sub directories.

After the process completes I should display the results of the grep if the desired search string has been found in any of the files or else Display a message that "No records Found"

I hope you can give me an idea as my expertise did not help.
And the template I provided in post #4 in the thread Need Linux equivalent for UNIX did exactly that. It includes tested, working code (although it did make a lot of assumptions about things you left unspecified). Did you try starting with that template? What didn't work when you used that template?

And the command:
find * -prune -type d | sort -r -n
gives you directories in reverse alphabetic name order; not reverse order by modification time (assuming that your directory names start with "fd" as in your previous thread's example).
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 05-10-2015
Quote:
Originally Posted by Don Cragun
And the command:
find * -prune -type d | sort -r -n
gives you directories in reverse alphabetic name order; not reverse order by modification time
Don is right, i haven't even bothered to look at that. What's more, "sort -rn" sorts in reverse numerical order and for a list of directory names this is (usually) nonsense. Remove "-n" because it does nothing at best and not what you want at worst.

Sometimes, i'd like to add, it is a good idea to read the man pages. Not, because everybody keeps telling you that, but because reading these with the intention to understand them is the speed lane to not having to open such threads. With that amount of posts under your belt you should have learned at least the basics of script programming - if you ever had the intention to do so, that is.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to compare to a previous value of a variable in a while loop for a file

Hello All, I'm working on a script that will actually read a file consisting of data like the below:(ReportID,Sub_reportID,Sub_reportName) 1,1,ABC 1,2,DEF 1,3,GHI 2,1,JKL 2,2,MNO 3,1,PQR I want to read the Sub Report details for a Report_ID using while loop and write these values into... (6 Replies)
Discussion started by: venkat_reddy
6 Replies

2. Programming

Global variable in for loop (BASH)

Hello, I'm trying to read the variable "pause" from a for loop without luck. The function is dependant on the outcome of the test within the loop. If i run this, pause is always 0 within the function. Any ideas? Thanks. pause=0 users=1 (for (( ; ; )) do speed=`cat speed.log` ... (7 Replies)
Discussion started by: shadyuk
7 Replies

3. Solaris

Netra X1 LOM: Unable to change any variable via the "set" command

I'm posting here as it didn't seem quite right in the hardware section (as it's LOM commands). My apologies if I have that wrong though :) I've finally gotten round to configuring the LOM on my Netra X1, but I can't get it to change the hostname via the "set" command: lom>show hostname... (2 Replies)
Discussion started by: Smiling Dragon
2 Replies

4. Shell Programming and Scripting

Unable to set my PATH variable

Hello All, Hope you can understand my problem from the below code. $ cat ~/.profile PS1=`whoami`@`hostname`':$PWD $ ' export PATH="$PATH:.:/logarchive/utility/util:/usr/sbin:" $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:.:/usr/sbin: $ echo $SHELL /usr/bin/ksh ... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

5. Shell Programming and Scripting

use variable to set the range of a for loop

Hi; For sure there's an easy answer to this one that I am not finding.. I first set a variable, say b1a:] max=5 then I want to use max to set the range for a for loop like so (it should run for i in 1:5) b1a:] for i in {1..$max}; do echo $i; done {1..5} I would like the output... (2 Replies)
Discussion started by: jbr950
2 Replies

6. UNIX for Dummies Questions & Answers

set global rm command

Hi, My team does not have root access however many a times my members logon to the unix server and fire the rm command mistakenly on wrong folder and later realise that they have made a mess. The rm -i option prompts for a confirmation before actually deleting the files/folders. I want to... (5 Replies)
Discussion started by: shifahim
5 Replies

7. Shell Programming and Scripting

Unable to access variable outside loop

I am unable to access the value set inside the loop from outside loop . Thought of taking this to forum , I had seen other replies also , where a pipe takes the execution to another shell and mentioned thats the reason we do not get the variable outside loop . But I am getting an issue and I am... (1 Reply)
Discussion started by: Armaan_S
1 Replies

8. Shell Programming and Scripting

unable to access a variable not local to a while loop

I have a while loop like this cat ${filename} | while read fileline do ... done I need to access a variable value $newfile inside this while loop How will i do that?? (6 Replies)
Discussion started by: codeman007
6 Replies

9. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

10. Shell Programming and Scripting

global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code: function f_open_log { typeset -r... (5 Replies)
Discussion started by: robotball
5 Replies
Login or Register to Ask a Question