print value of environment variable from a file - shell

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

Related

grep or sed how to get apachectl -v out in 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}')

awk command has different behaviors when executing the exact same code. Why?

I have created a little shellscript that is capable of receiving a list of values such as "MY_VAR_NAME=var_value MY_VAR_NAME2=value2 ...", separated by spaces only. There should be also the possibility to use values such as MY_VAR_NAME='' or MY_VAR_NAME= (nothing).
These values are then used to change the value inside a environment variables file, for example, MY_VAR_NAME=var_value would make the script change the MY_VAR_NAME value inside the .env file to var_value, without changing anything else about the file.
The env file has the following configuration:
NODE_ENV=development
APP_PATH=/media
BASE_URL=http://localhost:3000
ASSETS_PATH=http://localhost:3000
USE_CDN=false
APP_PORT=3000
WEBPACK_PORT=8080
IS_CONNECTED_TO_BACKEND=false
SHOULD_BUILD=false
USE_REDUX_TOOL=false
USE_LOG_OUTPUT_AS_JSON=false
ACCESS_KEY_ID=
SECRET_ACCESS_KEY=
BUCKET_NAME=
BASE_PATH=
MIX_PANEL_KEY=
RDSTATION_KEY=
RESOURCE_KEY=
SHOULD_ENABLE_INTERCOM=false
SHOULD_ENABLE_GTM=false
SHOULD_ENABLE_UTA=false
SHOULD_ENABLE_WOOTRIC=false
I have debugged my script, and found out that this is the point where sometimes it has a problem
cat .envtemp | awk -v var_value="$VAR_VALUE" \
-v var_name="$VAR_NAME" \
-F '=' '$0 !~ var_name {print $0} $0 ~ var_name {print $1"="var_value}' | tee .envtemp
This piece of code sometimes outputs to .envtemp the proper result, while sometimes it just outputs nothing, making .envtemp empty
The complete code i am using is the following:
function change_value(){
VAR_NAME=$1
VAR_VALUE=$2
cat .envtemp | awk -v var_value="$VAR_VALUE" \
-v var_name="$VAR_NAME" \
-F '=' '$0 !~ var_name {print $0} $0 ~ var_name {print $1"="var_value}' | tee .envtemp
ls -l -a .env*
}
function manage_env(){
for VAR in $#
do
var_name=`echo $VAR | awk -F '=' '{print $1}'`
var_value=`echo $VAR | awk -F '=' '{print $2}'`
change_value $var_name $var_value
done
}
function main(){
manage_env $#
cat .envtemp > .env
exit 0
}
main $#
Here is an example script for recreating the error. It does not happen every time, and when it happens, it is not always with the same input.
#!/bin/bash
ENV_MANAGER_INPUT="NODE_ENV=production BASE_URL=http://qa.arquivei.com.br ASSETS_PATH=https://d4m6agb781hapn.cloudfront.net USE_CDN=true WEBPACK_PORT= IS_CONNECTED_TO_BACKEND=true ACCESS_KEY_ID= SECRET_ACCESS_KEY= BUCKET_NAME=frontend-assets-dev BASE_PATH=qa"
cp .env.dist .env
#Removes comment lines. The script needs a .envtemp file.
cat .env.dist | grep -v '#' | grep -v '^$' > .envtemp
./jenkins_env_manager.sh ${ENV_MANAGER_INPUT}
Have you tried use two files:
mv .envtemp .envtemp.tmp
cat .envtemp.tmp | awk ... | tee .envtemp

Shell - Awk - reading from variable

Is it possible to read for the awk input from variable (e.g. i) instead of file ?
awk 'something' temp
Can the 'temp' be replaced by the variable $i ? Really couldn't find it in man pages.
If your variable is a string, you can do that by using Bash here-string notation.
awk 'something' <<< "$temp"
For example:
$ temp='hello:world'
$ awk -F':' '{print $2}' <<< "$temp"
world
If you variable is a file, then just do
$ cat file
hello:world
$ f='./file'
$ awk -F':' '{print $1}' "$f"
hello
In a POSIX shell without here-string support, you can use a here-doc instead:
awk 'something' <<EOF
$i
EOF
awk 'something' <<< "$i" is just a shortcut for the above when the here document is very short or already contained in a variable.

How to correct this awk code without eval?

The following piece of awk code works fine if I use eval builtin
var='$1,$2'
ps | eval "awk '{print $var}'"
But when I try to knock off eval and use awk variable as substitute then I am not getting the expected result
ps | awk -v v1=$var '{print v1}' # output is $1,$2
ps | awk -v v1=`echo $var` '{print v1}' # output is same as above
ps | awk -v v1=$var '{print $v1}' # output is all the fields of ps command
ps | eval "awk -v v1=$var '{print v1}'" # output is column of comma
How to get the desire output without using eval?
Use double-quotes in the awk command:
ps | awk "{print $var}"

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.

Resources