bash-shell: What is wrong with this code?

geifani

New Member
Credits
42
Hello all together,

What's wrong with my command?
I type this into my linux-terminal:
Code:
$ VBoxManage guestcontrol "NameOfMyVM" start --username UserNameOfMyWindowsLogin  --password PasswordOfMyWindowsLogin --exe "%SystemRoot%\system32\cmd.exe" -- 'cmd' /k start "run this" "for /f "usebackq skip=1 tokens=3 delims= " %x in (`reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.docx\UserChoice" /v progid`) do set BUFFER=%x& call vboxcontrol guestproperty set /myproperties/myproperty %BUFFER:*Word=%"
This is the output:
Code:
bash: reg: Command not found
[email protected]:~
$


The code has two parts:

The first part consists of this:
Code:
VBoxManage guestcontrol "NameOfMyVM" start --username UserNameOfMyWindowsLogin  --password PasswordOfMyWindowsLogin --exe "%SystemRoot%\system32\cmd.exe" -- 'cmd' /c start "run this"
I use a VirtualBox VM. When my windows guest is running, then this VirtualBox command (typed into a Linux terminal in my Linux host) opens a cmd-window in the windows-guest. And it runs the cmd command, that follows <"run this">, in my guest. ("run this" will be the title of the cmd-window). This first part is bash-shell-code.


The second part consists of this:
Code:
"for /f "usebackq skip=1 tokens=3 delims= " %x in (`reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.docx\UserChoice" /v progid`) do set BUFFER=%x& call vboxcontrol guestproperty set /myproperties/myproperty %BUFFER:*Word=%"
This second part is not bash-shell, but cmd batch-code.

A)
The code of the second part is tested in the windows cmd-batch-window and it works. Concrete: When I open cmd in windows and type directly in, the cmd batch-code of the second part, then this code works.
--> This means the second part should work and is OK.

B)
The bash-shell-code of part 1 also works, when I take some other cmd-batch code for the second part. For example:
Code:
"winword.exe"
To be more concrete, when "winword.exe" follows to "run this" in part 1, then this combination of commands works. To be concrete: Running the whole command, consisting of part 1 and "winword.exe" as "part 2", in a Linux host terminal, starts Winword in the windows-guest.
--> This means the first part of the code also should work.

This looks like a contradiction: Each part works in some cases, but not in the combination I would like to have.

Would appreciate some help. Thank you.
 
Last edited:


gvisoc

Active Member
Credits
1,173
As the error emmiter (bash) says, you are executing the `reg ...` part in bash, but you should review it because it is indeed written in bash syntax. This symbol ` is a deprecated bash construct to evaluate commands (now it's with $(...)). To evaluate commands in cmd, the syntax may be different (I don't know what that might be).

Aside from the syntax, another problem is that instead of remaining inside the VM, your script is executing that sentence in your Linux host, it has exited the VM environment and given back control to the host bash. Check for accidentally closed double quotes; indeed, I would suspect that this specific part is the cause for it:

Code:
"for /f "usebackq ...
I think that second double quotes is breaking the second part of your code, as they are closing the first ones, and you're executing an empty for loop. I don't have now a Windows VM to reproduce it, but I'd say that you need to escape (\") a couple of double quotes in your second part.
 
Last edited:

geifani

New Member
Credits
42
Hello @gvisoc
Thank you for the hint with double quotes.
-> The whole string should be in '...' instead of "..." .

This works:
Code:
[email protected]:~
$ BefehlRegAuslesen='for /f "usebackq skip=1 tokens=3 delims= " %x in (`reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.docx\UserChoice" /v progid`) do set BUFFER=%x& call vboxcontrol guestproperty set /myproperties/myproperty %BUFFER:*Word=%'

[email protected]:~
$ VBoxManage guestcontrol "Win7" start --username XX  --password XX --exe "%SystemRoot%\system32\cmd.exe" -- 'cmd' /c start "run this" $BefehlRegAuslesen

But unfortunately I have now another problem.
 

geifani

New Member
Credits
42
Sorry. I was wrong. I thought, that the command in post #3 would work, because I got no message. But it's wrong. In the meantime I have realized, that it does still not work this way.
 

JasKinasis

Well-Known Member
Credits
5,390
Maybe try putting all of the windows specific shell-script into a .bat script? Then when you start the windows7 vm, invoke cmd to run the .bat file instead?!
 

geifani

New Member
Credits
42
I want to make it as easy as possible for the user who wants to set up his VM.
With your suggestion the user has in addition to copy a batch file into the windows guest.

(But I want a solution, which is completely maintenance-free for the user.)
 

Members online


Top