Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash command result text format mismatch

team!
I want to execute bash command into variable.

COMMAND="systemctl status wazuh-manager --no-pager"
NEWMESSAGE="$($COMMAND)"
if [[ -n $NLINES ]]; then
     $NEWMESSAGE | tail -n $NLINES
fi
if [[ -n $OUTPUTFILE ]]; then
    echo -e $NEWMESSAGE |& tee -a $OUTPUTFILE
fi

I have output like this.
Single line with no color text.

● wazuh-manager.service - Wazuh manager Loaded: loaded (/lib/systemd/system/wazuh-manager.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-05-30 19:58:47 MSK; 17h ago Process: 12449 ExecStart=/usr/bin/env /var/ossec/bin/wazuh-control start (code=exited, status=0/SUCCESS) Tasks: 138 (limit: 28719) Memory: 1.0G CGroup: /system.slice/wazuh-manager.service ├─12505 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py ├─12526 /var/ossec/bin/wazuh-integratord ├─12547 /var/ossec/bin/wazuh-authd ├─12563 /var/ossec/bin/wazuh-db ├─12577 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py ├─12580 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py ├─12592 /var/ossec/bin/wazuh-execd ├─12606 /var/ossec/bin/wazuh-analysisd ├─12621 /var/ossec/bin/wazuh-syscheckd ├─12683 /var/ossec/bin/wazuh-remoted ├─12715 /var/ossec/bin/wazuh-logcollector ├─12735 /var/ossec/bin/wazuh-monitord └─12756 /var/ossec/bin/wazuh-modulesd May 30 19:58:39 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-analysisd... May 30 19:58:40 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-syscheckd... May 30 19:58:41 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-remoted... May 30 19:58:43 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-logcollector... May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-monitord... May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12753]: 2022/05/30 19:58:44 wazuh-modulesd: WARNING: The <ignore_time> tag at module 'vulnerability-detector' is deprecated for version newer than 4.3. May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12753]: 2022/05/30 19:58:44 wazuh-modulesd: WARNING: Invalid option 'os' for 'msu' provider at 'vulnerability-detector' May 30 19:58:45 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-modulesd... May 30 19:58:47 UBUNTU-2004-SRV-2.home.local env[12449]: Completed. May 30 19:58:47 UBUNTU-2004-SRV-2.home.local systemd[1]: Started Wazuh manager.

but if i run command in console i have different format.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

systemctl status wazuh-manager --no-pager
● wazuh-manager.service - Wazuh manager
     Loaded: loaded (/lib/systemd/system/wazuh-manager.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-05-30 19:58:47 MSK; 18h ago
    Process: 12449 ExecStart=/usr/bin/env /var/ossec/bin/wazuh-control start (code=exited, status=0/SUCCESS)
      Tasks: 138 (limit: 28719)
     Memory: 1.0G
     CGroup: /system.slice/wazuh-manager.service
             ├─12505 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py
             ├─12526 /var/ossec/bin/wazuh-integratord
             ├─12547 /var/ossec/bin/wazuh-authd
             ├─12563 /var/ossec/bin/wazuh-db
             ├─12577 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py
             ├─12580 /var/ossec/framework/python/bin/python3 /var/ossec/api/scripts/wazuh-apid.py
             ├─12592 /var/ossec/bin/wazuh-execd
             ├─12606 /var/ossec/bin/wazuh-analysisd
             ├─12621 /var/ossec/bin/wazuh-syscheckd
             ├─12683 /var/ossec/bin/wazuh-remoted
             ├─12715 /var/ossec/bin/wazuh-logcollector
             ├─12735 /var/ossec/bin/wazuh-monitord
             └─12756 /var/ossec/bin/wazuh-modulesd

May 30 19:58:39 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-analysisd...
May 30 19:58:40 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-syscheckd...
May 30 19:58:41 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-remoted...
May 30 19:58:43 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-logcollector...
May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-monitord...
May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12753]: 2022/05/30 19:58:44 wazuh-modulesd: WARNING: The <ignore…n 4.3.
May 30 19:58:44 UBUNTU-2004-SRV-2.home.local env[12753]: 2022/05/30 19:58:44 wazuh-modulesd: WARNING: Invalid opt…ector'
May 30 19:58:45 UBUNTU-2004-SRV-2.home.local env[12449]: Started wazuh-modulesd...
May 30 19:58:47 UBUNTU-2004-SRV-2.home.local env[12449]: Completed.
May 30 19:58:47 UBUNTU-2004-SRV-2.home.local systemd[1]: Started Wazuh manager.
Hint: Some lines were ellipsized, use -l to show in full.

How to save text format?

>Solution :

To keep the format and pass the color format of the output of systemctl status .... you can use;

$ var="$(SYSTEMD_COLORS=1 systemctl status wazuh-manager.service)"
$ echo "$var"

The color as well as the format wil now be preserved

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading