Regex Validation Kornshell - regex

I have the following REGEX expression
^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$
In an attempt to cover all eventuality of mobile number in the UK. While parsing this validation through a REGEX tester online, which works great I am having difficulty getting it to work correctly in cornshell
fn_validate_msisdn() {
MSISDN=$1
REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"
if [ `echo $MSISDN | egrep -c $REGEX_PTN` -gt 0 ]
then
return 1
fi
return 0;
}
Being called by:
if [ ! `fn_validate_msisdn ${MSISDN}` ]
then
...
fi
However It always seems to fail, either with illegal syntax or always returning greater than one.
some test data:
447999999999 : OK
07999999999 : OK
4407948777622 : FAIL
43743874874387439843 : FAIL
Any Suggestions would be great

Your function can be just this:
fn_validate_msisdn() {
MSISDN=$1
REGEX_PTN="^(\(?\+?(44|0{1}|0{2}4{2})[1-9]{1}[0-9]{9}\)?)?$"
echo "$MSISDN" | egrep -q "$REGEX_PTN";
}
then:
fn_validate_msisdn 43743874874387439843
echo $?
1
fn_validate_msisdn 447999999999
echo $?
0
Remember return status of 0 means success and 1 means failure here.

Related

If statement in build using ymal aws

Hey I'm trying to do an if statement in Yaml, something like
if $NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws )
what's the correct syntax?
is it even possible?
Hey I'm trying to do an if statement in Yaml,
something like this:
if
$NUMBER_OF_SOURCES == 3 then echo 1
(echo 1 will change I the future to a script that does something in aws ),
what's the correct syntax? is it even possible?
tried to do something like that but i get
- if [ $NUMBER_OF_SOURCES -eq 3 ]
- then
- echo "true"
- fi
[Container] 2022/04/24 10:35:36 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: if [ $NUMBER_OF_SOURCES -eq 3 ]. Reason: exit status 2
You do this using pipe (|) notation in yaml:
- |
if [ $NUMBER_OF_SOURCES -eq 3 ]
then
echo "true"
fi
or just put everything in one line:
- if [ $NUMBER_OF_SOURCES -eq 3 ]; then echo "true"; fi

Creating an alert function in Bash

