Looping Logic, Need to implement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Looping Logic, Need to implement
# 1  
Old 08-29-2013
Looping Logic, Need to implement

I need to implement a looping logic.

Code:
If [ Test = 1] then
Go to /path1/file*

Get all the filename starting with file* and store it in a array
count file number and store it in variable like run
Ex: I found 3 file with starting file* so my run = 3
means my loop should run three time
May be like this

Code:
While run =3

for first filename, it search 'a' is there in the file if yes
echo ' a is exist'
else
echo 'a is not exist
run ++

so next time it again run and search 'a' in the second file to
echo ' a is exist'
else
echo 'a is not exist
run ++


and same again it search 'a' in the third file.


Kindly help
# 2  
Old 08-29-2013
There should be no need to populate an array, or use a counting variable (run).

The for loop will populate a variable (file in this case) with each filename and loop for you.

Here I use grep with -q (quiet) option to test for string a and set the exit status, this is used by the if statement:

Code:
for file in /path1/file*
do
   if grep -q a $file
   then
        echo "a is exist in $file"
   else
        echo "a is not exist in $file"
   fi
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again: for (j = 0; data != 0; j++){ switch(data){ case 'c': output = ranit(r_brace_array); break; case 'h': output = ranit(pipe_array); break; ... (6 Replies)
Discussion started by: Azrael
6 Replies

2. Shell Programming and Scripting

How to implement "not in" logic in UNIX script?

Hi Gurus, I need use "not in " logic in my script, like below if $name not in exception_file_list, then do something. anyone can help out this? Thanks in advance. (5 Replies)
Discussion started by: ken6503
5 Replies

3. Shell Programming and Scripting

Need logic to implement file compare.

Hi all I have a requirement where i have to develop a file compare tool. Requirement: I have one expected file and one actual file. Both the files are database dumps in a csv file comma seperated. The first line of the file has the column names. Now there are 3 scenarios: 1) Firstly we have... (0 Replies)
Discussion started by: Ganesh_more
0 Replies

4. Shell Programming and Scripting

Logic needed to recursive looping in the script

Hello i have a requirement where in a file i will get string. The length could be from 1 to 20. if the string is less than 6 characters ( ex: ABCD) . i need to append 'X' on right hand side to make it 6 characters (ex: ABCDXX). if suppose i get the same string from the file as ABCDXX then i... (5 Replies)
Discussion started by: dsdev_123
5 Replies
Login or Register to Ask a Question