How do I compare a variable to a string (and do something if they match)?
相关问题
- how to split a list into a given number of sub-lis
- Generate string from integer with arbitrary base i
- JQ: Select when attribute value exists in a bash a
- Converting a string array to a byte array
- bash print whole line after splitting line with if
相关文章
- JSP String formatting Truncate
- Check if directory exists on remote machine with s
- Selecting only the first few characters in a strin
- Python: print in two columns
- Reverse four length of letters with sed in unix
- Launch interactive SSH bash session from PHP
- extending c++ string member functions
- Google app engine datastore string encoding proble
Notes:
if
and[
and]
are important>
and<
are redirection operators so escape it with\>
and\<
respectively for strings.following script reads from a file named "testonthis" line by line then compares each line with a simple string, a string with special characters and a regular expression if it doesn't match then script will print the line o/w not.
space in bash is so much important. so following will work
but following won't:
so please use as is:
Bash4+ examples. Note: not using quotes will cause issues when words contain spaces etc.. Always quote in bash IMO.
Here are some examples BASH4+ :
Example 1, check for 'yes' in string (case insensitive):
Example 2, check for 'yes' in string (case insensitive):
Example 3, check for 'yes' in string (case sensitive) :
Example 4, check for 'yes' in string (case sensitive):
Example 5, exact match (case sensitive):
Example 6, exact match (case insensitive):
Example 7, exact match :
enjoy.
I would probably use regexp matches if the input has only a few valid entries. E.g. only the "start" and "stop" are valid actions.
Note that I lowercase the variable
$ACTION
by using the double comma's. Also note that this won't work on too aged bash versions out there.