LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 06-01-2012, 05:53 AM   #1
psrdotcom
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 06-01-2012, 06:26 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
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.
 
Old 06-01-2012, 08:41 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
Old 06-01-2012, 04:34 PM   #4
psrdotcom
LQ Newbie
 
Registered: Sep 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Solved

I have wrote the command to a file and executed the file within the script.


Quote:
Originally Posted by David the H. View Post
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
 
  


Reply

Tags
command, run, shell script, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] environmental variable in bash command string cytmtn Linux - Newbie 6 02-09-2011 04:18 PM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 06:21 PM
how to format the output of a command and save it into a string variable gn00kie Programming 1 07-15-2006 04:04 AM
Bash Shellscript automating command input JoeneB Programming 3 01-13-2006 02:59 AM
how to execute mail command in a shellscript mema Programming 1 10-30-2003 03:58 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration