How do I determine whether a variable called spath="/srv/www/cyberciti.biz/https" contains a substring called "cyberciti.biz"?
You can use the portable BourneShell syntax as follows:
case "$var" in *pattern1* ) echo "do something #1";; *pattern2* ) echo "do something # 2";; * ) echo "Error...";; esac
Here is a sample code:
#!/bin/bash spath="/srv/www/cyberciti.biz/https" sync_root(){ echo "Running rsync..." rsync -ar $spath/* root@backup.cyberciti.biz:$spath } case "$spath" in *cyberciti.biz*) sync_root ;; *) echo "Error: Domain does not exits in path.";; esac
Bash Specific Syntax
The following is bash specific syntax and it will not work with BourneShell:
[[ $var = *pattern1* ]]
OR
if [[ $var = *pattern1* ]] then echo "Do something" fi
Here is a sample code:
#!/bin/bash # Wrapper for faq pdf generator # Manually generate pdf files and upload to static nixCraft 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: $0 faq-url"; exit 1; } [[ $1 != *cyberciti.biz/faq/* ]] && { printf "Error: Specify faq url (e.g., http://www.cyberciti.biz/faq/url-1-2-3/)\n"; exit 2; } ${_pdfwriter} faq "$1"
Bash v3 and above also supports additional regular expressions. See your local bash man page for more information:
man bash
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop










{ 5 comments… read them below or add one }
BASH Regular expression
[[ $var =~ '.*cyberciti.biz.*' ]] && echo Yes || echo NoBASH Regular expression
[[ '/srv/www/cyberciti.biz/https' =~ '.*cyberciti.biz.*' ]] && echo Yes || No
[[ '/srv/www/cyberciti.biz/https' =~ '.*cyberciti.biz.*' ]] && echo Yes || echo NoThank you!
if [ "${haystack}" != "${haystack/${needle}/}" ]
then
echo “found needle in haystack”
fi