Using Variables to Set Other Variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using Variables to Set Other Variables
# 1  
Old 04-20-2005
Using Variables to Set Other Variables

I have a script that I'm trying to shorten (below) by removing repetitive code.
if [[ -a ${abccontrols}/Commodity_NDM_${alt_db}_done ]]
then
commodity_ndm_done=Y
fi

if [[ -a ${abccontrols}/Customer_NDM_${alt_db}_done ]]
then
customer_ndm_done=Y
fi

if [[ -a ${abccontrols}/Department_NDM_${alt_db}_done ]]
then
department_ndm_done=Y
fi

if [[ -a ${abccontrols}/Division_NDM_${alt_db}_done ]]
then
division_ndm_done=Y
fi

if [[ -a ${abccontrols}/Order_Detail_NDM_${alt_db}_done ]]
then
order_detail_ndm_done=Y
fi

if [[ -a ${abccontrols}/Order_Summary_NDM_${alt_db}_done ]]
then
order_summary_ndm_done=Y
fi

if [[ -a ${abccontrols}/Witron_Forward_Pick_NDM_${alt_db}_done ]]
then
witron_forward_pick_ndm_done=Y
fi


And I tried using a loop as such:
{
echo "Commodity"
echo "Customer"
echo "Department"
echo "Division"
echo "Order_Detail"
echo "Order_Summary"
echo "Witron_Forward_Pick"
} |
while read v_type
do
if [[ -a ${abccontrols}/${v_type}_NDM_${alt_db}_done ]]
then
$(echo ${v_type} | tr '[A-Z]' '[a-z]')_ndm_done=Y
fi
done


Naturally, it doesn't work. Can I make variables set other variables by this method or another?
# 2  
Old 04-20-2005
i've tested creating a variable with another variable but it doesn't work ... so here's a consolation prize ... (i use "-f" to check if the file exists but you can change as required) ...
Code:
for v_type in Commodity Customer Department Division Order_Detail Order_Summary Witron_Forward_Pick
do
    if [ -f ${abccontrols}/${v_type}_NDM_${alt_db}_done ]
    then
          case $v_type in
          Commodity)   commodity_ndm_done=y ;;
          Customer)   customer_ndm_done=y ;;
          Department)   department_ndm_done=y ;;
          Division)   division_ndm_done=y ;;
          Order_Detail)   order_detail_ndm_done=y ;;
          Order_Summary)   order_summary_ndm_done=y ;;
          Witron_Forward_Pick)   witron_forward_pick_ndm_done=y ;;
          esac
    fi
done

# 3  
Old 04-21-2005
Not tested, but I think this will work....
Code:
#! /usr/bin/ksh
typeset -l var
LIST="Commodity Customer Department Division Order_Detail Order_Summary Witron_Forward_Pick"
for v_type in $LIST ; do
    if [ -f ${abccontrols}/${v_type}_NDM_${alt_db}_done ] ; then
                var=${v_type}_ndm_done
                eval \$$var=y
    fi
done
exit 0

# 4  
Old 04-21-2005
Thanks

Thanks, guys.

Perderabo, your code worked when I removed the \$ from the eval command. But it's pretty much what I was looking for.

#! /usr/bin/ksh
typeset -l var
for v_type in Commodity Customer Department Division Order_Detail Order_Summary Witron_Forward_Pick
do
if [[ -a ${abccontrols}/${v_type}_NDM__done ]]
then
var=${v_type}_ndm_done
eval ${var}=Y
else
var=${v_type}_ndm_done
eval ${var}=N
fi
done
echo "Job completion status is:"
echo "Commodity_NDM = ${commodity_ndm_done}"
echo "Customer_NDM = ${customer_ndm_done}"
echo "Department_NDM = ${department_ndm_done}"
echo "Division_NDM = ${division_ndm_done}"
echo "Order_Detail_NDM = ${order_detail_ndm_done}"
echo "Order_Summary_NDM = ${order_summary_ndm_done}"
echo "Witron_Forward_Pick_NDM = ${witron_forward_pick_ndm_done}\n"