I wanted to create a function in bash similar to a default alias I got in Ubuntu, looking like:
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
This creates a simple notification after a command has been issued with it.
For example, using
history | grep vim; sleep 5; alert
gives a notification after the sleep is done, simply saying
history | grep vim; sleep 5;
I would like to write the alert into a bash function instead, which have given some trouble with the regex.
I have tried:
function alert2 () {
ICON=$([ $? = 0 ] && echo terminal || echo error)
MSG=$(history | tail -n1 | sed -e s/^\s*[0-9]\+\s*//\;s/[\;\&\|]\s*alert$//)
notify-send --urgency=low -i $ICON $MSG
}
which would output both the linenumber in history when called itself, and give an Invalid number of options when called such as the first example.
Is this possible, and if so, how? Is it simply my regex that is faulty?
I'm running on WSL, so don't have notify-send installed:
function alert2 () {
ICON=$([ $? = 0 ] && echo terminal || echo error);
MSG=$(history | tail -n1| sed -e 's/^\s*[0-9]\+\s*//;s/[;&|]\s*alert2$//');
echo $ICON $MSG;
}
jadams#Temp046317:~/code/data-extract$ cat /etc/apt/sources.list > /dev/null ; alert2
terminal cat /etc/apt/sources.list > /dev/null
I'm hoping that this would work for you (instead of the echo):
notify-send --urgency=low -i "$ICON $MSG"

Why [ ... ] syntax doesn't work in a Makefile while `test` does?

I am on Linux Mint 19. I am entirely new to Makefiles.
Here is the problematic part:
[ $(shell id --user) -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
which throws this error:
[ 1000 -eq 0 ] && ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Makefile:25: recipe for target 'distrib' failed
make: *** [distrib] Error 1
On the contrary, using test command directly proves to be working entirely:
if test $(shell id --user) -eq 0; then ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 ) fi
I want to ask why that is, did I break some Makefile rule?
This doesn't have anything to do with makefiles, it has to do with shell scripting and the difference between using && vs. if in terms of the exit code. You are comparing apples and oranges here.
It's not related to test vs [. If you write the version using [ inside an if statement you'll get the same behavior as you do with test, and if you write the test version with the && model you'll get the same behavior as you do with [.
Run this in your shell:
[ 1000 -eq 0 ] && echo hi
echo $?
Now run this in your shell:
if [ 1000 -eq 0 ]; then echo hi; fi
echo $?
You'll see the former gives a non-0 exit code, while the latter gives a 0 (success) exit code. That's how if works; it "swallows" the exit code of the condition.
Make always looks at the exit code of the shell script to decide if it failed or not.
Generally in make scripting you want to re-arrange your expressions to use || rather than &&. That ensures that if the script exits early it exits with a success code not a failure code. You can write your script like this:
[ $$(id -u) -ne 0 ] || ( echo && echo "distrib target has to be run as normal user" && echo && exit 1 )
Note I use $$(id -u) not $(shell id --user); the recipe is run in the shell already and it's an anti-pattern to use the make shell function in a recipe. Also, the -u option is a POSIX standard option while --user is only available in the GNU utilities version of id.

How to separate package name by regex in bash?

I'm writing a script function to separate package tar ball name listing into package name version.
xorg-fonts-misc-1.0b-1
Xorg-font-bitstream-75dpi-1.0.0-2.i386
Xorg-font-bitstream-100dpi-1.2a-2.arm
Other-Third-Party-1.2.2-1-any
I'm using the following script to separate name and version.
split_pkgname_pipe() { # split x-x-1.3-1.x -> x-x 1.3-1.x
[ $opt_v != 0 ] && echo "dbg:split_pkgname_pipe $*" >&2
awk '{
f=$0
sub(/\-[0-9].*$/,"")
n=$1
v=substr(f, length(n)+2)
print n, v
}'
}
The problem of my code will cause Xorg-font-bitstream-75dpi-1.0.0 separate as Xorg-font-bitstream and 75dpi-1.0.0. But I want Xorg-font-bitstream-75dpi and -1.0.0
[SOLVED]
split_pkgname_pipe() { # split x-x-1.3-1.x -> x-x 1.3-1.x
[ $opt_v != 0 ] && echo "dbg:split_pkgname_pipe $*" >&2
local line namever name ver rel
while read line ; do
namever="${line%-*}"
rel="${line##*-}"
if [ `expr match $rel '[0-9]'` = 0 ] ; then # rel is 'i386/any'...
name="${namever%-*}"
ver="${namever##*-}"
namever="$name"
rel="$ver-$rel"
fi
name="${namever%-*}"
ver="${namever##*-}"
echo "$name $ver-$rel"
done
}
$ package="Xorg-font-bitstream-75dpi-1.0.0"
$ echo "${package%-*}"
Xorg-font-bitstream-75dpi
$ echo "${package##*-}"
1.0.0
Try this
sed -re '/^(.*?)((\d[a-z]?\.)+.*)$/\1\t\2/gmi' file.txt

how to detect when compiler emits an error

To compile a C++ project, I want to write a perl script to compile my program and see if the compilation went wrong or not. If the compiler gives any compilation error, I'll need to perform some other task.
The perl script will be something like this:
#l1 = `find . -name '*.c'`;
#l2 = `find . -name '*.cpp'`;
#l3 = `find . -name '*.cc'`;
my $err;
my $FLAGS = "-DNDEBUG"
push(#l , #l1, #l2, #l3);
chomp(#l);
foreach (#l) {
print "processing file $_ ...";
$err = `g++ $_ $FLAGS`;
if($err == something) {
#do the needful
}
}
so what should be something?
You should check $? instead, after g++....
perlvar
$?
The status returned by the last pipe close, backtick (`` ) command,
successful call to wait() or waitpid(), or from the system() operator.
The exit value of the subprocess is really ($?>> 8)
So you should check if g++ returned 0 (success) or non-zero.
if ($? >> 8) {
/* Error? */
}
IPC::System::Simple/IPC::Run3 make this easier