While easy in sed (where the last line is known and can be addressed with "$") this is more difficult in awk . One way would be to print the ORS before the output string except for the first line.
/sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status
execution of the above $var1 executes only
/sbin/service cmserver_was.init status
, but not all the services status check with logical AND, can you suggest whats going wrong!
$var1 execution output:
scripts]# $var1
Server - cmserver is running (5330).
while just the copy paste of the $var content execution:
# /sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status
Server - cmserver is running (5330).
Server - fmserver is running (6977).
Server is running.
Server - intgserver is running (15401).
Server - itsmserver is running (8771).
Server - server2 is running (7679).
Server - sysserver is running (2698).
---------- Post updated 03-17-17 at 09:11 AM ---------- Previous update was 03-16-17 at 04:59 PM ----------
Thanks RudiC.
used the suggested and it works fine, But the execution of the variable fails like below,
[node14 init.d]# export service_cmd=/sbin/service
[node14 init.d]# var1=`cd /etc/init.d/; ls *was.init | awk '!/dmgr/ && !/NodeAgent/ && !/interdependentwas/ {print TRS "${service_cmd} "$0 " status"; TRS = " && "}' ORS=""`
[node14 init.d]# echo $var1
${service_cmd} cmserver_was.init status && ${service_cmd} fmserver_was.init status && ${service_cmd} ihs_was.init status && ${service_cmd} intgserver_was.init status && ${service_cmd} itsmserver_was.init status && ${service_cmd} server2_was.init status && ${service_cmd} sysserver_was.init status
[node14 init.d]# $var1
-bash: ${service_cmd}: command not found
[node14 init.d]#
but the copy paste of the command gives the proper output,
node14 ~]# ${service_cmd} cmserver_was.init status && ${service_cmd} fmserver_was.init status && ${service_cmd} ihs_was.init status && ${service_cmd} intgserver_was.init status && ${service_cmd} itsmserver_was.init status && ${service_cmd} server2_was.init status && ${service_cmd} sysserver_was.init status
Server - cmserver is running (5330).
Server - fmserver is running (6977).
HTTPServer is running.
Server - intgserver is running (15401).
Server - itsmserver is running (8771).
Server - server2 is running (7679).
Server - sysserver is running (2698).
Can yu pls suggest and guide whats going wrong, and how to achieve this?
[node14 ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.8 (Santiago)
even below one fails to execute completely
[node14 ~]# var1=$(cd /etc/init.d/; ls *was.init | awk 'BEGIN{service_cmd="/sbin/service"} !/dmgr/ && !/NodeAgent/ && !/interdependentwas/ {print TRS service_cmd" "$0 " status"; TRS = " && "}' ORS="")
[node14 ~]# echo $var1
/sbin/service cmserver_was.init status && /sbin/service fmserver_was.init status && /sbin/service ihs_was.init status && /sbin/service intgserver_was.init status && /sbin/service itsmserver_was.init status && /sbin/service server2_was.init status && /sbin/service sysserver_was.init status
[node14 ~]# $(echo $var1)
Server - cmserver is running (5330).
[node14 ~]#
That's because ${service_cmd} has to be expanded by the shell. Which is no problem when entering that line at the command prompt. When contained in a variable like var , expansion would need to occur twice... That what the (dangerous and deprecated) eval shell command is for - be aware that you need to know exactly what you are doing and what the contents of the to-be-interpreted string (here: var's contents) is. Any malicious command will be executed without further notice / warning.
Did you try to pipe above awk 's output through another shell like ls ... | awk ... | sh ? Here as well you need to know exactly WHAT will be interpreted!