LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-28-2011, 12:26 PM   #1
sscn
LQ Newbie
 
Registered: Jan 2006
Location: China
Distribution: Fedora
Posts: 28

Rep: Reputation: 0
Smile How to make cascading 2 variables valid?


Hi,

I want to make below define valid in my script.

F1="123456"
F2="abcdef"

for (( i = 1; 1 < 3; i++ ))
do
echo "Search $F{$i} string ......"
grep -r $F{$i} /etc/
done

But I cannot get the expected result $F1 and $F2 in 1st and 2nd time.

How I can make it?

Thx,

sscn
 
Old 12-28-2011, 12:52 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
It would be easier to use arrays.
Code:
F=("123456" "abcdef")
for ((i = 0; i<2; i++)); do
   echo "Searching ${F[$i]} string ..."
   grep "${F[$i]}" /etc/*
done
However doing it your way, you need to use `eval' and escape the `$' sign.
eval grep -r \$F$i /etc/

To see how the arguments are evaluated, and passed on to a command, replace the command with `set' and then echo $*.
set grep -r \$F$i /etc/
echo $*
grep -r $F1 /etc/

Last edited by jschiwal; 12-28-2011 at 01:15 PM.
 
Old 12-28-2011, 01:40 PM   #3
sscn
LQ Newbie
 
Registered: Jan 2006
Location: China
Distribution: Fedora
Posts: 28

Original Poster
Rep: Reputation: 0
Great!

Hi jschiwal,

So nice to get it work. I like array method.

For eval, I updated my script as below but it couldn't work as array method did.

for (( i = 1; 1 < 3; i++ ))
do

echo "Search \$F$i string ......"
eval grep -r \$F$i /etc/

done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to make permanent changes to environment variables? cgchayan Programming 8 09-30-2009 02:57 PM
GNU Make - Variables as Targets Matthew Applegate Programming 11 08-09-2009 08:28 AM
How to make 2 variables from one variable value in awk intikhabalam Linux - General 1 07-30-2008 04:32 AM
How to make script cmd valid as execut it directly? cdcshu Linux - Software 5 06-05-2007 05:58 AM
How can you make valid mdk9.2 rpms from the source? kvtournh Mandriva 1 11-18-2003 11:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:56 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration