0

I'm trying to get a software version by using the bash script. The Command is simple:

/usr/local/sbin/keepalived -v

This is the output:

Keepalived v2.0.13 (02/18,2019), git commit v2.0.12-53-ga9ed1993+

Copyright(C) 2001-2019 Alexandre Cassen,

Built with kernel headers for Linux 3.10.0 Running on Linux 3.10.0-957.5.1.el7.x86_64 #1 SMP Fri Feb 1 14:54:57 UTC 2019

Config options: NFTABLES LVS VRRP VRRP_AUTH OLD_CHKSUM_COMPAT FIB_ROUTING SNMP_V3_FOR_V2 SNMP_VRRP SNMP_CHECKER SNMP_RFCV2 SNMP_RFCV3 FILE_LOGGING LOG_FILE_APPEND EINTR_CHECK

System options: PIPE2 SIGNALFD INOTIFY_INIT1 VSYSLOG EPOLL_CREATE1 IPV6_ADVANCED_API LIBNL3 RTA_ENCAP RTA_EXPIRES RTA_PREF FRA_TUN_ID RTAX_CC_ALGO RTAX_QUICKACK FRA_OIFNAME IFA_FLAGS IP_MULTICAST_ALL NET_LINUX_IF_H_COLLISION LIBIPTC_LINUX_NET_IF_H_COLLISION LIBIPVS_NETLINK VRRP_VMAC CN_PROC SOCK_NONBLOCK SOCK_CLOEXEC O_PATH GLOB_BRACE INET6_ADDR_GEN_MODE SO_MARK SCHED_RT SCHED_RESET_ON_FORK

From this output i need only the first row:

Keepalived v2.0.13 (02/18,2019), git commit v2.0.12-53-ga9ed1993+

I tried the following methods, but none of them works for me:

$ /usr/local/sbin/keepalived -v | head -n 1
$ echo "$(/usr/local/sbin/keepalived -v)" | head -n 1

In both cases, I'm receiving the full output instead of the first line only.

I tried to send stdout into file:

$ /usr/local/sbin/keepalived -v > /tmp/keepalived.txt
$ echo $(/usr/local/sbin/keepalived -v) > /tmp/keepalived.txt
$ /usr/local/sbin/keepalived -v | tee /tmp/keepalived.txt

But, I'm getting an empty file.

Can someone explain why is that happening and how can I get the first line only?

1

It's happening because the output of /usr/local/sbin/keepalived -v is going to stderr instead of stdout.

You can redirect the output to stdout before extracting the data you want by doing keepalived -v 2>&1 | head -n 1

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.