LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-14-2011, 12:48 PM   #1
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Rep: Reputation: 27
using variable in a command [SOLVED}


i'm trying to execute a shell script, i'm trying to use the values in an array for use in a sed command:
sed -n '/Sales ID: ${array[$i]}/,/Totals:/p'
that command creates empty files. so my guess is that its not recognizing the array as an array but as text?
how would i be able to utilize the array in the command?
thanks

i got it, didnt think that if i doubled up the single quotes that it would work, but this worked:

sed -n '/Sales ID: '${array[$i]'}/,/Totals:/p'

Last edited by Eppo; 04-14-2011 at 03:48 PM.
 
Old 04-14-2011, 01:54 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Q: Are you sure this is quoted correctly?
Code:
sed -n '/Sales ID: '${array[$i]'}/,/Totals:/p'
Anyway, you might also wish to try double-quotes. For example:
Code:
sed -n "/Sales ID: ${array[$i]}/,/Totals:/p"
 
Old 04-14-2011, 01:54 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Eppo View Post
i got it, didnt think that if i doubled up the single quotes that it would work, but this worked:

sed -n '/Sales ID: '${array[$i]'}/,/Totals:/p'
Of course it would work: The quote ends the text, then you get the value of the array, then the next quote starts the text again. In bash, when strings are stuck together with no spaces in between, they are combined into one. (I hope that the fact that the right curly brace is after the quote mark is a mistake)

Anyway, mark the thread as solved.
 
Old 04-14-2011, 03:31 PM   #4
Eppo
Member
 
Registered: Feb 2007
Location: NY
Distribution: Arch, Ubuntu
Posts: 77

Original Poster
Rep: Reputation: 27
i didn't think that it would work only because the single quotes are used in the command, so if i broke it up it would think it was the next parameter for the command and error out. but it does work.... you learn something new every day.
 
Old 04-14-2011, 03:38 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Mark the thread as solved.
 
  


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
command in variable thsuk1 Linux - General 2 07-22-2010 12:25 PM
[SOLVED] Can't use variable in mv command john206 Linux - General 13 05-30-2010 06:08 PM
cd command with variable pkacker Programming 8 06-06-2009 02:40 PM
command out to a variable zebrafx Programming 4 02-20-2009 09:12 AM
Command & Variable karunesh Programming 5 04-08-2005 06:54 PM

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

All times are GMT -5. The time now is 08:14 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
Open Source Consulting | Domain Registration