Solved

bash script to expand variables

Posted on 2014-09-30
3
360 Views
Last Modified: 2014-09-30
in a bash script, the user keys in a command with arguments
some of the arguments are variables defined in the bash profile
i need to run the command they enter
How does the script get the information and then does the whole command?

in the bash profile
  export TESTING=/home/programming

in the script, the user keys in: cd $TESTING/penguins

the script needs to: cd /home/programming/penguins

I am running RedHat EL 6.4
0
Comment
Question by:penguins_rule
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40353364
The script inherits the variables from the environment, you are setting the TESTING variable in the bash profile.

In the script you can use variables by using $VARIABLE_NAME or ${VARIABLE_NAME} in your case $TESTING or ${TESTING}

By doing: cd $TESTING/penguins in the script it is in fact doing: cd /home/programming/penguins

So looks OK to me ;)
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 40353405
Are the users prompted to enter a command, then they type in "cd $TESTING/penguins"?

If so, the command you need is "eval", as in:
echo "Enter your command:"
read cmd
eval $cmd

Open in new window

This is a bit dangerous (they could type in anything!).

I did think that there might be a specific problem with the "cd" command - if the command is run in a subshell, then the "cd" would have no effect, but in the above example it would actually change the directory.
0
 
LVL 1

Author Comment

by:penguins_rule
ID: 40353595
in my case, i am using the script to make sure they can only type in certain commands. Otherwise, i agree, it would be dangerous.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
The purpose of this article is to show how we can create Linux Mint virtual machine using Oracle Virtual Box. To install Linux Mint we have to download the ISO file from its website i.e. http://www.linuxmint.com. Once you open the link you will see …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question