based on "abc" value I have to call "efg" value , Am using below lines but it is not working
Moderator's Comments:
Please wrap all code, files, input & output/errors in CODE tags as required by the forum rules.
It makes them far easier to read and preserves multiple spaces for indenting or fixed width data.
Last edited by rbatte1; 08-04-2016 at 06:11 AM..
Reason: Code tags
Indeed, it IS working. Exactly as expected from the specifications of the bash shell. It may not do what you intend to do, though. Please explain more explicitly what your intentions are.
Did you consider "indirect expansion" ( ${!var} ) that recent bash versions offer, or "associative arrays"?
In this case I'd say the same as i thought when i read the first post: there must have happened a serious flaw in the design process before to make such outrageous means necessary. This is the same as the old:
which should equally be avoided like the plague. Life is too short to waste it chasing evals once they go haywire (which the always do sooner or later).
No, does not help, sorry.
is nicer than
especially if more context is added.
Sourcing shell code means you must trust the external source.
Sourcing variables is only a little safer, you must still trust it.
Back to the original problem, here is an elegant method
No, does not help, sorry.
is nicer than
especially if more context is added.
I beg to respectfully differ: both are great methods to shoot oneself into foot, even if i have to concede that your method is doing so with a nicer looking gun.
Perhaps the only reason to use such an indirect evaluation (regardless of using your construct or mine) is because the function doesn't know (at writing time) the variables true name. This establishes a sort-of passing by reference (instead of the usual passing by value).
To give an example for what i mean, if you have this code:
then inside the function you "know" the name of your received variable - $1:
Whereas here:
You have not the same kind of knowledge because you cannot expand $1 but have to expand what $1 expands to.
In languages such as C you pass around pointers to data (or, in FORTRAN, you pass by reference) because you want to manipulate the data statically across functions: one function creates a list and passes a pointer to it (instead of the copied structure) to another function which adds a new element. Both functions work on the same data instead of one calling the other with a copy of the original (which would be lost when the function returns).
But in scripting languages there is no such thing like pass by reference and there is no such thing as common, static data you can manipulate. You can try to dot-execute all your functions to get there, but this would most probably lead to desaster quite fastly. So there is no real application for this kind of "indirect evaluation" other than perhaps supporting bad design. I am sure that - by reformulating the problem - it would perhaps be possible to come up with a simpler and cleaner solution which will not rely on double dereferencing to get to a variables content.
And there is one more problem: if you look closely at the second variation with the double dereference you will notice that it relies on the existence of a variable named "x" which is defined outside, in the calling function. This is a violation against the rule of encapsulation: you should use at one level only what you defined on that level, not something from the background environment, because once this environment changes you are in deep kimchi.
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)
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)
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)
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)
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)
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)
Dear all,
I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file.
The file length will depend on the numbers of Earnings/Deductions/Direct Deposits.
And
The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
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)
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)
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.
HOSTNAME# echo $TESTIT
+ echo THIS IS A TEST
THIS IS A TEST
HOSTNAME# ls -l
total... (10 Replies)