How to pass variable variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass variable variable
# 1  
Old 05-20-2008
Question How to pass variable variable

Dear Unix guru's,

I have a SUSE linux variant with SAP running on it. From SAP it is possible to call UNIX commands, like the shell with parameters.
We have several of these systems, like a development system (DEV), a test system (TST) and a production system (PRD).

I have created a shell-script that works, but it references the machines in a hardcoded way. So my scripts work on PRD but not on DEV. For example, some datafile that I must work with in my script is located in directory:
/interface/PRD/in
Unfortunately it is company policy to have the system name in the directory tree.

One rule of my script (as an example) looks like this:
cp /interface/PRD/in/* /interface/PRD/work

Now I would like to have PRD as a variable so that would become:
cp /interface/$1/in/* /interface/$1/work

In this manner I can copy the scripts from one machine to another and they can still run. However, the parameter $1 for the script should be filled based on the content of a file: sysid.cfg which will contain one line only with the value DEV, TST or PRD. I can generate such a file from my SAP system.

Any suggestions on how I can do this?

(PS: I am not a UNIX guy, so please use plenty of explanation in your answer...Smilie)
# 2  
Old 05-20-2008
You've sort of answered you question, you substituted $1 which is the first command line argument to executing your script.

i.e.
# ./myscript.sh option1

$1 = option1

To verify you an do something like:

Code:
#!/bin/sh
#test_commandline_args.sh

echo $1

Now you should definitely create just 1 script across your 3 different environments, this can be done in many ways, you can query the hostname of the system if it can designate which is PROD/TEST/DEV or you can do what you had mentioned, which was to accept command line arguments. You can also do some checking to verify that its only one of the 3 options and error on any other input.

i.e.
Code:
#!/bin/sh
# myscript.sh

if [ $# != 1 ]; then
        echo "Usage: $0 [option]"
        echo "Option: PROD, TEST, DEV"
        echo -e "\ti.e. - $0 PROD\n"
        exit 1
fi

case $1 in
        PROD|prod)
                        ENV="PROD"
                        ;;
        TEST|test)
                        ENV="TEST"
                        ;;
        DEV|dev)
                        ENV="DEV"
                        ;;
                *)
                        echo "Usage: $0 [option]"
                        echo "Option: PROD, TEST, DEV"
                        ;;
esac

## use variable "ENV" to pass into the directory structure ##
echo $ENV

This should help, there's more than one way to do this but here's an option
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

4. UNIX for Dummies Questions & Answers

How To Pass an Array Variable

Hi, I have a master BASH shell script where I define a bunch of variables: $var1=why $var2=is $var3=(this so hard) I would then like to call another shell script and pass these variables to it: $script2 $var1 $var2 $var3 This works fine for var1 and var2. However, var3 is an array,... (9 Replies)
Discussion started by: msb65
9 Replies

5. Shell Programming and Scripting

how to pass variable to grep?

Hi I have a such conditional: SPAMH="it is SPAM" if grep -q $SPAMH $NMDIR/$mail; then SPAMHFLAG=1 else SPAMHFLAG=0 fi And grep doesn't catch this string, even it exists there. I think it's a problem with passing $SPAMH to grep. I tried... (2 Replies)
Discussion started by: xist
2 Replies

6. UNIX for Dummies Questions & Answers

How do I pass a variable to awk?

I have an awk statement where I Need to pass an environment variable but I cannot get it to work: My evironment varible examples below: $FILE1=/dev/fs/file.new $FILE2=/dev/fs/file.old Code below: awk -F"|" ' BEGIN { while( getline < "$FILE1" ) { arr=1 } } arr != 1 { print } '... (12 Replies)
Discussion started by: eja
12 Replies

7. Shell Programming and Scripting

getting the file name and pass as variable

Can any one suggest me how to check the file extension and pass the name based out of the filename within the folder. There would be always one latest file in the folder, but extension may vary... ie .csv, .CSV,.rpt,.xls etc what is best way to get the latest file name and pass as variable.... (1 Reply)
Discussion started by: u263066
1 Replies

8. UNIX for Dummies Questions & Answers

how to pass the value of a variable to another variable?

for eg. my code is this: line="" cat t | while read a do echo $a cat $a >line done echo $line the highlighted lines seems to have error. I wan the variable $a to pass to to variable line but it seems to have error...any help? (2 Replies)
Discussion started by: forevercalz
2 Replies

9. UNIX for Dummies Questions & Answers

pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop. i tried this... let x=0 until test $x -eq 32 do cat file | awk '{ print $1 , "Number" , $($x) }' >> output done thanks, (4 Replies)
Discussion started by: inquirer
4 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question