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 02-15-2004, 05:46 PM   #1
magjo813
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Rep: Reputation: 0
Bash script; command and args in variable.


Hi!

I'd like to do something like this;

url[1]=the.url.to.use
filter[1]='grep -v "What to filter out"'
.
.
.
while [ -n "${url[$a]}" ]
do
if [ -n "${filter[$a]}" ]
then
wget -t1 -q -O - ${url[$a]} | ${filter[$a]} > /tmp/thiswebcheck
.
.
.

Can I have both command and arguments in the same variable (filter-array) this way and somehow make it execute? I've tried alias but as it's in a loop that don't work. I've been using fgrep on the wget-line and just what to filter out in the filter-array, but I'd like to use different commands for new urls...
 
Old 02-16-2004, 01:43 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
I'm not quite sure what you are going for, so this might not be helpful, but depending
upon what it you're tying to do, the distinction between the use of ">" and ">>", and
where you use them, might be meaningful as follows:

$ for l in a b c d e f g
> do
> echo $l > this
> done
$ cat this
g
$ for l in a b c d e f g
> do
> echo $l
> done > this
$ cat this
a
b
c
d
e
f
g
$ rm this; for l in a b c d e f g
> do
> echo $l >> this
> done
$ cat this
a
b
c
d
e
f
g

Then too, I don't have any problems doing this in bash:

$ variable_command[1]='echo "a"'
$ variable_command[2]='echo "b"'
$ for fc in 1 2; do ${variable_command[$fc]}; done
"a"
"b"

Sometimes, in any almost any shell, the processing can get confused, or due to a
combination of factors, the result is not exactly what you'd expect if you simply
executed the command from the command line. As in the above, if you just
manually executed:

echo "a"

you would not expect the output to contain the double quotes. But used from
the variable it does. Sometimes the "eval" command can help, as in:

$ for fc in 1 2; do eval ${variable_command[$fc]}; done
a
b

the output is as you'd expect if you just executed the commands from the command
line.

Hope this helps. If not, maybe you can give more details about what you're
trying to do.
 
Old 02-16-2004, 09:22 AM   #3
magjo813
LQ Newbie
 
Registered: Feb 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Ok, I could've been more elaborate. I was having problems with the double quotes, either the whole variable was considered a command and thus not found or the string in double quotes was parsed/divided strangely. But adding eval seems to have solved it so I'm quite happy now. Thanks!
 
  


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
processing command args in bash slzckboy Programming 16 10-18-2017 03:12 AM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

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

All times are GMT -5. The time now is 04:03 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