-1

I am writing a bash/shell script to automate some file filtering functions. Here is what I have written so far. Its based of a script I found online.

if (( $(ps -ef | grep $service | wc -l) > 0 ))
then
    echo "$service is not done" >> /root/test.txt
else
    (( $(wc -l /root/filter/first.txt) > 0 ))
    if (( $(ps -ef | grep $service | wc -l) < 0 ))
    then
        ./root/filter/filefilter.sh
    else (( $(ls | grep /root/filter/final.txt | wc -l) > 0 ))
        if (( $(ps -ef | grep awk | wc -l) > 0 ))
        then
            (( $(ls | grep /root/filter/final.txt | wc -l) > 0 ))
        else
            # here is where I would like it to loop until the filtering is complete
            # then once the final file is > 0 stop/die/done
        fi
    fi
fi

I would like it loop until the final.txt has been created in the dir, I will add that part in, just need assistance on the loop or return function ( if return can be used to return to line 1 and start running the script again until the final.txt is created then die/done/stop)

migrated from serverfault.com May 17 '15 at 20:43

This question came from our site for system and network administrators.

  • Take a look at shellcheck.net and please indent your code. – Cyrus May 17 '15 at 6:00
  • As I mentioned I am new to coding and I do not know what you mean by indent, I followed the serverfault rules and made the code show up in the code box. – LinSheller May 17 '15 at 6:06
  • I do not need the code checked as the site you posted does, I need help coding it to loop back to line 1 until the final.txt is not empty then die/done/stop so unfortunately your link does not help me. – LinSheller May 17 '15 at 6:08
  • 2
    Also, what you're looking for is while instead of if on the first line. The man page for bash should tell you how it works. – Jenny D May 17 '15 at 6:13
  • 2
    you should probably be using [ -s filename ] for most of those wc -l tests, – mikeserv May 18 '15 at 1:35
0

I don't really understand some parts of your code. 1) Does the script filefilter.sh performs the filtering? 2) If yes, in what condition will it filters (eg. $service exist) 3) Are you filtering selected services or all processes running

Indent is pretty important in scripting as you can easily spot the loops clearly

[ Your Code ]:

if (( $(ps -ef | grep $service | wc -l) > 0 ))
then
    echo "$service is not done" >> /root/test.txt
else
    (( $(wc -l /root/filter/first.txt) > 0 ))        #There is no condition required for the else-loop

    if (( $(ps -ef | grep $service | wc -l) < 0 ))
    then
            ./root/filter/filefilter.sh

    else
        (( $(ls | grep /root/filter/final.txt | wc -l) > 0 ))   #There is no condition required for the else-loop

            if (( $(ps -ef | grep awk | wc -l) > 0 ))  
            then
                    (( $(ls | grep /root/filter/final.txt | wc -l) > 0 ))
            else
                    #You can use the exit command to stop the loop base on the condition you want

        fi
    fi
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy