Shell Scripting - Pass existing variable to a sudo command

Hi,

Im having a problem to pass a variable value to a command that is run inside a sudo command...:

cnt=1
while [ "$cnt" -le "$string2" ]
do
query1=$(sudo -H -u dpa bash -c 'cd; . .profile; sqlplus -s dpownera_1a/dpownera@DP9A <<END
    set pagesize 0 feedback off verify off heading off echo off linesize 30000;
        select to_char(smwhen,'YYYYMMDDHH24') as "DateHour",
        count( case when smstatus = 20002 and smsel = \${cnt} then 1 else null end ) as '\''SDelivered'\'',
        count( case when smstatus = 20001 and smsel = \${cnt} then 1 else null end ) as '\''SOnSMSC'\'',
        count( case when smstatus = 20000 and smsel = \${cnt} then 1 else null end ) as '\''SRejected'\'',
        count( case when smstatus = 20001 and smsel = \${cnt} then 1 else null end ) as '\''SExternExpire'\'',
        count( case when smstatus = 20009 and smsel = \${cnt} then 1 else null end ) as '\''SCancel'\'',
        count( case when smstatus = 20000 and smsel = \${cnt} then 1 else null end ) as '\''SInternalExpire'\'',
        count( case when smstatus != 20001 and smsel = \${cnt} then 1 else null end ) as '\''STotal'\''
        from ts_shore
        group by to_char(smwhen,'YYYYMMDDHH24')
        order by 1;
exit;
END
')
cnt=`expr $cnt + 1`


I have the cnt variable which will "control the loop and I need this variable for the query that I run inside the sudo but I could not find a way to pass it to it...

When I run it I get this:
+ query1='      count( case when smstatus = 200000022 and smsel = ${cnt} then 1 else null end ) as '\''SDelivered'\'',
                                                                  *
ERROR at line 2:
ORA-00911: invalid character'

===

Maybe I could declare a variable $query outside the sudo command but then I would fall into the same problem.. sudo does not recognize "outside" declared variables...

So, any suggestion to outcome this "issue"?

Tks,
Joao
joaotellesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
Try

cnt=1
while [ "$cnt" -le "$string2" ]
do
query1=$(sudo -H -u dpa bash -c 'cd; . .profile; sqlplus -s dpownera_1a/dpownera@DP9A <<END
    set pagesize 0 feedback off verify off heading off echo off linesize 30000;
        select to_char(smwhen,'YYYYMMDDHH24') as "DateHour",
        count( case when smstatus = 20002 and smsel = '${cnt}' then 1 else null end ) as '\''SDelivered'\'',
        count( case when smstatus = 20001 and smsel = '${cnt}' then 1 else null end ) as '\''SOnSMSC'\'',
        count( case when smstatus = 20000 and smsel = '${cnt}' then 1 else null end ) as '\''SRejected'\'',
        count( case when smstatus = 20001 and smsel = '${cnt}' then 1 else null end ) as '\''SExternExpire'\'',
        count( case when smstatus = 20009 and smsel = '${cnt}' then 1 else null end ) as '\''SCancel'\'',
        count( case when smstatus = 20000 and smsel = '${cnt}' then 1 else null end ) as '\''SInternalExpire'\'',
        count( case when smstatus != 20001 and smsel = '${cnt}' then 1 else null end ) as '\''STotal'\''
        from ts_shore
        group by to_char(smwhen,'YYYYMMDDHH24')
        order by 1;
exit;
END
')
(( cnt++ ))

means: Take $cnt out of the surrounding single quotes.


The "query1" variable will be overwritten at each iteration. I assume you didn't post the full code, and you're working with "query1" inside the loop?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.