grep or sed how to get apachectl -v out in bash - bash

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

That's because the variable expansion is not quoted. An unquoted variable is subject to Word Splitting (and Filename Expansion)
echo "Apache2 - ${strversion%')'}"
# ...^...........................^
See also Security implications of forgetting to quote a variable in bash/POSIX shells

Try the following:
strversion=$(apache2ctl -v | head -1 | awk -F '[ /]' '{printf "%s version %s", $3, $4}')

Related

bash IFS awk $2

How can I use IFS and awk $2 value on the same line?
ex.
array=(
element:5001
element:5002
element:5003
element:5004
)
IFS=':'
for i in "${array[#]}"
do
set -- $i
part1=$1
part2=$2
cd $part1
# this following line is where I am having the issue.
# $2 equal to part2 from the string split, but the $2 to be
# awk value for $2
echo "$(ps aux | grep '[s]omething --port '$2'' | awk '{print $2}')"
# use part2 later in code
done
Presently, I am only able to use $2 from IFS split.
Note: I have seen post about using the -v option with awk but I believe that is when you want to set a value to run against awk. I would like to print $2 generated from awk.
Thanks to #Barmar and #l0b0.
I need to fix my quotes.
...
echo $(ps aux | grep '[s]omething --port '$2 | awk '{print $2}')
...

Writing an AWK instruction in a bash script

In a bash script, I need to do this:
cat<<EOF> ins.exe
grep 'pattern' file | awk '{print $2}' > results
EOF
The problem is that $2 is interpreted as a variable and the file ins.exe ends up containing
"grep 'pattern' file | awk '{print }' > results", without the $2.
I've tried using
echo "grep 'pattern' file | awk '{print $2}' > results" >> ins.exe
But it's the same problem.
How can I fix this?
Just escape the $:
cat<<EOF> ins.exe
awk '/pattern/ { print \$2 }' file > results
EOF
No need to pipe grep to awk, by the way.
With bash, you have another option as well, which is to use <<'EOF'. This means that no expansions will occur within the string.

print value of environment variable from a file

Problem Description
In my Unix ksh system, I am having an environment variable A on doing
echo ${A}
I am getting the desired value which is hello.
I can check this value in
env | grep ${A}
output: A=hello
or in
printenv | grep ${A}
output: A=hello
Now I have a file file which contains the list of environment variables and I have to fetch the corresponding value.
Now what I tried just for only first value of the file.
env | grep $(cat file | awk 'NR==1{print $1}') --shows nothing
b=$(cat file | awk 'NR==1{print $1}')
env | grep echo $b
b=cat TEMP_ES_HOST_MAP | awk -F"|" 'NR==1{print $2 }'
echo $b
c=eval $b
c=echo $b
Nothing seems to be working.
Thank you
You can use xargs:
awk -F '[$()]+' '{print $1$2}' envfile | xargs printenv
Where:
cat envfile
$(LANG)
$LINES
USER
HISTFILESIZE
If you are looking for the variable named A in the output from env and printenv then using grep ${A} is incorrect (and unsafe/does not work for variables of more than one word).
What you want for that is:
env | grep ^A=
printenv | grep ^A=
So assuming your file looks like this:
VAR1
VAR2
OTHER_VAR
and you want to search for those you could use something like this (assuming you have process substitution):
env | grep -f <(awk '{print "^"$0"="}' varfile)
Assuming you don't have process substitution (or you would rather a simpler solution, I do) you can do this:
env | awk -F = 'NR==FNR{a[$1]++; next} $1 in a' varfile -
Actually this should work too and is even simpler (assuming an awk with ENVIRON):
awk '$1 in ENVIRON {print $1"="ENVIRON[$1]}' varfile

Pass variable to nawk in bash?

I'm trying to pass a variable to nawk in a bash script, but it's not actually printing the $commentValue variable's contents. Everything works great, except the last part of the printf statement. Thanks!
echo -n "Service Name: "
read serviceName
echo -n "Comment: "
read commentValue
for check in $(grep "CURRENT SERVICE STATE" $nagiosLog |grep -w "$serviceName" | nawk -F": " '{print $2}' |sort -u ) ; do
echo $check | nawk -F";" -v now=$now '{ printf( "[%u]=ACKNOWLEDGE_SVC_PROBLEM;"$1";"$2";2;1;0;admin;$commentValue"\n", now)}' >> $nagiosCommand
done
$commentValue is inside an invocation to nawk, so it is considered as a variable in nawk, not a variable in bash. Since you do not have such a variable in nawk, you won't get anything there. You should first pass the variable "inside" nawk using the -v switch just like you did for the now variable; i.e.:
... | nawk -F";" -v now=$now -v "commentValue=$commentValue"
Note the quotes - they are required in case $commentValue contains whitespace.

Problem with backticks in shellscript

I am having a problem getting my shellscript working using backticks. Here is an example version of the script I am having an issue with:
#!/bin/sh
ECHO_TEXT="Echo this"
ECHO_CMD="echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'"
result=`${ECHO_CMD}`;
echo $result;
result=`echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'`;
echo $result;
The output of this script is:
sh-3.2$ ./test.sh
Echo this | awk -F' ' '{print $1}'
Echo
Why does the first backtick using a variable for the command not actually execute the full command but only returns the output of the first command along with the second command? I am missing something in order to get the first backtick to execute the command?
You need to use eval to get it working
result=`eval ${ECHO_CMD}`;
in place of
result=`${ECHO_CMD}`;
Without eval
${ECHO_TEXT} | awk -F' ' '{print \$1}
which will be expanded to
Echo this | awk -F' ' '{print \$1}
will be treated as argument to echo and will be output verbatim. With eval that line will actually be run.
You Hi,
you need to know eval command.
See :
#!/bin/sh
ECHO_TEXT="Echo this"
ECHO_CMD="echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'"
result="`eval ${ECHO_CMD}`"
echo "$result"
result="`echo ${ECHO_TEXT} | awk -F' ' '{print $1}'`"
echo "$result"
Take a look to the doc :
help eval
In your first example echo is parsing the parameters - the shell never sees them. In the second example it works because the shell is doing the parsing and knows what to do with a pipe. If you change ECHO_CMD to be "bash echo ..." it will work.
Bash is escaping your command for you. Try
ECHO_TEXT="Echo this"
ECHO_CMD='echo ${ECHO_TEXT} | awk -F" " "'"{print \$1}"'"'
result=`${ECHO_CMD}`;
echo $result;
result=`echo ${ECHO_TEXT} | awk -F' ' '{print \$1}'`;
echo $result;
Or even better, try set -x on the first line, so you see what bash is doing

Resources