Escaping a variable in ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Escaping a variable in ksh
# 1  
Old 03-20-2019
Escaping a variable in ksh

I am trying to echo a variable exactly to a script-

Code:
echo "${var1} ${var2} >> output.output

Gives me a blank file.

I would like output.output to basically say:

Code:
${var1} ${var2}

I think I need to use a special escape character for variables. Am I right in assuming that, and is it the best approach?
# 2  
Old 03-20-2019
Quote:
Originally Posted by jeffs42885
I am trying to echo a variable exactly to a script-

Code:
echo "${var1} ${var2} >> output.output

Gives me a blank file.

I would like output.output to basically say:

Code:
${var1} ${var2}

I think I need to use a special escape character for variables. Am I right in assuming that, and is it the best approach?
couple of problems:
  1. you're missing a closing double-quote for your echo to work
  2. if you want a literal string ${var1} ${var2}, use SINGLE-quotes for the echo (instead of DOUBLE-QUOTES)

Last edited by vgersh99; 03-20-2019 at 01:10 PM..
# 3  
Old 03-20-2019
Code:
echo "\${var1} \${var2}"

Ooops just saw your post vgersh99...
This User Gave Thanks to vbe For This Post:
# 4  
Old 03-20-2019
Quote:
Originally Posted by vbe
Code:
echo "\${var1} \${var2}"

Ooops just saw your post vgersh99...
This worked great. One concern.

I am trying to echo a query that gets passed to my command:

Heres what I am getting

Code:
-i "where doc_name like \'%\'

Heres what I need

Code:
-i "where doc_name like '${variable}%'


Last edited by RudiC; 03-20-2019 at 03:41 PM..
# 5  
Old 03-20-2019
Please be VERY careful with your quotes (single / double)! You seem to be missing one again in above!



Do I get you right that you want '${variable}' literally written to your file, but then expanded when executing that file? Might run into trouble with all those single quotes in place. Please post a sample of the desired output file (and its possible execution log).
# 6  
Old 03-20-2019
Quote:
Originally Posted by RudiC
Do I get you right that you want '${variable}' literally written to your file, but then expanded when executing that file? Might run into trouble with all those single quotes in place. Please post a sample of the desired output file (and its possible execution log).
Nope.

Here's what I am running-

Code:
while IFS=, read agname tablename
do
echo "while read IFS=, read docname agname" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "do" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "get -G \${agname} -i \"where doc_name like \'${docname}%\'" -o \${agname}" >> $BASEDIR/scripts/${agname}_retrieval.ksh
echo "done" < $BASEDIR/data/${agname}/${agname}_docnames.del" >> $BASEDIR/scripts/${agname}_retrieval.ksh
done < $BASEDIR/output/$STEP1_OUTPUT

The final script is generating but..

Code:
while read IFS=, read docname agname
do
get -acgNv -G ${agname} -i "where doc_name like \'%\' -o ${agname} >> /tmp/final/scripts/AG_retrieval.ksh
echo done < /tmp/final/data/AG/AG_docnames.del

Not sure why it's putting extra echos in the output

Desired-

Code:
while read IFS=, read docname agname
do
get -acgNv -G ${agname} -i "where doc_name like '${docname}%' -o ${agname}
 done < /tmp/final/data/AG/AG_docnames.del




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-20-2019 at 03:56 PM.. Reason: Added/Changed CODE tags.
# 7  
Old 03-20-2019
See post #5, line 1.



EDIT: How far would this get you (zeroth approximation):
Code:
while IFS=, read agname tablename
  do    exec  > $BASEDIR/${agname}_retrieval.ksh
        echo "while read IFS=, read docname agname"
        echo "do"
        echo "get -G \${agname} -i \"where doc_name like '\${docname}%'\" -o \${agname}"
        echo "done < $BASEDIR/data/${agname}/${agname}_docnames.del"
        exec 1>&2
   done < $BASEDIR/output/$STEP1_OUTPUT


EDIT: or (first approximation)



Code:
while IFS=, read agname tablename
  do    cat <<-EOTXT  > $BASEDIR/${agname}_retrieval.ksh
        while read IFS=, read docname agname
        do
        get -G \${agname} -i "where doc_name like '\${docname}%'" -o \${agname}
        done < $BASEDIR/data/${agname}/${agname}_docnames.del
        EOTXT
      done < $BASEDIR/output/$STEP1_OUTPUT


Last edited by RudiC; 03-20-2019 at 05:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

2. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

3. Shell Programming and Scripting

setting variable value to dynamic sed match - escaping hell

Hello All, I'm trying to write a script that will perform a dynamic match (of a dynamic variable) and set a variable to have the resulting (match) value. The idea is that the environment variable to check ($1) and the regular expression to use ($2) are given as parameters. For example,... (5 Replies)
Discussion started by: aedgar
5 Replies

4. UNIX for Dummies Questions & Answers

Question on using a variable in KSH

Hi all, The below command tries to copy ".tgz" instead of "hello_test.tgz" -- It seems as if the underscore gets in the way. I tried with different ways of using quotes, with no luck, unfortunately...it's probably very simple, but may I ask how this would be done: How would the below be... (3 Replies)
Discussion started by: chatguy
3 Replies

5. Shell Programming and Scripting

checking variable has value - ksh

hi all, i have a shell (ksh) script where i am reading line by line from a grep command and i wanted to check if the grep command actually returned something or was null. i can do this by using -z : if ; then ..... but this forces me to do something when $myVariable is null when i... (3 Replies)
Discussion started by: cesarNZ
3 Replies

6. Shell Programming and Scripting

Escaping the * character in ksh.

Hi All, In ksh script i'm trying to assign "sqlstmt1" varaible value, update VAREntryTb set VAR10num = VAR1num * Mltplr where BusD = '$val1' and RunI = 1"` Hence i wrote below statement, the issue with this is shell is expanding "*" character adn thus subistuting it with the content of my... (6 Replies)
Discussion started by: arvindcgi
6 Replies

7. Shell Programming and Scripting

problem escaping newline in ksh

Hi, I did the below. $ print "\\n" $ I am curious, why does \\n give two new lines? I would have thought that the first \ would escape the second \, and so we'd get \n printed. But we didn't. Any ideas? Thanks. (7 Replies)
Discussion started by: JamesByars
7 Replies

8. Shell Programming and Scripting

KSH variable substitution

Hi folks Please let me know if anyone knows how to handle this. My KSH script -> testscript.ksh cmd=$1 ENV="devl" echo $cmd This is how I call the script ./testscript.ksh 'ps -ef | grep br$ENV' How do I get this to print the below text i.e $ENV should be substituted with the value... (5 Replies)
Discussion started by: tipsy
5 Replies

9. Shell Programming and Scripting

ksh - get stdout name as variable

Writing a ksh script. If someone starts a process with: test.ksh > date.log How can I grab 'date.log' name as a variable in test.ksh? I need to get the 'date.log' name (not the contents) as a variable...without entering something like 'test.ksh date.log > date.log' (4 Replies)
Discussion started by: mhcueball2
4 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question