If - else


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If - else
# 1  
Old 08-05-2018
If - else

Hello,

Input file "temp.txt" has multiple lines. Script will parse temp.txt and search for these strings on different folders. If result is "True", don't check other folders and continue with next object. Is there any way to accomplish this?

temp.txt

Code:
abc
abd
abe
abf
abg


xyz

m1
m2
m3
m4
m5

Script:

Code:
#!/bin/bash

tmpfile="/home/admin/temp.txt"
xyz=$(echo /home/admin/m*)

function do_job() {
    "some commands with grep" > a
    if [ "$a" -ne 0 ]; then
            echo "$i" 'True';
    else
            echo "$i" 'false';
    fi
}     


for x in ${xyz};
    do
    cd "$x" ||  exit
    while read -r i; do 
    do_job "$i" &
    done < "$tmpfile"
done


Last edited by Paladin; 08-05-2018 at 12:09 PM..
# 2  
Old 08-05-2018
Welcome to the forum.



Please become accustomed to provide decent context info of your problem.

It is always helpful to carefully and detailedly phrase a request, and to support it with system info like OS and shell, related environment (variables, directory structures, options), preferred tools, adequate (representative) sample input and desired output data and the logics connecting the two including your own attempts at a solution, and, if existent, system (error) messages verbatim, to avoid ambiguities and keep people from guessing.


How do you "search for these strings on different folders"?

Where is $a defined, what be its contents?

Why do you execute do_job in background?

Do you know of the continue / break commands to leave a loop?

Moderator's Comments:
Mod Comment Added spaces between lines to make it more readable
# 3  
Old 08-05-2018
Thanks RudiC.

This is some part of a script so some lines are missing.

do_job is running parallel so I can utilize all cores.

I am running some commands with grep and redirect the output to "a".

Last edited by Paladin; 08-05-2018 at 12:22 PM..
# 4  
Old 08-05-2018
Redirecting to a file and setting a variable are two very different things. Redirecting output to a file named a does not in any way affect the current value of a variable named a and does not create a variable named a if it did not already exist.

Since we have to assume that you have shown us everything that we need to know to figure out how your script works, all we can say is that:
Code:
[ "$a" -ne 0 ]

will give you a syntax error since a variable that has not been assigned a value does not have a numeric value (unless a was set somewhere else and exported into your environment before your script was started) and the if containing the above test will always evaluate to false.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question