Regex and if in shell script -
my programs starts services , store output in tmp variable , want match variable's content if starts fatal keyword or not? , if contains print port in use using echo command
for example if tmp contains fatal: exception in startup, exiting.
i can sed: echo $tmp | sed 's/^fatal.*/"port in use"/'
but want use builtin if match pattern. how can use shell built in features match regex?
posix shell doesn't have regular expression operator unix ere or pcre. have case keyword:
case "$tmp" in fatal*) dosomethingdrastic;; *) dosomethingnormal;; esac you didn't tag question bash, if have shell can other kinds of pattern matching or ere:
if [[ "$tmp" = fatal* ]]; … fi or
if [[ $tmp =~ ^fatal ]]; … fi
Comments
Post a Comment