[Solved] Variable Name as a Prompt


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Variable Name as a Prompt
# 1  
Old 04-26-2012
[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_VariableName


Thanks
Sandeep
# 2  
Old 04-26-2012
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 11:14 AM.. Reason: Script error.
# 3  
Old 04-26-2012
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  
Old 04-27-2012
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
# 5  
Old 04-27-2012
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-27-2012 at 12:57 AM.. Reason: code tag issue
# 6  
Old 04-27-2012
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 03:32 PM.. Reason: Added warning about eval being dangerous when used like this.
This User Gave Thanks to gary_w For This Post:
# 7  
Old 04-27-2012
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.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to prompt user to define a variable?

Hi everyone, Is it possible to define blank vaianbles and prompt user to fill them during the script execution? A very simple example: INPUT= OUTPUT= cut -f1-4 $INPUT | sed 's/hello/goodbye/g' | sort -uV > $OUTPUTThank you in advance! Best wishes (2 Replies)
Discussion started by: lsantome
2 Replies

2. Shell Programming and Scripting

[Solved] Help with Overriding a Prompt in UNIX/Java

I am executing a shell script which contains a jar call to an external java package for which I don’t have a read access to. The external package was written in such a way that whenever we make a java –jar call to the package, it shows a prompt on the console asking if we want to continue or no... (1 Reply)
Discussion started by: Harry1302
1 Replies

3. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

4. Shell Programming and Scripting

[Solved] Prompt problem while using mget to ftp

I am writing a scritp in which first step is to get some files from a server. I am using mget to do that. here is my FTP code ... HOST="XXX.XXX.com" 28 ftp -inv $HOST <<END &> $FTP_LOG 29 quote USER $USER 30 quote PASS $PASWD 31 cd log 32 prompt off 33 binary 34 mget... (0 Replies)
Discussion started by: shashidhar
0 Replies

5. Shell Programming and Scripting

[Solved] Running scripts in parallel that issue prompt

Hi all - I am totally stuck here :wall I have been asked to write a shell script that does a few little things and then reads from a config file and kicks off an instance of another script, say scriptB.ksh for each line in the config file. These should all be run in parallel. This is all fine but... (2 Replies)
Discussion started by: sjmolloy
2 Replies

6. Shell Programming and Scripting

[Solved] reassign value to variable

Hi, Please let me know how to reassign value to a variable.The calling script is passing parameter as HAT_DIV but I like to pass HAT DIV ( two words) to DIV parameter.These are .ksh scripts. # access to target - (user passwd sid) must be provided. USER=$1; ... (5 Replies)
Discussion started by: sandy162
5 Replies

7. Shell Programming and Scripting

zsh, prompt, variable expansion, ANSI color sequences

Hi! I am using latest ZSH and I have setopt prompt_subst I have a global hash array variable which contains some color definitions with color names as keys and 256-color ANSI codes (with %{ and %}) as values, eg %{\ePS1="$FG Hello World (%M) > " or PS1=$'$FG Hello World (%M) > ' then it... (3 Replies)
Discussion started by: galanom
3 Replies

8. UNIX for Dummies Questions & Answers

[Solved] Help needed to have changing value to the command prompt string variable PS1

Hi, I am using git bash terminal window to do git operations. I have set the prompt string variable PS1 in the ~/.bashrc file as follows: export PS1=" " This is intended to show me the current git branch's name which is active as part of the prompt string. But, the problem is when I do a git... (2 Replies)
Discussion started by: royalibrahim
2 Replies

9. Shell Programming and Scripting

Manipulating a variable using sed (solved)

Hi, My variable has value as this: tvar1="bool_risk_enabled" Boolean "true" Now I need to replace this true with false. Which is the best way to do this? Can we do this with sed command? Please help me. ---------- Post updated at 05:23 PM ---------- Previous update was at 05:00 PM... (2 Replies)
Discussion started by: pravintse
2 Replies

10. Shell Programming and Scripting

[Solved] trying to install pecl without having it prompt for a yes m

I want to install pecl from a single command without it asking me for a response of "yes" or "no" to finish the install... -------------------------------------------------------------------------- # pecl install pecl_http downloading pecl_http-1.7.0.tgz ... Starting to download... (1 Reply)
Discussion started by: sjsotelo
1 Replies
Login or Register to Ask a Question