0

I have a shell script that checks to see if a name has been added to a text file before it runs that part of the script. If it does find that name it skips this section of the script.

Here is an example of the conflict:

Netherlands Antillies

if ! grep "netherlands-antilles" uploaded.txt; then
    // Do some work here
    echo netherlands-antilles >> uploaded.txt
fi

Netherlands

if ! grep "netherlands" uploaded.txt; then
    // Do some work here
    echo netherlands >> uploaded.txt
fi

Because Netherlands Antillies is already in the text file, it doesn't run the "Netherlands" part of the script, which I really need it to.

Is there a way to have grep not just search for part of the string? I don't want a search for netherlands to match netherlands-antilles.

  • Do you only want to match if netherlands is the entire line? Or do you also want to match if a line contains eg. netherlands # some comment? – JigglyNaga Aug 19 '16 at 15:44
  • 1
    See the -x and -F option of grep. – Stéphane Chazelas Aug 19 '16 at 15:44
  • @JigglyNaga I want to match is netherlands is the entire line. So that the netherlands grep would never match netherlands-antilles. – Nic Hubbard Aug 19 '16 at 15:47
2

If these entries are the only thing on the line of this file you could use a regular expression to include the start of the line and enf of the line:

if ! grep "^netherlands$" uploaded.txt; then
// Do some work here
echo netherlands >> uploaded.txt
fi

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.