S
S
snofroy2021-08-11 21:02:41
linux
snofroy, 2021-08-11 21:02:41

Parsing a bash file?

Good afternoon colleagues, help to understand.

I have a config file from Lenovo server, I need to check it.

In the file, for example, there is a line:

SystemName: S4BZJ045

In a bash script, I create a variable that pulls S4BZJ045 from this line like this:

SystemName=$(cat server_out.db | grep 'SystemName' | awk '{print $2}' )


And then I try to check if the SystemName variable matches the Sys variable:

Sys=S4BZJ045

if 
then
  echo "Ok"
else
  echo "NotOK"


In response I get NotOK, I understand that the problem is in the extra characters in the SystemName variable. Checked via WC:

sys S4BZJ045
      1       1       9
SystemName S4BZJ045
      1       1      11


Tried using sed on the SystemName variable, same result.

SystemName=$(cat server_out.db | grep 'SystemName' | awk '{print $2}' | sed 's/^*//')

Answer the question

In order to leave comments, you need to log in

4 answer(s)
X
xotkot, 2021-08-11
@snofroy

In a bash script, I create a variable that pulls S4BZJ045 from this line like this:
SystemName=$(cat server_out.db | grep 'SystemName' | awk '{print $2}' )

too verbose command, can be shortened:
SystemName=$(awk '/SystemName/{printf $2}' server_out.db)

In response I get NotOK, I understand that the problem is in the extra characters in the SystemName variable. Checked via WC:

most likely the problem is in the newline, print puts \n at the end , to avoid this use printf

S
Saboteur, 2021-08-12
@saboteur_kiev

Take what you need regularly

SystemName=$(grep -oP "SystemName: \K[A-Za-z0-9]*" server_out.db)
if [ "$SystemName" == " S4BZJ045 ]; then
  echo "Ok"
else
  echo "Not Ok"
fi

S
SOTVM, 2021-08-12
@sotvm

in this case, here the second quotes [] are superfluous,
and in such quotes , wrap the variables.
if [ "$Sys" == "$SystemName" ]
then
echo "Ok"
else
echo "NotOK"
fi

V
ValdikSS, 2021-08-13
@ValdikSS

Most likely you have a CR LF Windows migration file.
Use dos2unix before processing it, that should fix the issue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question