Answer the question
In order to leave comments, you need to log in
How to form an if condition in bash?
MSG variable is the text in which to track.
For example: speed 22
if ; then
echo "СКОРОСТЬ = ${MSG//[^0-9]/}";
end
if ; then
echo "Модель = ?????"
end
Answer the question
In order to leave comments, you need to log in
celovec
here is an example
- by the way it works for both bash and ash (alpin)
#!/bin/bash
# или !/bin/sh в докере на apline
MSG='port 123'
if ; then
echo "PORT = ${MSG//[^0-9]/}";
fi
MSG='host mysql_db'
if ; then
echo "HOST = ${MSG//host /}";
fi
PORT = 123
HOST = mysql_db
celovec , the text in the variable is always two words?
Use pseudo-arrays:
[email protected]:~$ MSG=( speed 22 )
[email protected]:~$ if ; then echo "Speed = ${MSG[1]}"; fi
Speed = 22
[email protected]:~$ MSG=( car AUDI )
[email protected]:~$ if ; then echo "Auto = ${MSG[1]}"; fi
Auto = AUDI
[email protected]:~$ MSG=(car AUDI)
[email protected]:~$ echo ${MSG[1]}
AUDI
[email protected]:~$ MSG+=("test text")
[email protected]:~ $ echo ${MSG[2]}
test text
[email protected]:~$ echo ${MSG[@]}
car AUDI test text
[email protected]:
[email protected]:~$ echo ${MSG[@]}
car BMW test text
[email protected]:~$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question