Skip to Main Content
  • Questions
  • Variable value pass as new line in shell script

Breadcrumb

May 4th

Question and Answer

Connor McDonald

Thanks for the question, SURESH.

Asked: January 27, 2017 - 7:21 am UTC

Last updated: January 28, 2017 - 12:16 am UTC

Version: 11

Viewed 1000+ times

You Asked

Hi TEAM,

I have code as below but parameter value displaying as new line so my code is failing please help me asap .

STEP 1 :

OUTPUT=$( SQLPLUS -S /NOLOG
CONNECT USER/PASSWORD
SELECT ENAME FROM EMP;
EXIT
EOF
)

STEP2 : READING THE PARAMETER IN SHELL SCRIPT AS BELOW
ECHO "/USR/LOCAL/SCRIPTS/$OUTPUT

STEP3: IF I RUN THE SHELL SCRIPT parameter OUTPUT COMING in new line AS BELOW
/USR/LOCAL/SCRIPTS/
CONFIRM_BYTE.sh

Please help me how can we display as below

/USR/LOCAL/SCRIPTS/CONFIRM_BYTE.sh

and Connor said...

I have to admit, I generally dont do it that way because I like more control over the *possible* outputs.

So I'll often so something like:

print "
connect u/p
set pages 0
set feedback off
set ...  [whatever you need]
select ...
sqlplus -s -l /nolog 1>output.log 2>&1

if [ "`grep 'ORA-' output.log`" ] ;then
   email oracle error
fi

if [ "`grep 'SP2-' output.log`" ] ;then
   email sqlplus error
fi


etc etc

Once I've exhausted things I'm checking for, the remainder is the output I need, ie

OUTPUT=`cat output.log`

If its only a subset of the output I'm interested in, I'll just filter by that, eg

OUTPUT=`grep 'SPECIAL_VAR' output.log`

etc

Hope this helps. But for me - the most critical thing is not progressing unless I'm super confident the sqlplus component completed successfully.

Is this answer out of date? If it is, please let us know via a Comment