Link to home
Start Free TrialLog in
Avatar of joaotelles
joaotellesFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial