Answer the question
In order to leave comments, you need to log in
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}' )
Sys=S4BZJ045
if
then
echo "Ok"
else
echo "NotOK"
sys S4BZJ045
1 1 9
SystemName S4BZJ045
1 1 11
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
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}' )
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:
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question