Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-01-2012, 05:53 AM
|
#1
|
LQ Newbie
Registered: Sep 2011
Posts: 5
Rep:
|
Shellscript variable as command string
Hi Friends,
When I was trying form a command from different variables to one variable, I was successive.
Sample:
CMD="mount"
CMD=$CMD" /dev/sdb"
CMD=$CMD" /mnt/disk0/"
echo "The command is: $CMD"
$CMD
Output:
The command is: mount /dev/sdb /mnt/disk0
Error: No such file or directory:
""
Note: Both the /dev/sdb and /mnt/disk0 exists.
If I am running the same command without using variable from the script, it was working fine.
Please help me to get out of this problem.
|
|
|
06-01-2012, 06:26 AM
|
#2
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
seems to be a quoting issue. Can you try the following
Code:
CMD="mount"
CMD="$CMD /dev/sdb"
CMD="$CMD /mnt/disk0/"
echo "The command is: $CMD"
$CMD
Evo2.
|
|
|
06-01-2012, 08:41 AM
|
#3
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
The simple answer here is that you don't do it that way. Variables are for storing data, not code.
I'm trying to put a command in a variable, but the complex cases always fail!
http://mywiki.wooledge.org/BashFAQ/050
Once a string is stored in a variable, the parsing rules for it are very different, and you just can't reliably run a complex command that way. If you want to set up a dynamic command of some kind, use a function.
Code:
CMD() {
echo "The command is: mount $@"
mount "$@"
}
CMD /dev/sdb /mnt/disk0
Last edited by David the H.; 06-01-2012 at 08:45 AM.
Reason: added code
|
|
1 members found this post helpful.
|
06-01-2012, 04:34 PM
|
#4
|
LQ Newbie
Registered: Sep 2011
Posts: 5
Original Poster
Rep:
|
Solved
I have wrote the command to a file and executed the file within the script.
Quote:
Originally Posted by David the H.
The simple answer here is that you don't do it that way. Variables are for storing data, not code.
I'm trying to put a command in a variable, but the complex cases always fail!
http://mywiki.wooledge.org/BashFAQ/050
Once a string is stored in a variable, the parsing rules for it are very different, and you just can't reliably run a complex command that way. If you want to set up a dynamic command of some kind, use a function.
Code:
CMD() {
echo "The command is: mount $@"
mount "$@"
}
CMD /dev/sdb /mnt/disk0
|
|
|
|
All times are GMT -5. The time now is 12:47 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|