Korn Shell help - Using parameter to create variable names


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell help - Using parameter to create variable names
# 1  
Old 02-14-2013
Hammer & Screwdriver Korn Shell help - Using parameter to create variable names

I'm using korn shell and I am wondering if it's possible to use a parameter passed into a function to build a variable name in a configuration file. I have the function in one source file, I'd like to have a global configuration file instead of hardcoding logins to each script.

So I have a function, it accepts a system ID and I want to use that ID to read a configuration file for the server, user name and login for several different FTP sites. When I pass in the system ID of boe I then want to read the configuration file for boeServer, boeUser and boePassword but keep getting not found.

This is my source file functions:
Code:
function ftpConfirm {
system=$1
# Configuration file to look up FTP information
. $HOME/ftpLogins
server="${system}Server"
user="${system}User"
password="${system}Password"
echo "**** System variable passed in: $system"
echo "**** Server we are connecting to: $server"
echo "**** User name: $user"
echo "**** Password: $password"
echo "* Just referencing the variable returns the results I want: $boeServer"

Here's my config file ftpLogins:
Code:
export boeServer=ftp1
export boeUser=teamUser
export boePassword=download
export ibmServer=ibm1
export ibmUser=teamUser
export ibmPassword=download

And a simple script to call the function that I'm using:
Code:
#!/bin/ksh -f
. $HOME/fucntions
ftpConfirm "boe"

This would allow us to essentially build one function that could FTP anywhere we need and if we FTP to that server more than once, we would have to make just one change.

Thanks in advance,
Mike
# 2  
Old 02-14-2013
You must reevaluate using eval, like to print the last argument:
Code:
eval echo '$'$#

# 3  
Old 02-14-2013
That worked on the echo commands only. Now when I'm using the eval during the FTP, it's not reading the user name and password. I keep getting the error: 530 Please login with USER and PASS.

Code:
eval ftp -n -v '$'$server <<++EOF++ > logfile
eval user '$'$user '$'$password

Seems like it's connecting to the server, I see some messages. But it's not recognizing the user name or password variables.

Thanks,
Mike

Last edited by mrevello; 02-14-2013 at 06:06 PM..
# 4  
Old 02-14-2013
If someone puts `rm -Rf /home/username` into your config file, eval will execute that!

Fortunately you don't need eval to set arbitrary variables, read can do that.

Code:
A=abc
B=def

read ${A}${B} <<EOF
string
EOF

echo "abcdef = ${abcdef}"

# 5  
Old 02-14-2013
Neat! I suppose that might work in some other places where variables have no $, if any.
# 6  
Old 02-15-2013
I was able to get the eval to work but no luck on the read. I'm curious because I don't want someone accidentally running a command. However, after a few hours of trying different combinations, read wouldn't resolve a string. Take the following sample, the script would just hang.

Code:
read ${server} <<EOF
${system}Server
EOF

Per my samples where I pass in boe, I figured it would've resolved the ${system} to be boe and then look up boeServer in the ftpLogins file. In the end, I expect the variable server to have the value ftp1. While I'm happy for the eval solution, security wise I'd love to use read if I could.

Thanks,
Mike
# 7  
Old 02-15-2013
Not sure what your read was supposed to do. The read example worked for you, didn't it?

Eval is like 'echo ... | ksh' but cheaper without the fork, exec, pipe, nice in high rep loops.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass script with parameter in korn shell script

I have written a script which will take input parameter as another script. However, if the script passed as input parameter has parameters then this script doesn't work. I have a script b.ksh which has 1 and 2 as parameters I have a script c.ksh which has 3,4 and 5 as parameters vi a.ksh... (1 Reply)
Discussion started by: Vee
1 Replies

2. Shell Programming and Scripting

Segregate by suffixed file names using Korn Shell

I have following files at /dir1 a.csv.20131201 b.csv.20131201 c.csv.20131201 d.csv.20131201 a.csv.20131202 b.csv.20131202 c.csv.20131202 d.csv.20131202 ....................... ....................... ....................... ....................... I need to move these files to... (4 Replies)
Discussion started by: JaisonJ
4 Replies

3. UNIX for Dummies Questions & Answers

How to import a variable Used in Another Korn Shell Script?

Hi I am using two shell scripts which are running on the system simultaneously. And in one of the script i am exporting an Integer Variable. Now i want to use the variable in another script. But i cannot run the first script in the second as the first script has many other functions which... (3 Replies)
Discussion started by: Ajesh
3 Replies

4. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

5. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

6. Programming

How to refer to variable (korn shell)?

Hi I have the following block of code in korn shell and don't now how to refer to variable `print variable1.$dvd` ? --- integer dvd=4 integer number=0 while (( dvd!=0 )) do print "Iteracja numer : $dvd" print "$_" #it refers to $dvd var but want to refer... (3 Replies)
Discussion started by: presul
3 Replies

7. Shell Programming and Scripting

Korn Shell Variable values difference

I am using two shell scripts a.ksh and b.ksh a.ksh 1. Sets the value +++++++++++++++++ export USER1=abcd1 export PASSWORD=xyz +++++++++++++++++ b.ksh 2. Second scripts calls sctipt a.ksh and uses the values set in a.ksh and pass to an executable demo... (2 Replies)
Discussion started by: kunalseth
2 Replies

8. Shell Programming and Scripting

compound variable in korn shell

in a text " Korn Shell Unix programming Manual 3° Edition" i have found this sintax to declare a compoud variable: variable=( fild1 fild1 ) but this sintax in ksh and sh (HP-UNIX) not work... why?? exist another solution for this type of variable ??? (5 Replies)
Discussion started by: ZINGARO
5 Replies

9. Shell Programming and Scripting

compound variable in korn shell

in a text " Korn Shell Unix programming Manual 3° Edition" i have found this sintax to declare a compoud variable: variable=( fild1 (0 Replies)
Discussion started by: ZINGARO
0 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question