Linux.com

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: 1334
Location:New Delhi, India

Other Topics
Posted: Apr 13, 2008 7:35:20 PM
Subject: Command not executing from shell variable
Back to top Profile Email Website Yahoo!
anup
Joined Apr 05, 2008
Posts: 5

Other Topics
Posted: Apr 14, 2008 12:57:31 PM
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.

Back to top Profile Email Website
supratik
Joined Apr 10, 2008
Posts: 9

Other Topics
Posted: Apr 14, 2008 7:47:22 PM
Subject: 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

Back to top Profile Email Website
griff5w
Joined Aug 02, 2007
Posts: 1

Other Topics
Posted: Apr 18, 2008 2:46:37 PM
Subject: 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

Back to top Profile Email Website
Peter Jakobi
Joined Jun 21, 2007
Posts: 7

Other Topics
Posted: Apr 18, 2008 4:22:33 PM
Subject: Command not executing from shell variable

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"

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: 1334
Location:New Delhi, India

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

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
Subject: 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

Back to top Profile Email Website
supratik
Joined Apr 10, 2008
Posts: 9

Other Topics
Posted: Apr 19, 2008 7:02:23 PM
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

Back to top Profile Email Website
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya