calling a aliased variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calling a aliased variable
# 1  
Old 07-15-2002
calling a aliased variable

Issue:

i have variable A which is an alias for variable B which is equal to "THIS IS A TEST"

when every i echo variable A i only get the alias name for variable B, NOT the contents of variable B.



Code:
HOSTNAME# echo $TESTIT
+ echo THIS IS A TEST
THIS IS A TEST

HOSTNAME# ls -l
total 16
-rw-r--r--   1 user group        6 Jul 15 13:58 TEST.IT


HOSTNAME# for i in `ls`; do
DOH=`echo $i|awk -F. '{ print "$"$1$2 }'`
echo $DOH
done
$TESTIT


HOSTNAME#


Last edited by Optimus_P; 07-15-2002 at 05:55 PM..
# 2  
Old 07-15-2002
From your first statement:
Quote:
i have variable A which is an alias for variable B which is equal to "THIS IS A TEST"
I set A to equal value of B - echo $A - get nothing (B was nothing)
Set B and then A again - and the value shows up.
$ A=$B
$ B="This is a test"
$ echo $A

$ A=$B
$ echo $A
This is a test


As far as the code:
I don't see why you get two $TESTIT at the end. When I ran it, it only writes it out once ($TESTIT). I believe you are attempting to cat the file (show the contents of the file) Could you explain more of what you are attempting to do? What shell you are using and OS+version?
# 3  
Old 07-15-2002
sorry mate just got back from lunch.

OS: SOL 8
Shell: ksh

I have a list of files in a directory.

ie:
test.it
test.me
test.u

it takes this list and strips off the "." so i get testit, testme, testu.

these are actually variables that are sourced in from another file.
(i am sorceing it correctly)

so if you do a echo on those variables you get soemthing like the following:

echo $testit
THIS IS TEST IT

echo $testme
THIS IS TEST ME

echo $testu
THIS IS TEST U

Code:
. ./source-file
for i in `ls`; do
DOH=`echo $i|awk -F. '{ print "$"$1$2 }'`
echo $DOH
done

this only prints out the word $testit/$testme/$testu not the contents of what the variable is supposed to be.

now if by hand i say

doh=$testme
echo $doh
(it will then print the correct output) but useing the awk statement to fill the contents of $doh does not seem to work.
# 4  
Old 07-15-2002
just so you know what the actual script looks like.

Code:
. /var/adm/scripts/dfh

INDIR="/drop"
ARCHIVEDIR="/archive_messages/exported"
OUTDIR="/export/manual"
SENDDIR="/export/mbi"
FUUSER="user"
FUGROUP="group"
OPENFILE=`/usr/local/bin/lsof +d ${INDIR}|grep ${FUUSER}|awk -F"/" '{ print $4 }'`

set -x

if [[ -d ${INDIR} ]] ; then
        cd ${INDIR}
        for file in `ls` ; do

DFHNAME=$`echo $file | awk -F. '{ print $1 $2 }'`

                if [[ $file != ${OPENFILE} && `tail -1 $file|awk '{ print $1 }'` = "97" ]]; then
                        chown $FUUSER:$FUGROUP $file
                        echo "$DFHNAME" > /$OUTDIR/$file.tmp
                        cat /$INDIR/$file >> /$OUTDIR/$file.tmp
                        rm -f $file
                        cp -p  /$OUTDIR/$file.tmp $ARCHIVEDIR/$file.`date +%Y%m%d%H%M%S`
                        mv /$OUTDIR/$file.tmp $SENDDIR/$file.`date +%Y%m%d%H%M%S`
                                        fi
        done
fi

# 5  
Old 07-15-2002
Quote:
now if by hand i say
doh=$testme
echo $doh
(it will then print the correct output) but useing the awk statement to fill the contents of $doh does not seem to work.
Realize you are not setting DOH to a value of another variable - you are setting it to "$" $1$2. This is two different things. Just as I stated earlier, if B is nothing setting A=$B give you nothing.

You are stating DOH to be equal to a string "$TESTIT" not a value of the variable $TESTIT. I'm not sure the work around (I've been looking...). Will get back to it tomorrow if no one else has (must go home as the EOD is here).
# 6  
Old 07-15-2002
If I'm understanding this correctly, try this:

# b="this is a test"
# a="b"
# eval echo \$$a
this is a test
# 7  
Old 07-16-2002
gsatch: i dont have the eval command. i will see if i can download it.

RTM: your help is much appricated. Yes, shortyly after i posted last night i went home also. and your answer makes complete sense; something i had not thought of.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calling specific characters from a find variable

I'm trying to do something like this: find . -name blablabla -exec ln -s ./"{:53:14} blablabla" \; The idea is find blablabla and create a symbolic link to it using part of it's path and then it's name, "blablabla." I just don't know if I can call characters out of a find variable. ... (16 Replies)
Discussion started by: scribling
16 Replies

2. Shell Programming and Scripting

Calling a variable of variable from a file

Hi All, I have file which have looks like below abc=${def} def=${efg} efg= "this is the actual value" based on "abc" value I have to call "efg" value , Am using below lines but it is not working #!/bin/bash source file.txt echo $abc Please wrap all code, files, input &... (5 Replies)
Discussion started by: Prashanth.K
5 Replies

3. Shell Programming and Scripting

Calling a Variable based on a Variable

Hi all, I have a source config file with variables like so: eth1_ip=192.168.1.99 eth2_ip=192.168.1.123 eth3_ip=172.16.1.1 I am trying to run a script which loops based on the number of eth interfaces on a machine and therefore modifies the variable it calls in the environment based on the... (5 Replies)
Discussion started by: landossa
5 Replies

4. UNIX for Dummies Questions & Answers

Calling a variable from a variable.

Hi everyone, Is it possible to set a variable that calls another variable? I.E. SCRIPT=MY_SCRIPT.ksh ${VAR5} ${VAR5} is set earlier in the script, and I want to be able to call this when setting the ${SCRIPT} variable. I hope this makes sense. Thanks for your help. (3 Replies)
Discussion started by: jimbojames
3 Replies

5. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

6. Shell Programming and Scripting

Calling a variable in another variable

echo "$previous_tmp$i" I have a 5 variables like previous1 previous2 previous3 previous4 previous5 I want to use a for loop to call them one by one. How can I ?:confused: (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

7. Shell Programming and Scripting

PERL script -- calling 'sed' by passing 'variable value'.

Hi Friends, I'm calling 'sed' command inside one perl script, which is to list directory names which are having some date value as their names (in the form YYYYMMDD) with in the range (start and end date). #!/usr/bin/perl -w use strict; use warnings; my $DATA = "/export/home/ganapa"; my... (5 Replies)
Discussion started by: ganapati
5 Replies

8. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

9. Shell Programming and Scripting

calling a variable to echo to a log

Hi everyone, I am trying to create a simple batch file to make SQL backups. this part of it works fine. Currently the script can mysql dump the databases, compress them, delete the .sql, compress the individual tar.gz into one larger one, delete the smaller files, encrypt the final tar.gz and... (1 Reply)
Discussion started by: luma
1 Replies

10. Shell Programming and Scripting

Can we use aliased commands in script?

Hi All, I need a small help.. when we use aliased commands in shell script, they are not being recognized when I used. Is there any way to use aliased commands in scritping? Please let me know if u know... Thank you Chanu (19 Replies)
Discussion started by: Chanakya.m
19 Replies
Login or Register to Ask a Question