IT이야기

문자열에 다른 문자열이 포함되어 있지 않은지 확인하는 Bash

cyworld 2021. 4. 26. 21:09
반응형

문자열에 다른 문자열이 포함되어 있지 않은지 확인하는 Bash


.sh 스크립트에 문자열 ${testmystring}있고이 문자열 다른 문자열이 포함되어 있지 않은지 확인하고 싶습니다.

    if [[ ${testmystring} doesNotContain *"c0"* ]];then
        # testmystring does not contain c0
    fi 

어떻게 할 수 있습니까? 즉, doesNotContain은 무엇입니까?


사용 !=.

if [[ ${testmystring} != *"c0"* ]];then
    # testmystring does not contain c0
fi

자세한 내용은를 참조하십시오 help [[.


메인 프레임 러가 말했듯이 grep을 사용할 수 있지만 테스트를 위해 종료 상태를 사용하려면 다음을 시도하십시오.

#!/bin/bash
# Test if anotherstring is contained in teststring
teststring="put you string here"
anotherstring="string"

echo ${teststring} | grep --quiet "${anotherstring}"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found

if [ $? = 1 ]
then
  echo "$anotherstring was not found"
fi

참조 URL : https://stackoverflow.com/questions/30557508/bash-checking-if-string-does-not-contain-other-string

반응형