[Solved] Variable Name as a Prompt


 
Thread Tools Search this Thread
# 8  
Hi Gary,

Your Method worked. Thanks a ton for your help.

Thanks
Sandeep
# 9  
I was thinking about this during lunch and realized this is very dangerous though. The way it is written right now, with eval working on user input with no validation, it would execute a command passed to it. You need to be careful and add validation to allow only one of a known list of variables to be entered. Or look for a different way to accomplish the same thing. Try this to see for yourself:
Code:
$ x
***************************************
Please Enter Variable Name :
`ls -lar`
$total 8 -rwxrwx--x 1 user1 zx 233 Apr 27 09:13 x drwxr-xr-x 21 user1 zx 2048 Apr 23 16:02 .. drwxrwx--- 2 user1 1024 Apr 25 10:37 .
$

Someone devious could wreak havoc!

In this case, devious.sh just runs a shell script that does a query to the database, but you see how this could be very dangerous:
Code:
x
***************************************
Please Enter Variable Name :
`devious.sh`
$ SYSDATE ----------- 27-APR-2012
$


Last edited by gary_w; 04-27-2012 at 03:37 PM..
# 10  
It would be safer to assign the values to an associative array making eval unnecessary and allowing for fairly simple verification of a valid name entered.

Code:
#!/usr/bin/env ksh

function prompt
{
    read vname?"Enter a variable name: "
    return 0
}

typeset -A paths
paths[dir]=/tmp/dir
paths[bar]=/tmp/bar
paths[foo]=/tmp/foo

while prompt
do
    if [[ -z $vname ]]
    then
        exit
    fi

    if [[ -z ${paths[$vname]} ]]
    then
        echo "not a valid variable name: $vname"
    else
        echo "the path associated with $vname is: ${paths[$vname]}"
    fi
done

This User Gave Thanks to agama For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
The proof for the Chinese Remainder Theorem used in Number Theory was NOT developed by its first publisher, Sun Tzu.
True or False?

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

Featured Tech Videos