Linux /bin/sh check if string contains X
You can use a case statement:
case "$myvar" in
*string*) echo yes ;;
* ) echo no ;;
esac
All you have to do is substitute string
for whatever you need.
For example:
case "HELLOHELLOHELLO" in
*HELLO* ) echo "Greetings!" ;;
esac
Or, to put it another way:
string="HELLOHELLOHELLO"
word="HELLO"
case "$string" in
*$word*) echo "Match!" ;;
* ) echo "No match" ;;
esac
Of course, you must be aware that $word
should not contain magic glob characters unless you intend glob matching.
How to check if a string contains a substring in Bash
You can use Marcus's answer (* wildcards) outside a case statement, too, if you use double brackets:
string='My long string'
if [[ $string == *"My long"* ]]; then
echo "It's there!"
fi
Note that spaces in the needle string need to be placed between double quotes, and the *
wildcards should be outside. Also note that a simple comparison operator is used (i.e. ==
), not the regex operator =~
.
How do you tell if a string contains another string in POSIX sh?
Here's yet another solution. This uses POSIX substring parameter expansion, so it works in Bash, Dash, KornShell (ksh), Z shell (zsh), etc.
test "${string#*$word}" != "$string" && echo "$word found in $string"
A functionalized version with some examples:
# contains(string, substring)
#
# Returns 0 if the specified string contains the specified substring,
# otherwise returns 1.
contains() {
string="$1"
substring="$2"
if test "${string#*$substring}" != "$string"
then
return 0 # $substring is in $string
else
return 1 # $substring is not in $string
fi
}
contains "abcd" "e" || echo "abcd does not contain e"
contains "abcd" "ab" && echo "abcd contains ab"
contains "abcd" "bc" && echo "abcd contains bc"
contains "abcd" "cd" && echo "abcd contains cd"
contains "abcd" "abcd" && echo "abcd contains abcd"
contains "" "" && echo "empty string contains empty string"
contains "a" "" && echo "a contains empty string"
contains "" "a" || echo "empty string does not contain a"
contains "abcd efgh" "cd ef" && echo "abcd efgh contains cd ef"
contains "abcd efgh" " " && echo "abcd efgh contains a space"
bash - checking if a string contains #
Your code is clearly running with /bin/sh
, not bash.
An alternative to [[ $line = *"#"* ]]
that works with /bin/sh
is case
.
Thus, the following will work with /bin/sh
, or when invoked with sh yourscript
:
#!/bin/sh
while read -r line; do : line="$line"
case $line in
*"#"*) echo "Line is commented out: $line";;
"") echo "Line is empty" ;;
*) key=${line%%=*}
value=${line#*=}
eval "$key="'$line' # unsafe, but works with /bin/sh, which doesn't have better
# indirect assignment approaches.
printf '%s\t\t-\t\t%s\n' "$key" "$value"
;;
esac
done <input_file.ini
Alternately, consider putting in a guard to handle the case when your script is invoked with a non-bash shell:
#!/bin/bash
case $BASH_VERSION in
'')
echo "ERROR: Run with a non-bash shell" >&2
if [ "$tried_reexec" ]; then
echo "ERROR: Already attempted reexec and failed" >&2
exit 1
fi
if [ -s "$0" ]; then
export tried_reexec=1
exec bash "$0" "$@"
fi
;;
esac
while read -r line; do
if [[ $line = *"#"* ]]; then
echo "Line is Commented out: $line"
elif [[ "$line" = "" ]]; then
echo "Blank line"
else
key=${line%%=*}; value=${line#*=}
printf -v "$key" %s "$value"
printf '%s\t\t-\t\t%s\n' "$key" "$value"
fi
done <input_file.ini
How to check if a file contains a specific string using Bash
if grep -q SomeString "$File"; then
Some Actions # SomeString was found
fi
You don't need [[ ]]
here. Just run the command directly. Add -q
option when you don't need the string displayed when it was found.
The grep
command returns 0 or 1 in the exit code depending on
the result of search. 0 if something was found; 1 otherwise.
$ echo hello | grep hi ; echo $?
1
$ echo hello | grep he ; echo $?
hello
0
$ echo hello | grep -q he ; echo $?
0
You can specify commands as an condition of if
. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.
$ if /bin/true; then echo that is true; fi
that is true
$ if /bin/false; then echo that is true; fi
$
As you can see you run here the programs directly. No additional []
or [[]]
.
To check if a variable in Unix contains any of the string mentioned in a file bash shell
Parameter matching can use extended patterns:
#! /bin/bash
check=$(tr '\n' '|' < list_of_terms.txt)
check="@(${check%|})"
for service in Service1 GetFocus WalletLost ; do
if [[ $service == *$check* ]] ; then
echo $service matches
fi
done
In Bash, how can I check if a string begins with some value?
This snippet on the Advanced Bash Scripting Guide says:
# The == comparison operator behaves differently within a double-brackets
# test than within single brackets.
[[ $a == z* ]] # True if $a starts with a "z" (wildcard matching).
[[ $a == "z*" ]] # True if $a is equal to z* (literal matching).
So you had it nearly correct; you needed double brackets, not single brackets.
With regards to your second question, you can write it this way:
HOST=user1
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes1
fi
HOST=node001
if [[ $HOST == user1 ]] || [[ $HOST == node* ]] ;
then
echo yes2
fi
Which will echo
yes1
yes2
Bash's if
syntax is hard to get used to (IMO).
Checking if output of a command contains a certain string in a shell script
Test the return value of grep:
./somecommand | grep 'string' &> /dev/null
if [ $? == 0 ]; then
echo "matched"
fi
which is done idiomatically like so:
if ./somecommand | grep -q 'string'; then
echo "matched"
fi
and also:
./somecommand | grep -q 'string' && echo 'matched'
Related Topics
Changing Color of Eclipse Links in Quick Fix or Eclipse Links in Preferences on Linux
How to Join Multiple PDF Pages to a Single Page
Write to a File After Piping Output from Tail -F Through to Grep
What Happened to Socket If Network Has Broken Down
How to Install the Specific Version of Postgres
Bash (Or Other Shell): Wrap All Commands with Function/Script
Top Command First Iteration Always Returns the Same Result
Is There Way to Delete Duplicate Header in a File in Unix
How to Autocomplete a Bash Commandline with File Paths
This Script Wont Sort Correctly by Age
Why Do We Need to Call Poll_Wait in Poll
Installing Multiple Versions of R
How to Make Static Linked Elf File to Load Ld_Preload .So
Git Clone from Linux to Tfs Git Repo
Triggering Autokey Script via Mouse Button - How To
How to Configure a Specific Ip in Prometheus Yml Configuration File