andy_steven
asked on
Korn Shell Script
Hi,
Below is my original script I wrote, which I need to modify to do the following :
1) read values from a tab delimited flat file,
2) assign the values from the flat file to specific variables,
3) and then loop through the flat file and repeat the assignment of values to the variables until the end of the flat file.
Here is my script that needs modifying:
My Script
-----------
#!/bin/ksh
###################### modify_upload_cr_node.ksh ########################## #######
#
# In TI's testlab environment they do not have access to their production network.
# To work around this issue, a CR internal utility – show tech upload – can be
# used. Please follow the instructions below on how to extract the config file
# from the router, then use the “show tech upload” mechanism to upload without
# access to live router.
#
# CR_DataMiner Usage:
# CR_DataMiner ipAddress snmpPort snmpCommunity fileName connectionProtocol
# username password enablePassword
#
# Where:
# ipAddress = IP address of the router or switch
# snmpPort = SNMP agent port number
# snmpCommunity = SNMP community string
# fileName = output file name - if it exists, it will be
# overwritten without a warning
# connectionProtocol = connection protocol (SSH/telnet) - for any value
# other than SSH, telnet is used
# username = user account
# password = password
# enablePassword = password to enter privileged mode
#
# CR upload procedure (from CR_DataMiner :output):
# 1. CR_DataMiner must be executed from within the NPA environment
# 2. Run CR_DataMiner for the desired node.
# 3. Set the output filename so that the string 'showtech' is part of
# the filename. e.g. 'file1.showtech.txt'
# 4. Copy the output file to the NPA servers /tmp directory
# 5. Within Netprovision Activator, create a new node:
# - Set 'Managed' to FALSE
# - Set 'Fabric File' and 'Service File' to the name of the file
# copied to the /tmp directory, do not include the path
# 6. 'Upload' the created node.
#
# This script performs steps 5 and 6 above. Firstly it
# modifies the Cisco Router Node parameters in CRndNode.oaf file
# to the following :
#
# srmanaged set to FALSE
# crfabfile set to showtec.conf flat file
# crservfile set to showtec.conf flat file
#
# Then it performs an upload of a specific Cisco Router Node, which
# has already been created in the database.
#
########################## ########## ########## ########## ########## ########## #####
if [[ $# -ne 2 ]]; then
print "Usage: modify_upload_cr_node.ksh <node name> <fabric and service flat file>\n"
print "Example: modify_upload_cr_node.ksh R-TS31 r-ts31.conf.showtech.conf"
exit 1
fi
typeset Command_Name=$0
typeset Node_Name=$1
typeset Show_Tech=$2
function checkRunEnv
{
if [[ -z "$CCP_REL" ]];
then
print "\nERROR: Not in run enviroment. Exiting....\n"
exit
fi
}
checkRunEnv
# Initialize the script manager
export NPSNEWSESSION=1
export NPSLOG=${0}.log
export NPSMODE=Trace
# typeset STDOUT=${0}.stdout
# typeset STDERR=${0}.stderr
. $CCP_BIN/npsinit
trap "nps error" ERR
SESSION=${NPSSESSIONID#SYs snSession: }
TRID=$(AVcr start $SESSION)
# From command line insert node name, fabric and service text files
set -A CRIT srname=${Node_Name}
set -A NEW_ATTS crfabfile=${Show_Tech} crservfile=${Show_Tech} srmanaged="FALSE"
nps trace "These are attributes of transaction $TRNUM"
nps selectone CRndNode -criteria CRIT NODE_ID
if nps modify $NODE_ID NEW_ATTS; then
echo "\nNode name =${Node_Name} with node ID =$NODE_ID modified"
if AVcr apply $TRID $SESSION; then
echo "Transaction $TRID applied"
fi
else
"nps modify failed with error code $?"
if AVcr abandon $TRID $SESSION; then
echo "Transaction $TRID abandoned "
fi
fi
# Perform an upload after modify
set -A UPLD_ATTS UploadType=Node_Fabric_Ser v
nps upload $NODE_ID UPLD_ATTS UROID
echo "Node name=${Node_Name} with node ID=$NODE_ID with $UPLD_ATTS uploaded"
export NPSMODE=Notrace
# AVcr apply $TRID $SESSION
nps stop
End of script
---------------
As can be seen from the script the user has to enter 2 arguements on the command line. However, I have created a tab delimited flat file called nodename_showtech.lookup that contains the 2 arguements required for the script (see below for example of flat file). I now want my script to read the flat file and assign the values to Node_Name and Show_Tech variables. Then I want the script to repeat this assignment until the end of the flat file.
My tab delimited flat file
-------------------------- --
Node_Name Show_Tech
R-AL23 r-al23.conf.showtech.conf
R-AN24 r-an24.conf.showtech.conf
R-BA19 r-ba19.conf.showtech.conf
R-BA3D r-ba3d.conf.showtech.conf
R-BA52 r-ba52.conf.showtech.conf
R-BA66 r-ba66.conf.showtech.conf
R-BG42 r-bg42.conf.showtech.conf
R-BO102 r-bo102.conf.showtech.conf
R-BO103 r-bo103.conf.showtech.conf
R-BO106 r-bo106.conf.showtech.conf
R-BO10D r-bo10d.conf.showtech.conf
R-BO11D r-bo11d.conf.showtech.conf
R-BO16D r-bo16d.conf.showtech.conf
R-BO17D r-bo17d.conf.showtech.conf
R-BO18D r-bo18d.conf.showtech.conf
R-BO44 r-bo44.conf.showtech.conf
R-BO45 r-bo45.conf.showtech.conf
R-BO94 r-bo94.conf.showtech.conf
R-BO9D r-bo9d.conf.showtech.conf
R-BS1D r-bs1d.conf.showtech.conf
R-BS2D r-bs2d.conf.showtech.conf
R-BS38 r-bs38.conf.showtech.conf
R-BS54 r-bs54.conf.showtech.conf
R-BS60 r-bs60.conf.showtech.conf
I'm new to Korn shell scripting and I do not how to do what I want. Any help would be appreciated.
Best regards,
Andy Steven.
Below is my original script I wrote, which I need to modify to do the following :
1) read values from a tab delimited flat file,
2) assign the values from the flat file to specific variables,
3) and then loop through the flat file and repeat the assignment of values to the variables until the end of the flat file.
Here is my script that needs modifying:
My Script
-----------
#!/bin/ksh
###################### modify_upload_cr_node.ksh ##########################
#
# In TI's testlab environment they do not have access to their production network.
# To work around this issue, a CR internal utility – show tech upload – can be
# used. Please follow the instructions below on how to extract the config file
# from the router, then use the “show tech upload” mechanism to upload without
# access to live router.
#
# CR_DataMiner Usage:
# CR_DataMiner ipAddress snmpPort snmpCommunity fileName connectionProtocol
# username password enablePassword
#
# Where:
# ipAddress = IP address of the router or switch
# snmpPort = SNMP agent port number
# snmpCommunity = SNMP community string
# fileName = output file name - if it exists, it will be
# overwritten without a warning
# connectionProtocol = connection protocol (SSH/telnet) - for any value
# other than SSH, telnet is used
# username = user account
# password = password
# enablePassword = password to enter privileged mode
#
# CR upload procedure (from CR_DataMiner :output):
# 1. CR_DataMiner must be executed from within the NPA environment
# 2. Run CR_DataMiner for the desired node.
# 3. Set the output filename so that the string 'showtech' is part of
# the filename. e.g. 'file1.showtech.txt'
# 4. Copy the output file to the NPA servers /tmp directory
# 5. Within Netprovision Activator, create a new node:
# - Set 'Managed' to FALSE
# - Set 'Fabric File' and 'Service File' to the name of the file
# copied to the /tmp directory, do not include the path
# 6. 'Upload' the created node.
#
# This script performs steps 5 and 6 above. Firstly it
# modifies the Cisco Router Node parameters in CRndNode.oaf file
# to the following :
#
# srmanaged set to FALSE
# crfabfile set to showtec.conf flat file
# crservfile set to showtec.conf flat file
#
# Then it performs an upload of a specific Cisco Router Node, which
# has already been created in the database.
#
##########################
if [[ $# -ne 2 ]]; then
print "Usage: modify_upload_cr_node.ksh <node name> <fabric and service flat file>\n"
print "Example: modify_upload_cr_node.ksh R-TS31 r-ts31.conf.showtech.conf"
exit 1
fi
typeset Command_Name=$0
typeset Node_Name=$1
typeset Show_Tech=$2
function checkRunEnv
{
if [[ -z "$CCP_REL" ]];
then
print "\nERROR: Not in run enviroment. Exiting....\n"
exit
fi
}
checkRunEnv
# Initialize the script manager
export NPSNEWSESSION=1
export NPSLOG=${0}.log
export NPSMODE=Trace
# typeset STDOUT=${0}.stdout
# typeset STDERR=${0}.stderr
. $CCP_BIN/npsinit
trap "nps error" ERR
SESSION=${NPSSESSIONID#SYs
TRID=$(AVcr start $SESSION)
# From command line insert node name, fabric and service text files
set -A CRIT srname=${Node_Name}
set -A NEW_ATTS crfabfile=${Show_Tech} crservfile=${Show_Tech} srmanaged="FALSE"
nps trace "These are attributes of transaction $TRNUM"
nps selectone CRndNode -criteria CRIT NODE_ID
if nps modify $NODE_ID NEW_ATTS; then
echo "\nNode name =${Node_Name} with node ID =$NODE_ID modified"
if AVcr apply $TRID $SESSION; then
echo "Transaction $TRID applied"
fi
else
"nps modify failed with error code $?"
if AVcr abandon $TRID $SESSION; then
echo "Transaction $TRID abandoned "
fi
fi
# Perform an upload after modify
set -A UPLD_ATTS UploadType=Node_Fabric_Ser
nps upload $NODE_ID UPLD_ATTS UROID
echo "Node name=${Node_Name} with node ID=$NODE_ID with $UPLD_ATTS uploaded"
export NPSMODE=Notrace
# AVcr apply $TRID $SESSION
nps stop
End of script
---------------
As can be seen from the script the user has to enter 2 arguements on the command line. However, I have created a tab delimited flat file called nodename_showtech.lookup that contains the 2 arguements required for the script (see below for example of flat file). I now want my script to read the flat file and assign the values to Node_Name and Show_Tech variables. Then I want the script to repeat this assignment until the end of the flat file.
My tab delimited flat file
--------------------------
Node_Name Show_Tech
R-AL23 r-al23.conf.showtech.conf
R-AN24 r-an24.conf.showtech.conf
R-BA19 r-ba19.conf.showtech.conf
R-BA3D r-ba3d.conf.showtech.conf
R-BA52 r-ba52.conf.showtech.conf
R-BA66 r-ba66.conf.showtech.conf
R-BG42 r-bg42.conf.showtech.conf
R-BO102 r-bo102.conf.showtech.conf
R-BO103 r-bo103.conf.showtech.conf
R-BO106 r-bo106.conf.showtech.conf
R-BO10D r-bo10d.conf.showtech.conf
R-BO11D r-bo11d.conf.showtech.conf
R-BO16D r-bo16d.conf.showtech.conf
R-BO17D r-bo17d.conf.showtech.conf
R-BO18D r-bo18d.conf.showtech.conf
R-BO44 r-bo44.conf.showtech.conf
R-BO45 r-bo45.conf.showtech.conf
R-BO94 r-bo94.conf.showtech.conf
R-BO9D r-bo9d.conf.showtech.conf
R-BS1D r-bs1d.conf.showtech.conf
R-BS2D r-bs2d.conf.showtech.conf
R-BS38 r-bs38.conf.showtech.conf
R-BS54 r-bs54.conf.showtech.conf
R-BS60 r-bs60.conf.showtech.conf
I'm new to Korn shell scripting and I do not how to do what I want. Any help would be appreciated.
Best regards,
Andy Steven.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
and you want to put them into array vars.
Here's an sample script:
#!/bin/ksh
typeset -i cnt=0
while read read Node_Name Show_Tech
do
node[$cnt]=$Node_Name
tech[$cnt]=$Show_Tech
((cnt = cnt + 1))
done
exit
You can read the file and assign the value to ENV var use the same syntax.