在变量中保留 Bash 命令的颜色和格式
Retain color and format of Bash command in a variable
团队!
我想执行 bash 命令到变量中。
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
我有这样的输出。
没有彩色文本的单行。
● 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.
但是如果我在控制台中 运行 命令我有不同的格式。
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?
保持格式并传递systemctl status ...
输出的颜色格式。你可以使用;
$ var="$(SYSTEMD_COLORS=1 systemctl status wazuh-manager.service)"
$ echo "$var"
现在将保留颜色和格式
团队!
我想执行 bash 命令到变量中。
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
我有这样的输出。
没有彩色文本的单行。
● 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.
但是如果我在控制台中 运行 命令我有不同的格式。
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?
保持格式并传递systemctl status ...
输出的颜色格式。你可以使用;
$ var="$(SYSTEMD_COLORS=1 systemctl status wazuh-manager.service)"
$ echo "$var"
现在将保留颜色和格式