Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

Shell: Find if two strings are present in a file when strings share a substring?

I must create a shell script that does the following:

IF "foo" is not in FILE or "boofoogoo" is not in FILE THEN:
  doSomething

  IF "foo" is not in FILE THEN:
     doFooSomething
  END

  IF "boofoogoo" is not in FILE THEN:
     doBooSomething
  END
END

Problem:

If boofoogoo is in FILE but foo is not, then foo is still matched by grep and doBooSomething is not executed.

Here is the actual code which is running:

PYPY_HOST="myprivate-pypi.com"
PYPI_URL="https://$PYPY_HOST:8000/simple"

config_file=$(get_config_file)
url_exist=$(find $config_file -type f -exec grep -l "$PYPI_URL" {} ;)
host_exist=$(find $config_file -type f -exec grep -l "$PYPY_HOST" {} ;)

if [ -z "host_exist" ] || [ -z "$url_exist" ]; then

    echo "" >> $config_file
    echo "$GLOBAL_TAG" >> $config_file

    if [ ! -z $url_exist ]; then
        echo "Extra url found"
    else            
        echo "Adding extra url"
        echo "$EXTRA_URL" >> $config_file
    fi

    if [ ! -z $host_exist ]; then
        echo "Trusted host found"
    else
        echo "Adding trusted host"
        echo "$TRUSTED_HOST" >> $config_file
    fi
else
    echo "Do nothing"
fi

Expected final output is:


[global] 
extra-index-url = https://myprivate-pypi.com:8000/simple
trusted-host = myprivate-pypi.com

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...