and the output is correct (the last file being missing)
Job completion status is:
Commodity_NDM = Y
Customer_NDM = Y
Department_NDM = Y
Division_NDM = Y
Order_Detail_NDM = Y
Order_Summary_NDM = Y
Witron_Forward_Pick_NDM = N
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Set env variables for user

Hi , I have installed oracle in Solaris machine and unable to set the env variable. I tried to put the env variable in .dtprofile file but didn't help. So everytime I login in need to run the command and export the variable. Kindly suggest where I am doing wrong.Pls excuse as I am not too... (2 Replies)
Discussion started by: Rossdba
2 Replies

2. Shell Programming and Scripting

How to set a variable name from another variables value?

Experts, I want to set value of variables like this in bash shell: i=5 ; L=100 I want variable d5 (that is d(i) ) to be assign the value of $L , d$i=$L ; echo $d5 Not working Thanks., (3 Replies)
Discussion started by: rveri
3 Replies

3. Shell Programming and Scripting

Set variables from file, then loop

Hello, I am very, very new to shell scripting, but what I'm attempting to do is read in a list of user ID's to create on a database system from a CSV flat file, and for each entry run the "create user" script. I've gotten pretty far but I'm having trouble with the looping mechanism.... Any... (8 Replies)
Discussion started by: jkarren
8 Replies

4. Shell Programming and Scripting

Need to SET Environment variables

Hi Could you please tell me how to set environment variables in Unix ksh. And how can acess those varibles in shell scripts ( Please give the code with an example) For my scenario. We have written number of shell scripts with hard coded username and password. But if we want to... (1 Reply)
Discussion started by: shyamu544
1 Replies

5. Shell Programming and Scripting

Problems with expect and set variables

I'm writing a script that'll send a time-stamp to my backup server. I create a file with the name of the current date, send it to my server with scp and rm the file from the local computer. Individually these commands work fine and with a set name the expect scripts also work fine. The problem... (0 Replies)
Discussion started by: Ktesh564
0 Replies

6. SCO

Help finding where certain environment variables are set

i have two machines that should be identical but on one system there are some oracle environment (ORACLE_SID, ORACLE_HOME, etc...) variables that are not being set for the users. I am trying to find where those environment variables are being set on the system which is working properly. All... (5 Replies)
Discussion started by: kuliksco
5 Replies

7. UNIX for Dummies Questions & Answers

SET Command removed the variables

Hi, Im using csh and sometimes bash. I accidentally typed SET and looks like it has resetted some enviroment variables as im not able to run autosys jobs etc .. So i wanted to know how i can put it back to normal by replacing any profile files from another user or by editing the file which... (2 Replies)
Discussion started by: spectator
2 Replies

8. AIX

How to set permanent variables

I set my TERM variable to work with SMIT and everything works fine but when I logged out and log in again I have to set the variable again. How can I set a permanent variable into the system so it will be as I wish even if a reboot is needed? I set variables this way: export VAR=value (7 Replies)
Discussion started by: agasamapetilon
7 Replies

9. UNIX for Dummies Questions & Answers

Where are Oracle variables set in Solaris 5.9

Hi, It is solaris 5.9, and Oracle 10G is the database. If i login as user, and give 'env' command i can see the Oracle environment variables are set. PATH=/usr/bin:/usr/sbin:/usr/lib:/etc/ssh:/opt/oracle/app/oracle/product/10.2.0/db_1/bin:/opt/oracle/crs/oracle/product/10.2.0/crs_1/bin... (1 Reply)
Discussion started by: scriptlearner
1 Replies

10. UNIX for Dummies Questions & Answers

how to set up linux environment variables?

Hi I'm using Linux, in the directory /root/my there is a.out. but when I try to run it , the shell indicate "bash:a.out: command not found" but I AM working in this directory. if I use "./a.out" , it works perfectly. can any body tell me how to do a permanent set up so that I can use... (5 Replies)
Discussion started by: dell9
5 Replies
Login or Register to Ask a Question