|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
[Solved] Variable Name as a Prompt
Dear Members, I have an variable by name dir.If i do echo $dir i will get the path (/usr/bin/). I am writing a shell script which will prompt to enter the variable name if run.Suppose the script name is test.sh. If run test.sh it will prompt for entering variable name which is dir.Suppose if i do echo $dir inside the shell script it is not giving me the path.looks like it passing the value entered as a literal string. How can i get the actual value of variable dir inside the script. Following is my code inside the shell script: Code:
VariableName()
{
echo "***************************************"
echo -n "Please Enter Variable Name : "
read Read_VariableName
}
VariableName
echo $Read_VariableNameThanks Sandeep |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Please post what Operating System and version you are running and what Shell you use. If I guess that you have a Shell which creates a sub-shell for a function then variables become local to that function. A common circumvention is: EDIT: This script does not work! Ignore it. Code:
VariableName()
{
echo "***************************************"
echo -n "Please Enter Variable Name : "
read Read_VariableName
echo "${Read_VariableName}"
}
VariableName | read Read_VariableName
echo "${Read_VariableName}"Last edited by methyl; 04-27-2012 at 10:14 AM.. Reason: Script error. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
I don't think that works in anything but ksh, since other shells will put the read in a subshell, too...
Another way to do so is a temporary file. |
|
#4
|
|||
|
|||
|
We are using Linux 2.6.9-78.0.13.0.1.ELsmp x86_64.
The shell is ksh. Thanks Sandeep ---------- Post updated at 10:19 PM ---------- Previous update was at 10:12 PM ---------- methyl, I tried your method but it is not working. The result is the same as input. If you have any other idea please let me know. Thanks Sandeep |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
Have a go with this: Code:
#!/usr/bin/env ksh
function VariableName
{
echo "***************************************" >&2
printf "Please Enter Variable Name : " >&2
read Read_VariableName
echo "${Read_VariableName}"
}
VariableName | read Read_VariableName
echo "${Read_VariableName}"Your main problem is that your echo commands, which you assumed were going to the tty, were going to the read in the main body of the script. By redirecting them to stderr, the prompt and other information go to the tty, while the echo goes into VariableName. You could also code Code:
Read_VariableName=$( VariableName ) since you are only capturing the output into a single variable. This would work in bash as well as kshell. The echo/printf commands still need to be redirected; it's just an alternate approach. I changed your echo -n to a printf command. Echo has so many different implementations that when I need to do something other than normal (e.g. adding the -n) I use printf. Just a preference of mine and no real bearing here. ---------- Post updated 04-27-12 at 00:10 ---------- Previous update was 04-26-12 at 23:51 ---------- I did have a go with your original script, and regardless of function definition style, function name, or name() , it works for me in both Kshell and bash. Given that your kernel seems pretty old, I'm wondering if you don't have a very old shell too. If you continue to have issues, it might be interesting to know what version of the shell you are running. Try excuting the command echo ${.sh.version} at the command line, or ksh --version . Last edited by agama; 04-26-2012 at 11:57 PM.. Reason: code tag issue |
| Sponsored Links | |
|
|
#6
|
||||
|
||||
|
WARNING! eval when used like this can be dangerous. See a few posts below for more info! Are you trying to do something like this: Code:
#!/bin/ksh
dir="/usr/bin"
VariableName()
{
echo "***************************************"
echo "Please Enter Variable Name : "
read Read_VariableName
}
VariableName
eval echo \$$Read_VariableName
exit 0
$ x
***************************************
Please Enter Variable Name :
dir
/usr/bin
$eval first replaces $Read_VariableName with "dir", then echo sees "$dir". Last edited by gary_w; 04-27-2012 at 02:32 PM.. Reason: Added warning about eval being dangerous when used like this. |
| The Following User Says Thank You to gary_w For This Useful Post: | ||
sandeep_1105 (04-27-2012) | ||
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
If the O/P has ksh as stated in a reply to one of the questions, the original script in post #1 should work. We need the O/P to check which Operating System and Shell is actually in use here.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Solved] Help needed to have changing value to the command prompt string variable PS1 | royalibrahim | UNIX for Dummies Questions & Answers | 2 | 04-26-2012 01:46 AM |
| Manipulating a variable using sed (solved) | pravintse | Shell Programming and Scripting | 2 | 06-13-2011 11:51 PM |
| [Solved] trying to install pecl without having it prompt for a yes m | sjsotelo | Shell Programming and Scripting | 1 | 10-06-2010 04:29 AM |
| [SOLVED] Scope of KSH Variable in Perl? | dahlia84 | Shell Programming and Scripting | 1 | 07-30-2010 11:48 AM |
| AWK variable in Main script??? solved | skyineyes | Shell Programming and Scripting | 0 | 04-06-2010 10:28 AM |
|
|