|
Author |
Message |
|
|
Posted : Thu, 10 April 2008 16:07:01
Subject :
Command not executing from shell variable
Hi All !
I am using Fedora 8, I tried to store a linux command into a shell variable and tried to execute that from the script. The code i have written is
test="dialog --title \"MONyog Build Utility\" --backtitle \"MONyog Build Script\" --infobox \"hello world\" 16 20"
bash "$test"
but I am getting an error saying "No such file or directory". If i copy the same text and execute from the $prompt I am able to view the dialog box, the only problem is when I am trying to execute it from the script.
I am not able to trace where I am missing in the code, so can anyone please help me!!
Thanks
Supratik
|
|
|
|
Shashank Sharma
|
Posted : Sun, 13 April 2008 19:35:20
Subject :
Command not executing from shell variable
Maybe you should read some BASH tutorials and get a hang of shell variables:
http://www.hypexr.org/bash_tutorial.php
http://tldp.org/LDP/abs/html/
http://www.linuxtutorialblog.com/post/tutorial-the-best-tips-tricks-for-bash
http://csdir.org/tutorials/bash-tutorial/
|
|
anup
|
Posted : Mon, 14 April 2008 12:57:31
Subject :
Command not executing from shell variable
Check the permission given to the script,if it is only -rw-r--r-- then script will not work.
use chmod 755 script_name
then execute ./script_name
While executing script the script should be in pwd.
|
|
supratik
|
Posted : Mon, 14 April 2008 19:47:22
Subject :
Re: Command not executing from shell variable
I have given the execution permission to the script. but still the problem persists in the script
If I include the following two lines in the code, it works,
cmd="ls -l"
$cmd
but it is not working for the same type of statements
test="dialog --title \"MONyog Build Utility\" --backtitle \"MONyog Build Script\" --infobox \"hello world\" 16 20"
$test
The following error it thows
Error: Unknown option Build.
Use --help to list options.
Please help me !
Regards
Supratik
|
|
griff5w
|
Posted : Fri, 18 April 2008 14:46:37
Subject :
Re: Command not executing from shell variable
spuratik:
Without seeing the 'Big Picture' I am not sure why you are putting the dialog into a variable as opposed to a function. What are you trying to accomplish by using a variable instead of a function?
griff5w
|
|
Peter Jakobi
|
Posted : Fri, 18 April 2008 16:22:33
Subject :
Re: Re: Re: Re: Re: Re: Command not executing from she
[quote=supratik]
I am using Fedora 8, I tried to store a linux command into a shell variable and tried to execute that from the script. The code i have written is
test="dialog --title \"MONyog Build Utility\" --backtitle \"MONyog Build Script\" --infobox \"hello world\" 16 20"
bash "$test"
[/quote]
First simplify and try some variants:
D='dialog --title x --backtitle y --infobox z 16 10'; $D # ok
D='dialog --title "x z" --backtitle y --infobox z 16 10'; $D # bug
Strange, isn't it?
Well, welcome and enjoy your visit to the Quoting $Hell:
The problem is the way word splitting and variable expansion is performed by the shell.
Work arounds are:
a) use perl (python, ruby) with saner quotation and the ability to safely pass arrays to system/exec (e.g. system @command;).
b) D='dialog --title "x z" --backtitle y --infobox z 16 10'; eval $D.
--------------- ^^^^^ you want to (re)parse/split because of "x z"
For more info, check man bash or one of the better korn shell books.
[Modified by: Peter Jakobi on April 18, 2008 05:34 PM]
[Modified by: Peter Jakobi on April 18, 2008 05:35 PM]
|
|
Shashank Sharma
|
Posted : Fri, 18 April 2008 17:40:18
Subject :
Command not executing from shell variable
First of all, you don't need to escape where you have in your script. This works perfectly:
test="dialog --title "MONyog" --backtitle "MONyog" --infobox "hello" 16 20"
$test
The above works without any problems. For some reason, when you use a variable, you can't use more than one word for the title, backtitle and infobox. As a command on BASH, you can do:
dialog --title "MONyog Build Utility" --backtitle "MONyog Build Script" --infobox "hello world" 16 20
and there wouldn't be any problem. But in a variable, like you're trying to do, you can't use spaces and more than one word. So, I tried to escape the spaces, like so:
test="dialog --title "MONyog\ Build" --backtitle "MONyog" --infobox "hello\ world" 16 20"
$test
But this doesn't work either. You just need to figure out a way to handle multiple letters and spaces with variable in a shell script and that will solve the problem.
|
|
Anand
|
Posted : Fri, 18 April 2008 19:42:41
Subject :
Re: Command not executing from shell variable
This one seems to work:
TEST="dialog --title \"MONyog Build Utility\" --backtitle \"MONyog Build Script\" --infobox \"hello world\" 16 20"
eval $TEST
|
|
supratik
|
Posted : Sat, 19 April 2008 19:02:23
Subject :
Command not executing from shell variable
Hi ! Thanks guys. It worked, i tried with what every one has suggested.
Thanks once again.
-------------------------
Regards
Supratik
|