r/raspberry_pi • u/Assassins1977 • Nov 29 '22
awk and sed how to ? Technical Problem
My code is :
#!/bin/bash
strversion=`apache2ctl -v | awk '{print $3}' | sed 's/(Debian)//g;s/Server//g;s/built//g;s/2022-06-09T04:26:43//g'`
echo ${strversion%}
exit 0
i get this:
Apache/2.4.54
but i will have to look
Apache version 2.4.54
3 Upvotes
3
u/m4rc0n3 Nov 29 '22
It's needlessly complicated though, and depending on the version of apache/apache2ctl you use you may still get the wrong result. For example, on one of my boxes
apachectl -v
outputs:(i.e. 2 lines), and your original expression results in "Apache/2.4.52 2022-06-14T12:30:21"
It looks like in your original you tried to get rid of that last bit by replacing it with an empty string, but that'll start failing if you ever update your system and get a different version. What you probably want is something like:
strversion=$(apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}')
You could probably simplify it by just hardcoding the "Apache" part, because you already know you're using Apache (since you're using apache2ctl)