Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

bash script to expand variables

Posted on 2014-09-30
3
Medium Priority
?
364 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 20

Accepted Solution

by:
simon3270 earned 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
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.
Suggested Courses

730 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