0

I am trying to select attribute values within multiple occurrences of same tag using xmlstarlet within a for loop ,but the $i in the iteration is not taking value as expected , I am referencing it as "$i" with escape characters within the -m ' ' , but it does not seem to be interpreting it correctly , not sure how I should approach this problem :

for i in {1..16};
do
FTRSERVER=`xmlstarlet sel -t -m '//JOB[\"$i\"]' -v './AUTOEDIT2[@NAME="%%FTP-RHOST"]/@VALUE' ~testhwa/FTPJobstable007.xml`
done

Sample Input XML , Multiple occurrences of JOB tag :

    <JOB
     ACTIVE_TILL="20141124" 
     APPLICATION="CTDS0320" 
     APPL_FORM="AFT" 
     APPL_TYPE="FILE_TRANS" 
     APPL_VER="6.1.01" 
     APR="1" 
     AUG="1" 
     AUTHOR="wyy" 
     AUTOARCH="0" 
     CHANGE_DATE="20190403" 
     CHANGE_TIME="141554" 
     CHANGE_USERID="ecsadmin" 
     CM_VER="610" 
     CONFIRM="0" 
     CREATION_DATE="20190328" 
     CREATION_TIME="105555" 
     CREATION_USER="singhm7" 
     CRITICAL="0" 
     CYCLIC="0" 
     CYCLIC_TOLERANCE="0" 
     CYCLIC_TYPE="Interval" 
     DAYS_AND_OR="OR" 
     DEC="1" 
     DESCRIPTION="Processes Source data files FTP from Source System" 
     DOCLIB="/prod/appl/ctds/Doc" 
     DOCMEM="energent_billing_delta.doc" 
     FEB="1" 
     GROUP="@CTDS-BW" 
     IND_CYCLIC="START" 
     INTERVAL="00000M" 
     JAN="1" 
     JOBNAME="energent_billing_delta-FTP" 
     JUL="1" 
     JUN="1" 
     MAR="1" 
     MAXDAYS="0" 
     MAXRERUN="0" 
     MAXRUNS="0" 
     MAXWAIT="7" 
     MAY="1" 
     MEMLIB="Not in use for application jobs" 
     MEMNAME="energent_billing_delta-FTP" 
     MULTY_AGENT="N" 
     NODEID="vprv0320" 
     NOV="1" 
     OCT="1" 
     OWNER="ctds_energent" 
     PARENT_TABLE="@CTDS" 
     PRIORITY="AA" 
     RETRO="0" 
     RULE_BASED_CALENDAR_RELATIONSHIP="OR" 
     SEP="1" 
     SHIFT="IGNOREJOB" 
     SHIFTNUM="+00" 
     SYSDB="0" 
     TASKTYPE="Job" 
     USE_INSTREAM_JCL="N" 
     WEEKSCAL="BCD-Y">
      <INCOND AND_OR="AND" NAME="CTDS0320-ENERGENT-BILLING-DELTA-OK" ODATE="ODAT"/>
      <OUTCOND NAME="CTDS0320-ENERGENT-BILLING-DELTA-FTP" ODATE="ODAT" SIGN="ADD"/>
      <AUTOEDIT2 NAME="%%FTP-ACCOUNT" VALUE="ctds_energent"/>
      <AUTOEDIT2 NAME="%%FTP-LOSTYPE" VALUE="Unix"/>
      <AUTOEDIT2 NAME="%%FTP-LUSER" VALUE="ctds"/>
      <AUTOEDIT2 NAME="%%FTP-ROSTYPE" VALUE="Windows"/>
      <AUTOEDIT2 NAME="%%FTP-RUSER" VALUE="controlm@enbridge.com"/>
      <AUTOEDIT2 NAME="%%FTP-LPATH1" VALUE="/prod/appl/ctds/Publish/ENERGENT/*"/>
      <AUTOEDIT2 NAME="%%FTP-RPATH1" VALUE="/\Business Intelligence-Gas Consumption\Energent\Outgoing\Gas Consumption\"/>
      <AUTOEDIT2 NAME="%%FTP-PATH" VALUE="Not in use for application jobs"/>
      <AUTOEDIT2 NAME="%%FTP-USE_DEF_NUMRETRIES" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-RPF" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CLEAR_ALL" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE2" VALUE="FTP"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE1" VALUE="LOCAL"/>
      <AUTOEDIT2 NAME="%%FTP-LHOST" VALUE="Local"/>
      <AUTOEDIT2 NAME="%%FTP-RHOST" VALUE="hdq-nt164"/>
      <AUTOEDIT2 NAME="%%FTP-LPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-RPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD5" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRANSFER_NUM" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE1" VALUE="A"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE2" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE3" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE4" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE5" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UNIQUE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM5" VALUE="1"/>
    </JOB>
    <JOB
    ACTIVE_TILL="20141124" 
     APPLICATION="CTDS-VPRV0342" 
     APPL_FORM="AFT" 
     APPL_TYPE="FILE_TRANS" 
     APPL_VER="6.1.01" 
     APR="1" 
     AUG="1" 
     AUTHOR="wyy" 
     AUTOARCH="0" 
     CHANGE_DATE="20190716" 
     CHANGE_TIME="233201" 
     CHANGE_USERID="pahwar" 
     CM_VER="610" 
     CONFIRM="0" 
     CREATION_DATE="20190611" 
     CREATION_TIME="072632" 
     CREATION_USER="pahwar" 
     CRITICAL="0" 
     CYCLIC="0" 
     CYCLIC_TOLERANCE="0" 
     CYCLIC_TYPE="Interval" 
     DAYS_AND_OR="OR" 
     DEC="1" 
     DESCRIPTION="Processes Source data files FTP from Source System" 
     DOCLIB="/prod/appl/ctds/Doc/" 
     DOCMEM="energent_billing_delta.doc" 
     FEB="1" 
     GROUP="@CTDS-BW" 
     IND_CYCLIC="START" 
     INTERVAL="00000M" 
     JAN="1" 
     JOBNAME="energent_billing_delta-FTP" 
     JUL="1" 
     JUN="1" 
     MAR="1" 
     MAXDAYS="0" 
     MAXRERUN="0" 
     MAXRUNS="0" 
     MAXWAIT="7" 
     MAY="1" 
     MEMLIB="Not in use for application jobs" 
     MEMNAME="energent_billing_delta-FTP" 
     MULTY_AGENT="N" 
     NODEID="vprv0342" 
     NOV="1" 
     OCT="1" 
     OWNER="ctds_energent" 
     PARENT_TABLE="@CTDS-VPRV0342" 
     PRIORITY="AA" 
     RETRO="0" 
     RULE_BASED_CALENDAR_RELATIONSHIP="OR" 
     SEP="1" 
     SHIFT="IGNOREJOB" 
     SHIFTNUM="+00" 
     SYSDB="0" 
     TASKTYPE="Job" 
     USE_INSTREAM_JCL="N" 
     WEEKSCAL="BCD-Y">
      <INCOND AND_OR="AND" NAME="V0342-ENERGENT-BILLING-DELTA-OK" ODATE="ODAT"/>
      <OUTCOND NAME="V0342-ENERGENT-BILLING-DELTA-FTP" ODATE="ODAT" SIGN="ADD"/>
      <AUTOEDIT2 NAME="%%FTP-ACCOUNT" VALUE="ctds_energent"/>
      <AUTOEDIT2 NAME="%%FTP-LOSTYPE" VALUE="Unix"/>
      <AUTOEDIT2 NAME="%%FTP-LUSER" VALUE="ctds"/>
      <AUTOEDIT2 NAME="%%FTP-ROSTYPE" VALUE="Windows"/>
      <AUTOEDIT2 NAME="%%FTP-RUSER" VALUE="controlm@enbridge.com"/>
      <AUTOEDIT2 NAME="%%FTP-LPATH1" VALUE="/prod/appl/ctds/Publish/ENERGENT/*"/>
      <AUTOEDIT2 NAME="%%FTP-RPATH1" VALUE="/\Business Intelligence-Gas Consumption\Energent\Outgoing\Gas Consumption\"/>
      <AUTOEDIT2 NAME="%%FTP-PATH" VALUE="Not in use for application jobs"/>
      <AUTOEDIT2 NAME="%%FTP-USE_DEF_NUMRETRIES" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-RPF" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CLEAR_ALL" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE2" VALUE="FTP"/>
      <AUTOEDIT2 NAME="%%FTP-CONNTYPE1" VALUE="LOCAL"/>
      <AUTOEDIT2 NAME="%%FTP-LHOST" VALUE="Local"/>
      <AUTOEDIT2 NAME="%%FTP-RHOST" VALUE="hdq-nt164"/>
      <AUTOEDIT2 NAME="%%FTP-LPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-RPASSIVE" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-UPLOAD5" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRANSFER_NUM" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE1" VALUE="A"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE2" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE3" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE4" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-TYPE5" VALUE="I"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-MINSIZE5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TIMELIMIT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-UNIQUE1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-IF_EXIST5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-SRCOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-DSTOPT5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME1" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME2" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME3" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME4" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-ABSTIME5" VALUE="0"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM1" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM2" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM3" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM4" VALUE="1"/>
      <AUTOEDIT2 NAME="%%FTP-TRIM5" VALUE="1"/>
    </JOB>
    <JOB

Regards, Sriram.V

0

I don't really see why you would want to parse the whole XML file once for each FTP server address. It would make more sense to extract all server names and read them in a loop:

xmlstarlet sel -t -v '//JOB/AUTOEDIT2[@NAME="%%FTP-RHOST"]/@VALUE' -nl file.xml |
while IFS= read -r ftpserver; do
    # do things with "$ftpserver"
done

If you need to make sure you only get the first 16 jobs:

xmlstarlet sel -t -v '//JOB[position()<=16]/AUTOEDIT2[@NAME="%%FTP-RHOST"]/@VALUE' -nl file.xml |
while IFS= read -r ftpserver; do
    # do things with "$ftpserver"
done

The main issue in your code is that you are trying to use a shell variable in a single-quoted string. The shell will not expand variables in single-quoted strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.