How do I write and match regex in / bin / sh script?
I'm writing a shell script for a limited unix-based microkernel that doesn't have bash! For some reason / bin / sh cannot run the following lines.
if [[ `uname` =~ (QNX|qnx) ]]; then
read -p "what is the dev prefix to use? " dev_prefix
if [[ $dev_prefix =~ ^[a-z0-9_-]+@[a-z0-9_-"."]+:.*$ ]]; then
For 1st and 3rd lines, it complains about a missing expression statement, and for 2nd line, it doesn't talk about coprocess! Can anyone shed some light on the differences between the / bin / bash and / bin / sh scripts?
+3
source to share
2 answers
Here's a way to see what non-Posix functions are in the script:
Copy / Paste this to shellcheck.net:
#!/bin/sh
if [[ `1uname` =~ (QNX|qnx) ]]; then
read -p "what is the dev prefix to use? " dev_prefix
if [[ $dev_prefix =~ ^[a-z0-9_-]+@[a-z0-9_-"."]+:.*$ ]]; then
: nothing
fi
fi
Or install shellcheck locally and run shellcheck ./check.sh
it and it will highlight non-list functions:
In ./check.sh line 2:
if [[ `1uname` =~ (QNX|qnx) ]]; then
^-- SC2039: In POSIX sh, [[ ]] is not supported.
^-- SC2006: Use $(..) instead of deprecated `..`
In ./check.sh line 4:
if [[ $dev_prefix =~ ^[a-z0-9_-]+@[a-z0-9_-"."]+:.*$ ]]; then
^-- SC2039: In POSIX sh, [[ ]] is not supported.
You need to either rewrite the expressions as globes (not realistic) or use external commands (grep / awk) explained by @anubhava
+2
source to share