I have 2 values, project and tag. I need check if them are empty/null and if project has only characters (except '-') and if tag has only numbers (expect '.').
User actually invert the order of them, first need be project and after tag.
I try invert the regex of $project
and $tag
if user wrong the order... But nothing happens, no errors and code continue.
if [ -z $project ] ||
[ -z $tag ] ||
[[ $project =~ '/^[0-9]+(\.[0-9]+)*$/' ]] ||
[[ $tag =~ '/^[A-Za-z]+(\-[A-Za-z]+)*$/' ]] ; then
echo Project or tag wrong, please try again. &&
echo Example: file.sh project-test 1.99.2
fi
This code no give me errors, but also not do what I need. What I'm doing wrong here?
---- update with answer
My syntax was wrong, now it's working fine (without quotes).
if [[ $project =~ ^[0-9]+(\.[0-9]+)*$ ]] &&
[[ $tag =~ ^[A-Za-z]+(\-[A-Za-z]+)*$ ]] ; then
echo Correct
else
echo Project or tag wrong, please try again. &&
echo Example: file.sh project-test 1.99.2
fi
only let it pass when everything all rightgrep -cP
uses perl compatible regex, and print the number of matched lines
combined with echo
,count limited to 0 or 1 only
i functioned them and replace 4 "not" condition with 2 "yes" condition
#!/bin/bash
function check_arg() {
project=$1
tag=$2
if [ `echo $project|grep -cP '^[-a-zA-z]+$'` -eq 1 ] &&
[ `echo $tag|grep -cP '^[\.0-9]+$'` -eq 1 ] ;
then
echo all good $project $tag
else
echo Example : file.sh project-test 1.99.2
fi
}
check_arg project-test 99.2
check_arg 1.99.2 project-test
output
all good project-test 99.2
Example : file.sh project-test 1.99.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.