surfrock66
surfrock66

Reputation: 79

Sed on bash skips lines with question mark (?) in them, sed or io redirection issue?

I'm trying to produce an html-formatted email of my server status, I'm running ubuntu server 14.04.1 and using the bash shell. I wanted to output an HTML-formatted list of services for an e-mail to myself. I'm trying to use the following command, which should format the output of server --status-all:

sudo service --status-all | sed -e 's/^ \[/<li>\[/g' -e 's/$/<\/li>/g'

The output skips processing any lines with the question mark (?) character. I can't seem to figure out why, or even if it's an issue in sed or in IO redirection in general. Any help is appreciated. This is the output:

<li>[ + ]  acpid</li>
 [ ? ]  ajaxterm
<li>[ - ]  anacron</li>
<li>[ + ]  apache2</li>
<li>[ + ]  apparmor</li>
 [ ? ]  apport
<li>[ + ]  atd</li>
<li>[ + ]  avahi-daemon</li>
<li>[ + ]  bind9</li>
 [ ? ]  binfmt-support
<li>[ + ]  bluetooth</li>
<li>[ - ]  brltty</li>
 [ ? ]  console-setup
<li>[ + ]  cron</li>
 [ ? ]  cryptdisks
 [ ? ]  cryptdisks-early
<li>[ + ]  cups</li>
<li>[ + ]  cups-browsed</li>
<li>[ - ]  dbus</li>
 [ ? ]  dns-clean
<li>[ - ]  docker</li>
<li>[ + ]  exim4</li>
<li>[ + ]  friendly-recovery</li>
<li>[ - ]  grub-common</li>
 [ ? ]  iptables-persistent
 [ ? ]  irqbalance
<li>[ - ]  isc-dhcp-server</li>
<li>[ + ]  kerneloops</li>
 [ ? ]  killprocs
 [ ? ]  kmod
 [ ? ]  lightdm
<li>[ + ]  mdadm</li>
 [ ? ]  mdadm-waitidle
<li>[ + ]  minidlna</li>
 [ ? ]  mysql
 [ ? ]  networking
<li>[ + ]  nmbd</li>
 [ ? ]  ondemand
<li>[ - ]  postfix</li>
 [ ? ]  pppd-dns
<li>[ - ]  procps</li>
<li>[ - ]  pulseaudio</li>
 [ ? ]  rc.local
<li>[ + ]  resolvconf</li>
<li>[ + ]  rpcbind</li>
<li>[ - ]  rsync</li>
<li>[ + ]  rsyslog</li>
<li>[ + ]  samba</li>
<li>[ - ]  samba-ad-dc</li>
<li>[ + ]  saned</li>
 [ ? ]  screen-cleanup
<li>[ + ]  sendmail</li>
 [ ? ]  sendsigs
<li>[ - ]  sipwitch</li>
<li>[ + ]  smbd</li>
 [ ? ]  speech-dispatcher
<li>[ - ]  ssh</li>
<li>[ + ]  subsonic</li>
<li>[ - ]  sudo</li>
<li>[ + ]  tor</li>
<li>[ + ]  transmission-daemon</li>
<li>[ + ]  udev</li>
 [ ? ]  umountfs
 [ ? ]  umountnfs.sh
 [ ? ]  umountroot
<li>[ - ]  unattended-upgrades</li>
<li>[ - ]  urandom</li>
<li>[ + ]  winbind</li>
<li>[ - ]  x11-common</li>
<li>[ + ]  x2goserver</li>
<li>[ + ]  xrdp</li>

Upvotes: 2

Views: 161

Answers (1)

chaos
chaos

Reputation: 9302

It's because all the services, that appear as [ ? ] are printed to the standard error file descriptor (#2). You have to invoke the command as follows:

sudo service --status-all 2>&1 | sed -e 's/^ \[/<li>\[/g' -e 's/$/<\/li>/g'

It redirects the standard error channel to the standard output channel, where sed reads it from its standard input.

Upvotes: 4

Related Questions