linux - unix: how to tell if a string matches a regex -
trying out fish shell, i'm translating bash functions. problem in 1 case, i'm using bash regexes check if string matches regex. can't figure out how translate fish.
here example.
if [[ "$arg" =~ ^[0-9]+$ ]] ...
- i looked sed, don't see way set exit status based on whether regex matches.
- i looked delegating ruby, again, getting exit status set based on match requires making ugly (see below).
- i looked delegating bash, despite trying maybe 3 or 4 ways, never got match.
so, there way in *nix check if string matches regex, can drop conditional?
here have works, unhappy with:
# kill jobs job number, or range of job numbers # example: k 1 2 5 # example: k 1..5 # example: k 1..5 7 10..15 # example: k 1-5 7 10-15 function k arg in $argv if ruby -e "exit ('$arg' =~ /^[0-9]+\$/ ? 0 : 1)" kill -9 %$arg else set _start (echo "$arg" | sed 's/[^0-9].*$//') set _end (echo "$arg" | sed 's/^[0-9]*[^0-9]*//') n in (seq $_start $_end) kill -9 %"$n" end end end end
the standard way use grep
:
if echo "$arg" | grep -q -e '^[0-9]+$' kill -9 %$arg
Comments
Post a Comment