Sponsored Content
Top Forums Shell Programming and Scripting Variables are not taken in a string Post 302843660 by sheila_ga2013 on Wednesday 14th of August 2013 10:43:03 AM
Old 08-14-2013
Variables are not taken in a string

Here is what I'm trying to do.
A query embedded with a unix variable is stored in the database table. When it is pulled out, it has a variable in it and it needs to be replaced by the variable passed to the script below.

Code:
1. select cast(cmd_string as varchar(1000)) from mytable where tabname='mytable'
Output from the query:
select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
2. Now this query is passed to a variable
lowsql="${abovequery}"
echo lowsql:
 select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
3. Now trying to replace $lowvalue with the actual value:
function runq {
lowvalue=$1
echo lowvalue $lowvalue before
echo "lowsql before $lowsql"
p=`echo "$lowsql"`
echo p: $p
}
runq 201302

4. Output from this function:
./makeq.ksh 201302
lowvalue 201302 before
lowsql before select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur
p: select char(substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,7,4)||substr(date(substr(MTH_BEG_DY_ID,1,4)||'-'||substr(MTH_BEG_DY_ID,5,2)||'-'||substr(MTH_BEG_DY_ID,7,2)) + 1 month,1,2),6) from ECD.DT_MTH_DIM where MTH_DIM_ID=$lowvalue with ur

However, $lowvalue is not being replaced by the actual value 201302.

Anyone has any suggestion or idea why?

Thanks.

Sheila
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

KSH split string into variables

Hello, I am an intermediate scripter. I can usually find and adapt what I need by searching through previous postings, but I'm stumped. I have a string with the format "{Name1 Release1 Type1 Parent1} {Name2 Release2 Type2 Parent2}". It is being passed as an argument into a ksh script. I need to... (5 Replies)
Discussion started by: drd_2b
5 Replies

2. UNIX for Dummies Questions & Answers

comparing 2 string variables in unix

search=Jul 22 date=Jul 22 if then echo They match >>this piece of code does not detect that search and date are equivalent strings.. what's wrong with it? (17 Replies)
Discussion started by: katdaniel16
17 Replies

3. Shell Programming and Scripting

Passing string variables

HI all, Very new to shell programming and just wanted some help on how to solve the following problem. I have a small shell script which searches a given file and extracts some string parameters. I want to now be able to call this script from another shell script and somehow pass the parameters... (11 Replies)
Discussion started by: pxy2d1
11 Replies

4. Shell Programming and Scripting

Compare String Variables for Greater or Less Than?

Is there any way to compare two strings for a greater or less than condition? For example, StringA is "apple" and StringB is "bonnet" and StringC is "captain". Can I only test for equal/not-equal, or is there a way to find out whether StringA is less than StringB, and StringC is greater than... (6 Replies)
Discussion started by: OPTIMUS_prime
6 Replies

5. Shell Programming and Scripting

Comparing String with Number variables

I have two variables and want to perform some functions after comparison $cat file1 14.abcde a=`cut -f 1 -d "." file1 b=15 if then .... fi but i got an error message says that unary operator expected and i think its because of $a is a string and trying to compare with... (3 Replies)
Discussion started by: bonosungho
3 Replies

6. Shell Programming and Scripting

divide a string into variables

i have /tmp/dev/string1/testfile.txt i need only testfile.txt How can get that..can anyone helpme out Thanx (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

7. Shell Programming and Scripting

How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like: aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use var1=`echo $sting1 | cut -c1-c2` but... (9 Replies)
Discussion started by: aquimby
9 Replies

8. Shell Programming and Scripting

string into variables

Hi, i am new to Shell Scripting, i want to get some input from user which is string, and i want to store it into variables. Eg: str='hello how are you' i want to store this string into 4variables lyk s1=hello s2=how s3=are s4=you thanks, lak (11 Replies)
Discussion started by: lakshmikanthe
11 Replies

9. Shell Programming and Scripting

Help with setting variables extracted from a string

Hi guys, I'm using tcsh. I have a string that contains variable names: "var1:var2:var 3", I want to be able to do different operations on the content of those variables: I extract the variable names with: foreach var ( `echo $string | sed 's/:/\n/g'`) now in the variable `var` I have the... (1 Reply)
Discussion started by: gofmarat
1 Replies

10. Shell Programming and Scripting

comparing variables to date as string

I have a file $ cat myfile A 02/16/2012 B 02/19/2012 C 02/20/2012 D 02/17/2012 E 02/16/2012 My simple script > cat myscript.sh mydate="02/16/2012" awk ' ($2~/$mydate/) {print $1}' < myfile but I got no output! and when I try $2~/'$mydate'/ I got: The error context is (2 Replies)
Discussion started by: Sara_84
2 Replies
General::Interpolated(3)				User Contributed Perl Documentation				  General::Interpolated(3)

NAME
Config::General::Interpolated - Parse variables within Config files SYNOPSIS
use Config::General; $conf = new Config::General( -ConfigFile => 'configfile', -InterPolateVars => 1 ); DESCRIPTION
This is an internal module which makes it possible to interpolate Perl style variables in your config file (i.e. $variable or "${variable}"). Normally you don't call it directly. VARIABLES
Variables can be defined everywhere in the config and can be used afterwards as the value of an option. Variables cannot be used as keys or as part of keys. If you define a variable inside a block or a named block then it is only visible within this block or within blocks which are defined inside this block. Well - let's take a look to an example: # sample config which uses variables basedir = /opt/ora user = t_space sys = unix <table intern> instance = INTERN owner = $user # "t_space" logdir = $basedir/log # "/opt/ora/log" sys = macos <procs> misc1 = ${sys}_${instance} # macos_INTERN misc2 = $user # "t_space" </procs> </table> This will result in the following structure: { 'basedir' => '/opt/ora', 'user' => 't_space' 'sys' => 'unix', 'table' => { 'intern' => { 'sys' => 'macos', 'logdir' => '/opt/ora/log', 'instance' => 'INTERN', 'owner' => 't_space', 'procs' => { 'misc1' => 'macos_INTERN', 'misc2' => 't_space' } } } As you can see, the variable sys has been defined twice. Inside the <procs> block a variable ${sys} has been used, which then were interpolated into the value of sys defined inside the <table> block, not the sys variable one level above. If sys were not defined inside the <table> block then the "global" variable sys would have been used instead with the value of "unix". Variables inside double quotes will be interpolated, but variables inside single quotes will not interpolated. This is the same behavior as you know of Perl itself. In addition you can surround variable names with curly braces to avoid misinterpretation by the parser. SEE ALSO
Config::General AUTHORS
Thomas Linden <tlinden |AT| cpan.org> Autrijus Tang <autrijus@autrijus.org> Wei-Hon Chen <plasmaball@pchome.com.tw> COPYRIGHT
Copyright 2001 by Wei-Hon Chen <plasmaball@pchome.com.tw>. Copyright 2002-2010 by Thomas Linden <tlinden |AT| cpan.org>. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See <http://www.perl.com/perl/misc/Artistic.html> VERSION
2.13 perl v5.12.1 2010-04-09 General::Interpolated(3)
All times are GMT -4. The time now is 01:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy