1

I could need some help with a oneline script im building. The script should give me a list of services, which are running on the current system. I wanted to realize this with systemctl.

The current oneliner looks like this:

systemctl list-units --type service | grep -f /etc/update-motd.d/service.list

However this command always creates a blank line between the services which are listed

  smbd.service                       loaded active running Samba SMB Daemon    

  uuidd.service                      loaded active running Daemon for generating UUIDs  

  virtlogd.service                   loaded active running Virtual machine log manager 

  zabbix-agent.service               loaded active running Zabbix Agent                                                

I would like to remove the empty spaces. After some google research ive found some solutions with tr (like 'tr -d ""' or 'tr -s ""'), but this doesnt seem to work. Ive even tried some solutions with grep or sed but none of them helped me wih this.

edit: following solutions I tried without success: | grep "." | sed '/^$/d | grep "\S"

Some help would be great! Thanks

1
  • you probably have a empty line in ...service.list file
    – user986805
    Apr 1, 2020 at 12:44

3 Answers 3

2

You can try something like this: systemctl list-units --type service | grep -f /etc/update-motd.d/service.list | grep -v ^$ This should remove any emtpy lines

4
  • This also didnt work... seems like the systemctl command opens the output in a "less" like output, not a stdout. Seems like this causes the empty lines, which i cant delete. Any idea, how to get the output to stdout?
    – Paradice
    Apr 1, 2020 at 12:03
  • Works on my Ubuntu 18.04 installation. What is the content of this service.list file?
    – BulletBob
    Apr 1, 2020 at 12:07
  • just some entries like: smbd.service uuidd.service
    – Paradice
    Apr 1, 2020 at 12:21
  • Well.... now it works... just reconnected via ssh and tried something and worked and now your example works to... maybe a brokenpipe thingy in the ssh connection!? im just kinda cnfused right now....
    – Paradice
    Apr 1, 2020 at 12:28
1

Using tr to squeeze newlines:

$ systemctl list-units --type service | \
  grep -f <(tr < /etc/update-motd.d/service.list -s \\n)
0

What you are seeing are not empty lines - they're long trailing sequences of space characters that appear to be part of the systemctl tabular output:

$ systemctl list-units --type service | grep bluetooth | xxd
00000000: 626c 7565 746f 6f74 682e 7365 7276 6963  bluetooth.servic
00000010: 6520 2020 2020 2020 2020 2020 2020 2020  e               
00000020: 2020 2020 2020 2020 2020 2020 2020 2020                  
00000030: 2020 2020 2020 2020 2020 2020 2020 2020                  
00000040: 2020 2020 2020 2020 2020 2020 2020 2020                  
00000050: 2020 2020 2020 2020 2020 6c6f 6164 6564            loaded
00000060: 2061 6374 6976 6520 7275 6e6e 696e 6720   active running 
00000070: 426c 7565 746f 6f74 6820 7365 7276 6963  Bluetooth servic
00000080: 6520 2020 2020 2020 2020 2020 2020 2020  e               
00000090: 2020 2020 2020 2020 2020 2020 2020 2020                  
000000a0: 2020 2020 2020 2020 2020 2020 2020 2020                  
000000b0: 2020 2020 2020 2020 2020 2020 200a                    .

They appear as empty lines because of wrapping by the terminal.

You can remove them with tr, for example by squeezing horizontal whitespace:

$ systemctl list-units --type service |  tr -s '[:blank:]' | grep -f service.list 
bluetooth.service loaded active running Bluetooth service 
cron.service loaded active running Regular background program processing daemon 
polkit.service loaded active running Authorization Manager 

Another option, which preserves the tabular format better, would be to remove only trailing spaces:

$ systemctl list-units --type service | sed 's/ *$//' | grep -f service.list 
bluetooth.service                                                                         loaded active running Bluetooth service
cron.service                                                                              loaded active running Regular background program processing daemon
polkit.service                                                                            loaded active running Authorization Manager

There should be a way to stop systemctl from trying to format the output (or at least to be responsive to the COLUMNS environment variable), but I can't find it.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .