The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Awk and Variables deepak4you Shell Programming and Scripting 4 11-15-2007 09:18 AM
Variables bobo UNIX for Dummies Questions & Answers 1 01-19-2007 01:37 AM
doing a sed on certain variables seaten UNIX for Dummies Questions & Answers 10 09-05-2005 11:45 PM
doing a sed with variables seaten Shell Programming and Scripting 1 05-06-2005 04:08 PM
doing a sed with variables seaten UNIX for Dummies Questions & Answers 1 05-06-2005 03:48 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-20-2005
superdelic superdelic is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 8
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 (permalink)  
Old 04-20-2005
Just Ice's Avatar
Just Ice Just Ice is offline Forum Advisor  
Lights on, brain off.
  
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 637
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 (permalink)  
Old 04-21-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
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 (permalink)  
Old 04-21-2005
superdelic superdelic is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 8
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0