Check input consists only of numeric characters in shell script - shell

I need to check that the input consists only of numeric characters. I have the code below, but it didn't work properly.
if [[ $1 =~ [0-9] ]]; then
echo "Invalid input"
fi
It should give true only for 678686 not for yy66666.

How about this:-
re='^[0-9]+$'
if ! [[ $Number =~ $re ]] ; then
echo "error: Invalid input" >&2; exit 1
fi
or
case $Number in
''|*[!0-9]*) echo nonnumeric;;
*) echo numeric;;
esac

Try using start/end anchors with your pattern. If you don't, the match succeeds with a part of a test string. Don't forget that you have to use a pattern matching the complete test string if you follow this suggestion.
if [[ $1 =~ ^[0-9]+$ ]]; then
echo "Invalid input"
fi
Check out this SO post for more details.

Related

Not able to validate string in shell script

echo "enter variable"
read var
if[[ ${var} = ^[a-zA-Z][a-zA-Z0-9]{0,25}$ ]];then
echo "valid"
else
echo "invalid"
fi
I'm trying to validate(starting with a character followed by alphanumeric, no special character). 26 characters allowed.
For every input, it gives valid. What am I missing here?
This should work (please note the tilde):
echo "enter variable"
read var
if [[ ${var} =~ ^[a-zA-Z][a-zA-Z0-9]{0,25}$ ]] ; then
echo "valid"
else
echo "invalid"
fi

Bash checking if string does not contain other string

I have a string ${testmystring} in my .sh script and I want to check if this string does not contain another string.
if [[ ${testmystring} doesNotContain *"c0"* ]];then
# testmystring does not contain c0
fi
How can I do that, i.e. what is doesNotContain supposed to be?
Use !=.
if [[ ${testmystring} != *"c0"* ]];then
# testmystring does not contain c0
fi
See help [[ for more information.
Bash allow u to use =~ to test if the substring is contained.
Ergo, the use of negate will allow to test the opposite.
fullstring="123asdf123"
substringA=asdf
substringB=gdsaf
# test for contains asdf, gdsaf and for NOT CONTAINS gdsaf
[[ $fullstring =~ $substring ]] && echo "found substring $substring in $fullstring"
[[ $fullstring =~ $substringB ]] && echo "found substring $substringB in $fullstring" || echo "failed to find"
[[ ! $fullstring =~ $substringB ]] && echo "did not find substring $substringB in $fullstring"
As mainframer said, you can use grep, but i would use exit status for testing, try this:
#!/bin/bash
# Test if anotherstring is contained in teststring
teststring="put you string here"
anotherstring="string"
echo ${teststring} | grep --quiet "${anotherstring}"
# Exit status 0 means anotherstring was found
# Exit status 1 means anotherstring was not found
if [ $? = 1 ]
then
echo "$anotherstring was not found"
fi

How to check whether a string starts with a substring

I am writing a very simple script which copies a file to a path provided by the user as a parameter and I would like to make sure the path starts with /var/log/messages.
So I've written this:
if [[ $2 -ne /var/log/messages/* ]]
then
echo "Incorrect path, must be under /var/log/messages/"
exit
fi
But it doesn't seem to work.
What am I doing wrong?
This should work:
if [[ "$2" != "/var/log/messages/"* ]]
then
echo "Incorrect path, must be under /var/log/messages/"
exit
fi
You can use regular expressions as well (starting from Bash version 3). Just remember this:
Every quoted part of the regular expression is taken literally, even if it contains regular expression special characters.
In this case, you could do something like this:
if [[ ! "$2" =~ ^"/var/log/messages/" ]]
then
echo "Incorrect path, must be under /var/log/messages/"
exit
fi
You should in general provide a minimal non-working example. However, I suspect you want:
if [[ $2 != /var/log/messages/* ]]
then
echo "Incorrect path, must be under /var/log/messages/"
exit
fi
You could use dirname:
if [[ `dirname $2` != "/var/log/messages" ]]
then
echo "Incorrect path, must be under /var/log/messages/"
exit
fi
UNTESTED:
...
case $2 in
/var/log/messages/*) ;;
# do whatever...
*)
echo "incorrect ..."
break
;;
esac
...

How can I compare two similar strings with a wildcard in bash?

How can I best compare two similar strings in bash?
I want to compare 1.1.1.1:1000 to 1.1.1.1 and find it to be a match.
1.1.1.1:1000 1.1.1.1 MATCH
1.1.1.2:1000 1.1.1.1 NO MATCH
Here's a simple script illustrating the challenge:
#!/bin/sh
IPONE="1.1.1.1:1000"
IPTWO="1.1.1.1"
if [[ "$IPONE" == "$IPTWO*" ]] ; then
echo "$IPONE $IPTWO Match"
else
echo "$IPONE $IPTWO ERROR"
fi
If I understand correctly, you want to compare, for an exact match, the part before the colon. In that case:
if [[ "${IPONE%%:*}" == "${IPTWO%%:*}" ]] ; then
echo "$IPONE $IPTWO Match"
else
echo "$IPONE $IPTWO ERROR"
fi
If you want to use glob-syntax, then you need to use =~ instead of == in your [[ ... ]], and you can't have the second parameter quoted. So it should like this:
if [[ "$IPONE" =~ $IPTWO* ]]; then
If you need to have the second parameter quoted, just do this:
if [[ "$IPONE" =~ "$IPTWO"* ]]; then
If you need to find a match that matches a string up to the last of a delimiter, in this case ., then try this:
if [[ "$IPONE" =~ ([0-9]|\.){3}[0-1]* ]]; then
I think POSIX substring parameter expansion, may be have trick
#!/bin/bash
IPONE="1.1.1.1:1000"
IPTWO="1.1.1.1"
if test "${IPONE#*$IPTWO}" != "$IPONE"
then
echo "$IPONE $IPTWO Match"
else
echo "$IPONE $IPTWO ERROR"
fi
OR
IPONE="1.1.1.1:1000"
IPTWO="1.1.1.1"
if [[ "${IPONE}" == *$IPTWO* ]]
then
echo "$IPONE $IPTWO Match"
else
echo "$IPONE $IPTWO ERROR"
fi

Validating filename in Bash

I want to validate a file name in bash to make sure that I don't have this '[]' character in it
I have this :
if ! [[ $filename=~ ^[a-zA-Z]+$ ]]; then
echo 'Wrong filename input' >&2
exit 1
fi
but I want explicitly avoid [] and allow other special characters.
any advice?
Thanks.
Use spaces around =~ operator:
[[ ! "$filename" =~ ^[a-zA-Z]+$ ]] && echo "bad filename" || echo "its good"
OR your own script:
if [[ ! "$filename" =~ ^[a-zA-Z]+$ ]]; then
echo 'Wrong filename input' >&2
exit 1
fi
Update:
If you want to explicitly avoid only [ and ] then following check is better:
if [[ "$filename" == *[]\[]* ]]; then
echo 'Wrong filename input' >&2
exit 1
fi

Resources