Pass value from file to parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass value from file to parameter
# 1  
Old 12-05-2011
Pass value from file to parameter

Hi Guys,

I have a file in the format Parmater=value. I want to read the value and pass it to corresponding Variable.

The Parameter file is as follows
Code:
Number=23
Text1=mango
Text2=yup

'Number' value needs to be read and passed to ID variable. Also, 'Text1' value needs to be passed to 'Name' Variable and 'Text2' for Department variable.

Following is the code i tried.

Code:
while read line
do
c=`awk -F"=" '{print $2}' $line`
case $c in
Number) ID=$line;;
Text1) Name=$line;;
Text2) Department=$line ;;
esac
done < filename.

Can somebody guide me where i am doing wrong.

Cheers!!!!!

Last edited by mac4rfree; 12-05-2011 at 08:41 AM.. Reason: changed it with proper names
# 2  
Old 12-05-2011
Hi, you are assigning field 2 to c instead of field 1. Plus, you probably want to assign field 2 to the variables Id, Name and Department instead of the whole line.

Last edited by Scrutinizer; 12-05-2011 at 08:36 AM..
# 3  
Old 12-05-2011
Are 'a', 'b', 'c' the actual values in your input file, or are they 'ID', 'Name', 'Department'?

If they're arbitrary characters, you could do:
Code:
tr '=' ' ' < filename| while read par val
do
   case $par in
      'a') ID=$val;;
      'b') NAME=$val;;
      'c') DEPARTMENT=$val;;
   esac
done


Last edited by CarloM; 12-05-2011 at 08:41 AM..
# 4  
Old 12-05-2011
I have to read the parameter file whatever value 'Number' is associated to has to be passed to 'ID' Variable. Similarly, Text1 to 'Name' and 'Text2' to 'Department'.

---------- Post updated at 06:16 PM ---------- Previous update was at 06:13 PM ----------

@CarloM, i guess that will help me. The only thing is, i should not make any alterations to the file. Will tr command make any changes to the parameter file?
# 5  
Old 12-05-2011
No - it just writes to stdout (which is being piped to the while).

If your actual input file is more complex you can still use awk, just pipe the output of that into the while instead.
# 6  
Old 12-05-2011
yeah you are correct. But the variables are not getting assinged. I really appreciate your help but can you point out as what i am doing wrong. Please find the below with the actual file.

Code:
mac> cat Dallysales.prm
GP_BATCH_TRK_ID=56
GP_IP_FILE1=/appl/data/Input/US/DAILYSLS_US_LOAD*.txt
GP_SLS_DT_CURR_YR=20110524
GP_COUNTRY=US
GP_BUSINSS_UNIT=GMUS
mac> tr '=' ' ' < Dallysales.prm | while read parm value
> do
> case $parm in
> 'GP_BATCH_TRK_ID') Tracking_ID=${value} ;;
> 'GP_IP_FILE1') File=${value} ;;
> 'GP_SLS_DT_CURR_YR') Sales_date=${value} ;;
> 'GP_COUNTRY') Country=${value} ;;
> 'GP_BUSINSS_UNIT') BU=${value};;
> esac
> done
mac> echo $File
mac> echo $Sales_date
mac> echo $BU
mac>

# 7  
Old 12-05-2011
That should work (at least it does on the ksh I'm using), but it may be that your shell is executing the commands in a subshell.

Try putting the script in a file and doing . ./scriptname.sh.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (3 Replies)
Discussion started by: Debalina Roy
3 Replies

3. Shell Programming and Scripting

How to pass the parameter in xml file in UNIX shell script?

Hi, I have an XML file like the following... <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ONDEMAND_JOB VERSION="5.1" LOCALE="en_US"> <IMPORT_JOBSET TC_CONNECTION_NAME="default" ENVIRONMENT="PRD" USERNAME="Administrator" PASSWORD="AdminPassword" CALENDAR="Main Monthly Calendar"... (2 Replies)
Discussion started by: Debalina Roy
2 Replies

4. Programming

How to pass parameter from file to sqlplus in UNIX?

i have file in which i have employee id are there and every time number of employee id are different in file means number of count of employee id in file are every time different. 343535435 365765767 343534543 343543543 i want to pass this file to sqlplus and sql command is ... (7 Replies)
Discussion started by: pallvi_mahajan
7 Replies

5. Shell Programming and Scripting

Pass parameter

Hi, I have following for loop , please let me know how to get ${TXP_EXT_TABLE_${i}_SQL} parameter with 1DAY and 7DAY values. for i in 1DAY 7DAY do ${NZSQL_DIR}/nzsql -h ${HOST} -time -v ON_ERROR_STOP=1 -f ${SQL_DIR}/${TXP_EXT_TABLE_${i}_SQL} > ${TMP_LOG_FILE} 2>&1 done ... (4 Replies)
Discussion started by: sandy162
4 Replies

6. UNIX for Dummies Questions & Answers

How to pass the parameter value to a... ?

Hello I have a simple code like this one: #!/bin/ksh VER=$1 cat /usr/text | while read line do echo $line done Let's say $1=1.0.0 and the contents of text is: abcd.cfg asdf I would like the output to be like this abcd1.0.0.cfg asdf1.0.0 I am thinking of passing the... (5 Replies)
Discussion started by: khestoi
5 Replies

7. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

8. Shell Programming and Scripting

How to pass a parameter

Hi all, How to pass a parameter from a oracle pl/sql procedure parameter to shell environment and use it? (1 Reply)
Discussion started by: megh
1 Replies

9. Shell Programming and Scripting

Pass tablename as a parameter in the Control File -- sqlldr

Just wanted to know if there is a way to pass the table name as a parameter in the control file. I have two tables...Table A and Table B. LOAD DATA append INTO TABLE TABLE_B FIELDS TERMINATED BY X'09' OPTIONALLY ENCLOSED BY '"' AND '"' TRAILING NULLCOLS Rather than hard coding... (1 Reply)
Discussion started by: madhunk
1 Replies
Login or Register to Ask a Question