Bash - Check if the string starts with a predefined string (substring) - linux

I have a variable $projectName how can i check if it starts with this string 'testProject'

You can use this check in BASH:
[[ "$projectName" == "testProject"* ]]

You can for example use:
[[ "$projectName" =~ ^testProject ]] && echo "yes"
^
beginning of line
Test
$ var="hello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
yes
$ var="ahello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
no

Related

How to simplify the comparison in the bash?

how to make several comparisons in the bash by placing the condition and comparison points next to instead of the long queue ?
that something like this
before :
if [[ $var == "aaa" || $var == "bbb" || $var == "ccc" || $var == "ddd" ]];
then
echo "good";
fi
after (what I want):
if [[ $var==["aaa","bbb","ccc","ddd"] ]];
then
echo "good";
fi
With extended pattern matching:
shopt -s extglob
[[ $var = #(aaa|bbb|ccc|ddd) ]] && echo "good"
Try this using bash regex with the keywork =~:
if [[ $var =~ ^(aaa|bbb|ccc|ddd)$ ]];
then
echo "good";
fi
Edit :
As seen in comments, for real you need to compare int, not strings, so :
((var%3 == 0)) && echo "ok"
Using bash arithmetic

What does "-?" mean in bash script

I have this script I am looking at, learning scripting but I cannot figure out what this line means:
if [[ $1 = "-?" ]]
I understand the $1 is first argument, but the after the equals I cannot figure it out the -?.h
if [[ $1 = "-?" ]]
then
echo "Use is: 235.sh <username>"
exit 1
I think it would be checking to see if the first argument is a string equal to -?
You can check this by running:
# [[ "a" = "-?" ]] && echo true
# [[ "-a" = "-?" ]] && echo true
# [[ "a-?" = "-?" ]] && echo true
# [[ "-?" = "-?" ]] && echo true
true
I would guess that is comparing something to the string "-?".

How to check if a string contains brackets ")("?

I'm trying to check if an input string contains parentheses, which are: ()[]{}.
I wrote the following code:
#!/bin/bash
str="$1"
if [ -z "$str" ]; then
echo "Usage: $(basename $0) string"
exit 1
fi
if [[ "$str" == *['\{''}''\[''\]''('')']* ]];
then
echo "True"
else
echo "False"
fi
If the string contains any of these: []{} then the output is correct but if the string contains () then I get an error:
-bash: syntax error near unexpected token `('
These are the things I've tried so far:
*['\(''\)']*
*['()']*
*[()]*
Any idea how it should be written?
Edit #1:
[root#centolel ~]# date
Tue Nov 3 18:39:37 IST 2015
[root#centolel ~]# bash -x asaf.sh {
+ str='{'
+ '[' -z '{' ']'
+ [[ { == *[{}\[\]\(\)]* ]]
+ echo True
True
[root#centolel ~]# bash -x asaf.sh (
-bash: syntax error near unexpected token `('
[root#centolel ~]#
You can use this glob pattern with () and [] escaped inside [...]:
[[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
Testing:
str='abc[def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc[def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc(def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc)def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc{}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abc}def' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
yes
str='abcdef' && [[ $str == *[{}\(\)\[\]]* ]] && echo "yes" || echo "no"
no

How do I test if a variable is a string in bash?

I tried the following but without success
[root#OBAMA~]# bash
[root#OBAMA~]# a=HelloWorld
[root#OBAMA~]# [[ $a == [A-Za-z] ]] && echo "YES ITS STRING"
( the command not prints anything )
[root#OBAMA~]# [[ $a == [A-Z][a-z] ]] && echo "YES ITS STRING"
( the command not prints anything )
Change your command lke below.
$ [[ $a =~ [A-Za-z]+ ]] && echo "YES ITS STRING"
YES ITS STRING
Use =~ operator to test an input string against a regex.
Add + next to the character class, so that it would repeat the previous pattern or token one or more times. Here it's unnecessary.
Add anchors , in-order to do an exact string match. [[ $a =~ [A-Za-z] ]] && echo "YES ITS STRING" alone will print the string YES ITS STRING because the variable a contains atleast an alphabet.
$ a="HelloWorld"
$ [[ $a =~ ^[A-Za-z]+$ ]] && echo "YES ITS STRING"
YES ITS STRING
$ a="Hello World"
$ [[ $a =~ ^[A-Za-z]+$ ]] && echo "YES ITS STRING"
$
how do you define "a string"
[[ -n $a ]] && echo variable a is not empty
[[ $a == *[[:alpha:]]* ]] && echo variable a contains a letter
shopt -s extglob failglob
[[ $a == +([[:alpha:]]) ]] && echo variable a only has letters
Your glob expressions are not matching because your checking that your variable contains only 1 character or 2 characters.

how to use if to see whether file has suffix in shell bash script [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 9 years ago.
if[ xxx ]
how to expresss the string or file include '.'
I am new to study shell,thanks for any help
You can use the matching operator:
$ if [[ "abc.def" =~ \. ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ "abcdef" =~ \. ]]; then echo "yes"; else echo "no"; fi
no
This matches if the dot is the first or last (or only) character in the string. If you expect characters on both sides of the dot, you can do the following:
$ if [[ "ab.cdef" =~ .\.. ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ ".abcdef" =~ .\.. ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "abcdef." =~ .\.. ]]; then echo "yes"; else echo "no"; fi
no
You can also use pattern matching:
$ if [[ "ab.cdef" == *?.?* ]]; then echo "yes"; else echo "no"; fi
yes
$ if [[ ".abcdef" == *?.?* ]]; then echo "yes"; else echo "no"; fi
no
$ if [[ "abcdef." == *?.?* ]]; then echo "yes"; else echo "no"; fi
no
A good reference for both patterns and regexes is at Greg's Wiki
bash supports glob-style pattern matching:
if [[ "$file" = *?.?* ]]; then
...
fi
Note that this assumes a prefix as well - this also ensures that it will not match the . and .. directories.
If you want to check for a specific extension:
if [[ "$file" = *?.foo ]]; then
...
fi
echo "xxx.yyy" | grep -q '\.'
if [ $? = 0 ] ; then
# do stuff
fi
Or
echo "xxx.yyy" | grep -q '\.' && <one statement here>
#e.g.
echo "xxx.yyy" | grep -q '\.' && echo "got a dot"

Resources