How do I preserve spaces in a KornShell script when using backlinks?

I am working on a script in KornShell (ksh). The connection to my database is through SQLPLUS to the Oracle 9i database. I didn't have any problem getting DB values ​​in shell variables, EXCEPT, that any consecutive spaces are automatically truncated by only one character.

Here's an example of my code:

MY_VAR=`sqlplus -s usr/pass@db << !
set heading off;
set pagesize 0;

select a_value from a_table where an_index = 25;
!`

      

The actual data in the database is as follows:

Dec 15 09:19:10 <24:0070> User record (5    XATY       41839FG8   58775HK9AFF) is invalid for this condition

      

My example code above returns it like this:

Dec 15 09:19:10 <24:0070> User record (5 XATY 41839FG8 58775HK9AFF) is invalid for this condition

      

Spacing is critical to what I work with.

Thank you in advance for your help.

+3


source to share


1 answer


First of all, don't use backlinks, they are deprecated in "The New Kornshell Programming Language" published in 1995! Instead, use the simplified command substitution version $( yourCmd goes here)

.

You are not showing us how you use the $ MY_VAR variable. If you say

echo $MY_VAR > outputFile

      

You need to specify a variable i.e.

print -- "$MY_VAR" > outputFile

      

Print

is ksh major-upgrade on echo, but it's better to get in the habit of using '-' between the command and the lines you want Print

. Any '-' char in the string that you want to print can issue a noguarded (i.e. No '-') command Print

to loop.



You may also need to specify the assignment MY_VAR, i.e.

MY_VAR="$(sqlplus -s usr/pass@db <<-EOD
set heading off;
set pagesize 0;

select a_value from a_table where an_index = 25;
EOD
)"

      

I switched to EOD like! char is used by many shells for type notation! $ (last word of previous line) so why confuse things.

Note to the future: There is a tag for ksh that you can use here on StackOverflow. Shell is ambiguous and can mean Windows cmd.com as well as zsh.

Hope this helps.

+5


source







All Articles