C
C
celovec2020-06-13 16:37:02
linux
celovec, 2020-06-13 16:37:02

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

Everything works here, everything suits me.
And how to formulate such an if:
Example: car Audi
if ; then
echo "Модель = ?????"
end

Please help to display the model in echo.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
sergey, 2020-06-14
@celovec

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

we get
PORT =  123
HOST = mysql_db

A
Alexey, 2020-06-13
@AlexeyKolodchenko

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]:~$

S
Saboteur, 2020-06-13
@saboteur_kiev

echo "Модель = ${MSG//car /}"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question