I need help with how to compare bash variable to a specific format.
i will read user input with read command
for example:
MyComputer:~/Home$ read interface
eth1
MyComputer:~/Home$ echo $interface
eth1
Now i need to check if "$interface" variable with IF loop(it should have "eth" in beginning and should contains numbers 0-9):
if [[ $interface=^eth[0-9] ]]
then
echo "It looks like an interface name"
fi
Thanks in advance
You can use regular expressions for this:
if [[ $interface =~ ^eth[0-9]+$ ]]
then
...
fi
You can use bash's globs for this:
if [[ $interface = eth+([[:digit:]]) ]]; then
echo "It looks like an interface name"
fi
(avoiding regexps removes one problem). Oh, and mind the spaces around the = sign, and also before and after [[ and ]].
You could use bash V3+ operator =~ as Andrew Logvinov said :
[[ $interface =~ ^eth[0-9]+$ ]] && # ...
Or :
if [[ $interface =~ ^eth[0-9]+$ ]]; then
# ...
fi
Otherwise, you could use too egrep or grep -E (which is useful with older shells like sh...) :
echo "$interface"|egrep "^eth[0-9]+$" > /dev/null && # ...
Or :
if echo "$interface"|egrep "^eth[0-9]+$" > /dev/null; then
# ...
fi
Related
I am taking baby steps at learning bash and I am developing a piece of code which takes an input and checks if it contains any spaces. The idea is that the variable should NOT contain any spaces and the code should consequently echo a suitable message.
Try this:
#!/bin/bash
if [[ $1 = *[[:space:]]* ]]
then
echo "space exist"
fi
You can use grep, like this:
echo " foo" | grep '\s' -c
# 1
echo "foo" | grep '\s' -c
# 0
Or you may use something like this:
s=' foo'
if [[ $s =~ " " ]]; then
echo 'contains space'
else
echo 'ok'
fi
You can test simple glob patterns in portable shell by using case, without needing any external programs or Bash extensions (that's a good thing, because then your scripts are useful to more people).
#!/bin/sh
case "$1" in
*' '*)
printf 'Invalid argument %s (contains space)\n' "$1" >&2
exit 1
;;
esac
You might want to include other whitespace characters in your check - in which case, use *[[:space:]]* as the pattern instead of *' '*.
You can use wc -w command to check if there are any words. If the result of this output is a number greater than 1, then it means that there are more than 1 words in the input. Here's an example:
#!/bin/bash
read var1
var2=`echo $var1 | wc -w`
if [ $var2 -gt 1 ]
then
echo "Spaces"
else
echo "No spaces"
fi
Note: there is a | (pipe symbol) which means that the result of echo $var1 will be given as input to wc -w via the pipe.
Here is the link where I tested the above code: https://ideone.com/aKJdyN
You could use parameter expansion to remove everything that isn't a space and see if what's left is the empty string or not:
var1='has space'
var2='nospace'
for var in "$var1" "$var2"; do
if [[ ${var//[^[:space:]]} ]]; then
echo "'$var' contains a space"
fi
done
The key is [[ ${var//[^[:space:]]} ]]:
With ${var//[^[:space:]]}, everything that isn't a space is removed from the expansion of $var.
[[ string ]] has a non-zero exit status if string is empty. It's a shorthand for the equivalent [[ -n string ]].
We could also quote the expansion of ${var//[^[:space:]]}, but [[ ... ]] takes care of the quoting for us.
I am new to Bash and having a problem checking a variable contains a string
Works:
foo="abc def ghi"
if [[ "$foo" =~ "def" ]]; then
echo "Match!"
fi
Does not work (issue i'm having):
javaVersion="$(java -version)"
if [[ "$javaVersion" =~ "1.8.0_74" ]]; then
echo "Match!"
fi
I have manually checked the variable contains the string 1.8.0_74.
The problem is java -version prints the information to stderr(2) stream instead of stdout(1). You need to capture both of them as 2>&1 which literally means write the standard error output stream also to standard output stream.
javaVersion="$(java -version 2>&1)"
if [[ "$javaVersion" =~ "1.8.0_74" ]]; then
echo "Match!"
fi
will work as expected.
Also you don't a a regex operator for this comparison, a simple glob comparison using the test operator [[ would suffice,
if [[ "$javaVersion" == *"1.8.0_74"* ]]; then
echo "Match!"
fi
I am very new to Bash Scripting and I have a question regarding my CheckOurCodingRules.sh script:
I want to search for every 'hPar,' in a textfile and if found it should be checked if there is a also a 'const' in the same row.
Thats what I got so far but there is something wrong here:
while read line
do
if [[ $line == *hPar\,* ]] && [[ $line == *const\*]];then
DOCUMENTATION_TEST_A=1
else
echo DOCUMENTATION_TEST_A=0
fi
done < $INPUT_FILE
if [[DOCUMENTATION_TEST_A=0]];then
echo "error: Rule1: No const before hpar"
fi
There are a couple of issues with your script, see the code below which works for me:
DOCUMENTATION_TEST_A=0 # initial value
while read line
do
# spaces between conditional and brackets, no backslashes
if [[ $line == *hPar,* ]] && [[ $line == *const* ]]
then
DOCUMENTATION_TEST_A=1
break # optional, no need to scan the rest of the file
fi
done < $INPUT_FILE
# spaces and $, -eq is used for numerical comparisons
if [[ $DOCUMENTATION_TEST_A -eq 0 ]];
then
echo "error: Rule1: No const before hpar"
fi
A cleaner solution would be to use grep:
if ! grep "hPar," $INPUT_FILE | grep "const" >/dev/null
then
echo "error: Rule1: No const before hpar"
fi
I am writing a bash script and I would like to verify if a string is a shell reserved word (like if, for, alias, etc...).
How can I do this?
#!/bin/bash
string="$1"
if [[ $(type "$string" 2>&1) == "$string is a shell"* ]]; then
echo "Keyword $string is reserved by shell"
fi
If you want only the shell keywords, then:
#!/bin/bash
string="$1"
[[ $(type -t "$string" 2>&1) == "keyword" ]] && echo reserved || echo not reserved
builtins won't pass this test (only keywords).
A way of doing this with possibility of extending in various cases:
#!/bin/bash
string="$1"
checkfor=('keyword' 'builtin')
for ((i=0;i<${#checkfor[#]};i++))
do
[[ $(type -t "$string" 2>&1) == "${checkfor[$i]}" ]] && reserved=true && break || reserved=false
done
[[ $reserved == true ]] && echo reserved || echo not reserved
command, hash, alias, type etc.. (builtins) will pass the above test as well as keywords.
You can add other possible test conditions by adding an element into the array checkfor:
checkfor=('keyword' 'builtin' 'file' etc...)
Unfortunately i tried this and it doesn't work, i must use the [[ ]]
read input
for i in input
do
if [[ i = "$input" ]]
then
echo "i"
fi
done
when I run this nothing happens, it only reads my input
This line:
if [[ i = "$input" ]]
should be:
if [[ "$i" = "$input" ]]
OR:
if [[ "$i" == "$input" ]]
PS: Same thing for input also.
Remember that variables in shell are accessed with $ prefix.
May be you can re-factor your script to this:
read input
for i in $input
do
[[ "$i" == "something" ]] && echo "$i"
done
I think when you use only numbers you can also try:
for i in input
do
if [[ $i -eq "$input" ]]
then
echo "$i"
fi
done