LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2008, 07:33 AM   #1
TIGA
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Rep: Reputation: 0
Unhappy Bash Shell: Using the value of $A to read the value of $B ?


hi all,

i am having problem getting the value of a variable from a txt file.

please read below and also look in the code: <*????*>

1) addresses.txt: contains "websitenames = website url"
2) test.sh: promots user to input a websitename, if the website name is found in the txt file then it will retrive the the url of that website from the addresses.txt



// addresses.txt

---------------------------------------
google=google.com
yahoo=yahoo.com
linuxQs=linuxquestions.com

---------------------------------------


// test.sh
---------------------------------------
#!/bin/bash

Name=""; #website name
Url=""; #url of the website

function get_Name {
while [ -z $Name ]; do
read -p "Enter the name: " Name;
if [ $(cat addresses.txt | grep $Name) != "" ] ; then
WorkName=$Name;
echo "website name: $Name, Url=*????*; #<< the problem part
else
echo "$Name couldnt be found in the addresses.txt, try again";
Name="";
fi
done
}

get_Name

---------------------------------------
 
Old 04-12-2008, 08:26 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
There are a lot of ways to do this. Here is some example
Code:
url=$(gawk -F= -v name=$Name '$1 ~ name {print $2}' addresses.txt)
url=$(grep ^$Name addresses.txt | cut -f2 -d=)
Just a note on your script: using
Code:
$(cat addresses.txt | grep $Name)
is not necessary, since grep accepts a file as argument. It could be simply
Code:
$(grep $Name addresses.txt)
Also you may consider to use some other language like python (using dictionaries) or perl (using hashes). It would be easier.
 
Old 04-12-2008, 09:20 AM   #3
ktgajowniczek
LQ Newbie
 
Registered: Oct 2007
Location: Warsaw, Poland
Distribution: Debian
Posts: 7

Rep: Reputation: 0
other way:

Code:
#!/bin/bash

NAME=""
URL=""
LINE=""
ADDRFILE="addresses.txt"

while [ -z ${URL} ]; do
  read -p "Enter the name: " NAME
  LINE=$(grep ${NAME} ${ADDRFILE}) && ( 
    URL=$(echo ${LINE} | cut -d= -f2) 
    echo "website name: ${NAME}, URL=${URL}" 
  ) || echo "${NAME} couldn't be found in the addresses.txt, try again" 
done
but you sholuld also be aware, that any part of appropriate NAME will work
for example "o" will work for all of the above, to avoid it you can put:
Code:
LINE=$(grep -E "^${NAME}=" ${ADDRFILE})

Last edited by ktgajowniczek; 04-12-2008 at 09:28 AM.
 
Old 04-12-2008, 09:20 AM   #4
viron
Member
 
Registered: Dec 2005
Location: McKinney, Texas
Distribution: Slackware
Posts: 40

Rep: Reputation: 15
In perl....assuming the script and addresses.txt are in the same directory.

--cut--
#!/usr/bin/perl

use strict;
use warnings;

my $file = "addresses.txt";
my %addresses;

open(FILE, "$file") or die "Could not open $file: $!\n";

foreach (<FILE>) {
chomp;
my ($k, $v) = split(/=/);
$addresses{$k} = $v;
}

sub get_name {

print "Enter the name: ";
my $name = <STDIN>;
chomp($name);

if (exists($addresses{$name})) {
print "Name: $name, Url=$addresses{$name}\n";
}
else {
print "$name could not be found in addresses.txt, try again..\n";
}

get_name();
}


get_name();
--cut--
 
Old 04-12-2008, 12:20 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/bash
while true
do
 read -p "Enter name (q to quit): " name
 case $name in q|Q )break ;;esac
 while IFS="=" read -r a b
 do
    if [ "$a" == "$name" ];then
      echo "$a :$b"
      break
    fi
 done < "file"
done
 
Old 04-24-2008, 04:19 PM   #6
TIGA
LQ Newbie
 
Registered: Apr 2008
Posts: 2

Original Poster
Rep: Reputation: 0
thanks a lot guys and sorry for the late replay.

i tried both shell and perl solutions. and they both worked like a charm..

i didnt expectd frequent responses, this is a great place, i think i will stick around here for some time.
 
Old 04-25-2008, 12:08 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by TIGA View Post
i didnt expectd frequent responses, this is a great place, i think i will stick around here for some time.
He he... welcome to LQ!
 
Old 04-25-2008, 02:00 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
TIGA (OP),
What did you actually try first before posting the code in *????* ?
End
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
bash read value function mcandy Programming 11 02-15-2008 11:47 AM
own shell in place of bash shell somu_thedev Programming 6 07-31-2007 10:13 AM
Run a C Shell using only Bash shell HSN Linux - Software 1 12-25-2006 07:44 AM
BASH Shell program Read a configuration File minil Programming 10 01-17-2005 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration