Command not executing from shell variable

Forum Index » Forums » Programming and Development
Author Message
Joined: Apr 10, 2008
Posts: 9
Other Topics
Posted Apr 10, 2008 at 4:07:01 PM
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
Back to top Profile Email Website
Shashank Sharma
Joined Jan 01, 1970
Posts: 1657
Location:New Delhi, India

Other Topics
Posted: Apr 13, 2008 7:35:20 PM
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/

Coauthor of Beginning Fedora: From Novice to Professional published by Apress.

Please follow the Forum Guidelines

Back to top Profile Email Website Yahoo!
anup
Joined Apr 05, 2008
Posts: 5

Other Topics
Posted: Apr 14, 2008 12:57:31 PM
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.
Back to top Profile Email Website
supratik
Joined Apr 10, 2008
Posts: 9

Other Topics
Posted: Apr 14, 2008 7:47:22 PM
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
Back to top Profile Email Website
griff5w
Joined Aug 02, 2007
Posts: 1

Other Topics
Posted: Apr 18, 2008 2:46:37 PM
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
Back to top Profile Email Website
Peter Jakobi
Joined Jun 21, 2007
Posts: 7

Other Topics
Posted: Apr 18, 2008 4:22:33 PM
[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]
Back to top Profile Email
Shashank Sharma
Joined Jan 01, 1970
Posts: 1657
Location:New Delhi, India

Other Topics
Posted: Apr 18, 2008 5:40:18 PM
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.

Coauthor of Beginning Fedora: From Novice to Professional published by Apress.

Please follow the Forum Guidelines

Back to top Profile Email Website Yahoo!
Anand
Joined Apr 18, 2008
Posts: 1

Other Topics
Posted: Apr 18, 2008 7:42:41 PM
This one seems to work: TEST="dialog --title \"MONyog Build Utility\" --backtitle \"MONyog Build Script\" --infobox \"hello world\" 16 20" eval $TEST
Back to top Profile Email Website
supratik
Joined Apr 10, 2008
Posts: 9

Other Topics
Posted: Apr 19, 2008 7:02:23 PM
Hi ! Thanks guys. It worked, i tried with what every one has suggested. Thanks once again. ------------------------- Regards Supratik
Back to top Profile Email Website
Forum Index » Forums » Programming and Development