Read from a file in unix(Continue from previous thread)

Hi This is continuation of previos thread
[test]
status=running
username=abc
password=123456
server=linux

The script was made which is used to capture the data from file
./scr test status
It will give result running

I have a case like
[test]
status = running
username=abc
password=123456
server=linux

./scr test status
It Fails in such contratst when any space comes in between

Assuming you need to check spaces present only in which you are passing argument.
Is this what you want..?

file_name="$1"
var="$2"

if [[ $(awk -v VM="$var" 'VM && NF > 1' $file_name) ]]
then
echo "Fail"
else
echo "Running"
fi

OR

file_name="$1"
var="$2"

if [[ $(grep $var $file_name | grep " " -c) -gt 1 ]]
then
echo "Fail"
else
echo "Running"
fi

No its not like that
I will pass two values and the script will give the result .
Suppose the file is like

[test]
status=running
username=abc
password=123456
server=linux

[live]
status=running
username=xyz
password=09876
server=unix

now suppose if I write

./script live password

it will give the result 09876
If i write

./script live server

it will give the result unix

file_name="$1"
var="$2"

awk -F "=" -v VM="$var" '{if($1 == VM){print $2}}' $file_name

The script fails
when i write ./script test username
It gives output as
abc
xyz
But It should give only output as abc

Please post the content of test file.

I have posted up

If there is no xyz present in test file how it can print that...????

assuming you have only single file

 file_name="$1" 
var="$2"

awk -F "=" -v VM="$var" -v FN="$file_name" '{if($1 ~ FN){s=1}
if(s && $1 == VM){print $2;s=""}}' file

Now my Question is If suppose the file is like

[test]
status=running
username=abc
password=123456
server =  linux

[live]
status=running
username=xyz
password=09876
server=unix

The script fails here when I write ./script test server

---------- Post updated at 02:58 PM ---------- Previous update was at 02:54 PM ----------

If there is space in between server = linux the scripts fails, But the script should give the result and the last case is If i have commented the portion
for example #server=linux
then the script should consider this one

Hi Guys..

Am trying to use mail command to send mail.. But its not working in our LAN. I cant able to send any mail to other localhost.. But i can send and receive mail in my own machine.. Help me ASAP!!:wall:

This is not the right thread to post your queries. Please create the new one

Where i can post.. I can not able to do that.. Can you post the Link for the new post??:wall:

@Adhi, click on:

UNIX for Dummies Questions & Answers - The UNIX and Linux Forums

Then click the big button that says:

2 Likes

Try this..

file_name="$1" 
var="$2"

awk -F "[= ]" -v VM="$var" -v FN="$file_name" '$0 ~ FN{s=1}
s && $1 ~ VM {s="";if(NF==2){print $NF}}' file

Pls check this suggestion and come back with results:

SECT="$1"
ENTRY="$2"
awk     'BEGIN{FS="[= ]*"}                              # set field separator to any combination of "="s and " "s
         /\[.*\]/ {gsub(/[][]/,""); SECTION=$0}         # find the respective section between "[" and "]"
         $1 == EN && SECTION==SC {print $NF}            # if entry found in the respective section, output it
        '  EN="$ENTRY" SC="$SECT" file                  # make shell vars to awk vars and use "file" for input