Bash查找变量是否包含子字符串
时间:2020-01-09 10:37:21 来源:igfitidea点击:
如何确定变量spath =/srv/www/theitroad.local/https是否包含名为theitroad.local的子字符串?
如何在Bash脚本中检查字符串是否包含子字符串?
您始终可以在Linux或Unix Shell脚本中找出一个字符串/单词/变量是否包含另一个字符串/单词。
例如,在$var="hello bash"
中找出是否包含hello单词
有很多方法可以测试字符串是否是bash中的子字符串。
我们可以使用awk,perl,bash和其他方法在shell脚本中查找变量是否包含子字符串。
找出bash变量是否包含子字符串
让我们看一下各种bash方法,以检查字符串是否包含子字符串。
使用case语法
您可以使用可移植的BourneShell语法,如下所示:
case "$var" in *pattern1* ) echo "do something #1";; *pattern2* ) echo "do something # 2";; * ) echo "Error...";; esac
这是一个示例代码:
#!/bin/bash spath="/srv/www/theitroad.local/https" sync_root(){ echo "Running rsync..." rsync -ar $spath/* [email protected]:$spath } case "$spath" in *theitroad.local*) sync_root ;; *) echo "Error: Domain does not exits in path.";; esac
Bash检查字符串是否包含子字符串
[
和[[
计算条件表达式。
这是测试命令/内置命令的同义词。
但是,[[
是对[
命令的改进。
请注意,以下是bash特定的语法,不适用于BourneShell:
[[ $var = *pattern1* ]] ## OR ## if [[ $var = *pattern1* ]] then echo "Do something" fi
这是一个示例代码:
#!/bin/bash # Wrapper for faq pdf generator # Manually generate pdf files and upload to static theitroad download server # - # Get all defaults and functions [[ -f ~/backend/utils/functions.sh ]] && ~/backend/utils/functions.sh _pdfwriter=${_PYTHON_PDF_WRITER:-~/backend/utils/pdfwriter.beta} [[ $# -eq 0 ]] && { echo "Usage:[[ $var =~ .*substring.* ]] [[ $value =~ .*container1.* ]] && do_somethingfaq-url"; exit 1; } [[ != *theitroad.local/faq/* ]] && { printf "Error: Specify faq url (e.g., http://www.theitroad.local/faq/url-1-2-3/)\n"; exit 2; } ${_pdfwriter} faq ""
使用bash regex语法
Bash v3及更高版本还支持其他正则表达式。
语法如下,以查看bash变量是否包含子字符串:
#!/bin/bash url="https://www.theitroad.local/faq/bash-for-loop/" [[ $url =~ .*faq* ]] && echo "Found" || echo "Not found" ## if syntax ## if [[ $url =~ .*faq.* ]] then echo "I found a word faq in ${url}." else echo "Sorry. Not found." fi
例如,找出$url中是否存在单词faq:
# define a var at shell var="This is a test" # use awk awk 'var="Hello world" grep -q 'foo' <<< $var && echo "A substring called foo found" || echo "Not a substring" grep -q 'world' <<< $var && echo "A substring called world found" || echo "Not a substring"~/test/{print "A substring called test found"}' <<< $var
使用AWK正则表达式语法
语法非常简单:
#!/bin/bash my_quote='Look for opportunities in every change in your life' search_word='opportunities' if grep -q "${search_word}" <<< "$my_quote" then echo "I found '${search_word}' word in $my_quote variable." else echo "Sorry I cannot find '${search_word}' word in $my_quote variable." fi
grep命令语法
在此示例中,我将使用grep检查Bash脚本中的字符串是否包含子字符串:
#!/bin/bash # Purpose - Basic shell script to backup required LXD containers under Linux # Author - Hyman Gite {https://www.theitroad.local} under GPL version 2.0+ # ------------------------------------------------------------------------- ## Get today's date ## NOW=$(date +'%m-%d-%Y') # # NFS mounted backup or AWS efs # DEST="/backups/lxd/${NOW}" # # Define config and other paths # LXD_CONF="${DEST}/$HOSTNAME-lxd.config.txt" LXD_INST_LIST="${DEST}/$HOSTNAME-lxd.instances.list.txt" LXD_VER="${DEST}/$HOSTNAME-lxd.version.txt" # # Containers to ignore from backup # ignore="nginx-build-service docker-test-service mysqld-test-data php8-build-service www-testing" [ ! -d "$DEST" ] && mkdir -p "$DEST" ## Dump LXD server config ## lxd init --dump > "${LXD_CONF}" ## Dump all instances list ## lxc list > "${LXD_INST_LIST}" ## Make sure we know LXD version too ## snap list lxd > "${LXD_VER}" ## Backup all Instances for i in $(lxc list -c n --format csv) do file="$DEST/${i}-backup.tar.xz" # Ignore backup for this container if defined in $ignore grep -q $i <<< $ignore && continue lxc export "${i}" "$file" --optimized-storage --compression xz done
让我们尝试一下我们的小型Shell脚本中的if命令:
##代码##例如,这是我的LXD备份脚本,该脚本忽略$ignore中定义的备份中的容器:
##代码##