I'm writing a shell script. I want to put the variables in a separate config files and use those inside my script.
e.g. the config file (temp.conf)will have the values like
mapping=123
file_name=xyz.txt
I want to access these variables in temp.conf(i.e. mapping and file_name) from inside the shell script.
The purpose of the shell script is to check the value of the variable "mapping" .
If mapping=123 then it'll create a file with name equivalent to the value assigned to the
variable file_name (i.e. xyz.txt in this case)
I'm new to Unix Shell scripting.
Could you help me solving this problem?
hope below can help you some
while read line;do
eval $line
done < a
echo $mapping
echo $file_name
if [ $1 -eq $mapping ];then
touch $file_name
else
echo "mapping code is incorrect"
fi
The way to do this is...
temp.conf should look like this...
export mapping=123
export file_name=xyz.txt
now in the script you can use these variable once you execute the conf file...
. temp.conf
echo $mapping
echo $file_name
awk -F"=" '$1=="mapping" && $2=="123"{
getline
filename = $2
print filename
}' file
Hi,
Just explaining the requirement more elaborately:
data in temp.conf :
----------
a=10
b=7
c=8
My requirement:
----------------
check the temp.conf file using awk
find the value of "a" and assign the value to variable "ram" i.e. ram=value of a i.e.10
find the value of "b" and assign it to the variable "shaym" i.e. shyam=value of b i.e.7
find the value of "c" and assign it to the variable "hari" i.e. hari=value of c i.e.8
Could anyone please post the code snippet?
ram=`grep 'a=' temp.conf | awk -F"=" '{print $2}'`
shyam=`grep 'b=' temp.conf | awk -F"=" '{print $2}'`
hari=`grep 'c=' temp.conf | awk -F"=" '{print $2}'`
no need that many tools. use just awk
set -- $(awk -F"=" '$1=="a"{ram=$2}
$1=="b"{shaym=$2}
$1=="c"{hari=$2}
END{
print ram,shaym,hari
}' file)
echo $1
echo $2
echo $3
with bash
while IFS="=" read -r a b
do
case $a in
"a") ram=$b;;
"b") shyam=$b;;
"c") hari=$b;;
esac
done < file
echo $ram,$shyam,$hari