LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-31-2010, 07:42 PM   #1
GamerX
Member
 
Registered: Nov 2006
Location: Vancouver, BC
Distribution: Slackware
Posts: 102

Rep: Reputation: Disabled
Question bash: substitute parameter in a quoted string, stored in another variable


Tired searching for this but I have no idea what this is called so bear with me please..
(variable substitution?)
(parameter expansion?)

Code:
run_repeatedly()
{
    NUM=0
    while [ <irrelevant stuff here> ]
    do
        NUM=$((NUM+1))
        echo "$1"
        eval "$1"
    done
}

run_repeatedly "programX -o \"./messy/path/output-\$NUM.txt\""
The echo inside the loop prints "...-$NUM.txt"; obviously I'm aiming to have bash substitute the iteration number so that I end up with many output files not 1.

Help?
 
Old 01-31-2010, 07:52 PM   #2
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
remove the \ in front of "$" your escaping the $ which kills the vairable.
 
Old 01-31-2010, 07:57 PM   #3
GamerX
Member
 
Registered: Nov 2006
Location: Vancouver, BC
Distribution: Slackware
Posts: 102

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by trey85stang View Post
remove the \ in front of "$" your escaping the $ which kills the vairable.
But then $NUM is evaluated when I'm calling the function and it's not defined at that time. Essentially I'm trying to pass a format string here..

EDIT: Speaking of format strings ---> printf to the rescue!
Code:
run_repeatedly()
{
    NUM=0
    while [ <irrelevant stuff here> ]
    do
        NUM=$((NUM+1))
        echo "`printf "$1" $NUM`"
        eval "`printf "$1" $NUM`"
    done
}

run_repeatedly "programX -o \"./messy/path/output-%d.txt\""

Last edited by GamerX; 01-31-2010 at 08:07 PM. Reason: solved own problem
 
Old 01-31-2010, 09:26 PM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by GamerX View Post
But then $NUM is evaluated when I'm calling the function and it's not defined at that time. Essentially I'm trying to pass a format string here..

EDIT: Speaking of format strings ---> printf to the rescue!
Code:
run_repeatedly()
{
    NUM=0
    while [ <irrelevant stuff here> ]
    do
        NUM=$((NUM+1))
        echo "`printf "$1" $NUM`"
        eval "`printf "$1" $NUM`"
    done
}

run_repeatedly "programX -o \"./messy/path/output-%d.txt\""
NUM is defined first thing in your function. Your first argument is: "programX -o \"./messy/path/output-$NUM.txt\"" then NUM is defined in the function before the echo is printed... Anyways.. printf works for you, but remiving the backslask from the $ should be working as well... at least with bash.
 
Old 01-31-2010, 09:41 PM   #5
GamerX
Member
 
Registered: Nov 2006
Location: Vancouver, BC
Distribution: Slackware
Posts: 102

Original Poster
Rep: Reputation: Disabled
Without the backslash the value of $NUM will be evaluated immediately when the program call is made; NUM is not defined there because its declaration is in a lower scope.

Code:
VAR1="fffff"
VAR2="zzzz$VAR1"
echo $VAR2
VAR1="dddddd"
echo $VAR2
will print
Code:
zzzzfffff
zzzzfffff
not
Code:
zzzzfffff
zzzzdddddd
 
Old 01-31-2010, 09:48 PM   #6
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
you're right gamerx, my apologies. I didnt try your code exactly (or my suggestion)... until just now. Personally.. Id just move NUM outside the function to ensure it is decaled and call it good.... but the point of scripting is doing it your way

Thanks,
Trey
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 04:07 PM
Bash variable problem: cURL -b parameter (string form e.g. "name=value;n2=v2") sithemac Other *NIX 3 07-09-2008 07:15 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 10:43 AM
Bash: how do I pass a double quoted string to a command? Nylex Programming 8 04-18-2007 10:36 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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