Solved

bash script to expand variables

Posted on 2014-09-30
3
356 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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…
This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

734 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