Solved

bash script to expand variables

Posted on 2014-09-30
3
330 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
3 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
Comment Utility
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
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now