Lua os.execute() does not work - eclipse

I'm having a problem with the Lua os.execute() command.
I just want to echo a word and write it into a file, like echo 'aword' > C:\folder\tempworkspace\foo to try the os.execute() command. The direcory C:\folder\tempworkspace exists, "foo" is the name of the file I want the command to create and fill with "aword".
Later, when this works, I'd like to call R, using R -q -e "rbinom(1000,1,0.7)" > C:\folder\tempworkspace\foo.
I've already tried all the advice provided in
Lua programming - os.execute() is not working in Windows and Why won't applications in Program Files run using os.execute in lua?
but my problem seems to be a different one, maybe not even in the syntax, but somewhere else?
When I type those command directly in the Command Prompt, they work. I use Windows 7 Professional as an administrator, and Lua 5.1.4 with Eclipse.
Here is what I have tried so far:
os.execute("echo 'hehe' > C:\folder\tempworkspace\foo")
os.execute [["echo 'hehe' > C:\folder\tempworkspace\foo"]]
os.execute [["echo 'hehe' > 'C:\folder\tempworkspace\foo'"]]
os.execute [[echo 'hehe' > C:\folder\tempworkspace\foo]]
os.execute [[echo 'hehe' > C:\\folder\\tempworkspace\\foo]]
os.execute[[cmd.exe /c echo 'hehe' > C:\folder\tempworkspace\foo]]
os.execute("cmd.exe /c echo 'hehe' > C:\\folder\\tempworkspace\\foo")
os.execute("cmd.exe /c echo 'hehe' > 'C:\\folder\\tempworkspace\\foo'")
I'd be very grateful for any suggestions to improve my code. (Note: I'm writing to a file, because I want to use the output later in lua. Another way of doing this, using io.popen() has been suggested somewhere, but it is said to be platform dependent, anyway my Lua crashes when I try to use x = io.popen("R -q -e 'rnorm(10)'")).
Edit, after first answer:
Your sendMsg function somehow also does not work on my computer, and I don't get any error, I really wonder what's the problem. For pcall, am I doing this right? As the line print(err) does not print anything I wonder if I am doing correctly.
function sendMsg(cmd, msg, fpath)
local output = cmd.. " ".. msg.. " > ".. fpath
print(output)
os.execute(output)
end
function sendMsgArgs()
sendMsg("echo", "huhu", "C:\\merret\\tempworkspace\\foo");
end
err = pcall(sendMsgArgs)
if err == true then
print("THIS WORKED")
else
print("THIS DID NOT WORK")
print(err)
end
Edit: This was actually a Eclipse/editor issue. So I wanted to add some tags, such as lua-eclipse, but I can't.

it's been a while, but finally (thanks to a colleague), I figured out that the problem was not the Lua code, but actually executing Lua from Eclipse (I'm using the Lua Eclipse plugin).
Executing a lua file with the code
x = os.execute('R -q -e rnorm(10) > C:/folder/something/foo')
print(x)
from the command prompt or using Crimson Editor (see below), it worked perfectly, and returned status code 0 (instead of -1 when executing from Eclipse).
Thanks a lot to all helpers anyway!
That Thing
PS: For anyone who is interested, I'm using Lua as part of the TerraME environment, http://www.terrame.org/doku.php. There is also a description there how to use Crimson Editor.

this snippet of code works on my Win7 box:
function sendMsg(cmd, msg, fpath)
local output = cmd.. " ".. msg.. " > ".. fpath
print(output)
os.execute(output)
end
sendMsg("echo", "hehe", "C:\\path\\to\\foo");
you need to make sure the file exists before you attempt to write to it. are you running this from the lua interpreter? do you get any error messages back when it fails to do the write?
alternatively - you can just use io.open() and write that way. wrapping this in a function and calling it using pcall() may give you more information if you have some sort of windows issue opening/reading to that location.
local fout = io.open("C:\\path\\to\\foo", "w+")
fout:write("hehe")
fout:close()
an example using pcall:
local result, error = pcall(sendMsg, "echo", "huhu", "C:\\merret\\tempworkspace\\foo")
if result == false then
print(error)
else
print("success!")
end

If the path you're trying to write to contains a space, it must be surrounded by double quotes under Windows; single quotes will throw an error.
As such, the following works fine for me:
os.execute([[ echo test > "C:\\Program Files\\xyz.txt" ]])

Related

zsh: command not found: foo

I'm learning the power of Vagrant, and I have stumbled upon this problem. I am trying to create a text named foo.txt with the text "foo" inside of it.
What I type into the terminal is this:
user#User-MBP data % "foo" > foo.txt
Terminal says:
zsh: command not found: foo
Has anyone encountered this? Thank you in advance for your help!
You can't just throw a string at the file like that. You need to use a program like echo to throw it for you.
echo "foo" > foo.txt
To be a little more clear about why, run man echo.
The echo program "writes arguments to the standard output". Whatever argument you give it (i.e. "foo") it will write to standard output.
You follow that with the > operator which "redirects standard output". Read about that here.
You then specify a file to "catch" the redirected standard output (i.e. foo.txt) which you already did just fine.

How can I save an interaction with a command line program to a file?

I need to create output files that include the input I'm providing. For example, a run of the program might look like:
Input command: do_things
Things have been done.
Input command: stop_doing_things
Things are no longer being done.
Where "do_things" and "stop_doing_things" are input from the user.
How can I output all of the above to a file using command prompt functions?
It's not clear what environment using "script " command linux will open a new shell and save everything done it to
This works for you, if you run it at last...
CMD > D:\mycmdout.txt
In that case, maybe you can capture your input as a variable. Echo the variable into the >> mycmdout.txt, then procede with the actual commands, again piped into >> mycmdout.txt as Sunny suggested.
SET /P variable=EnterInputHere
echo %variable% >> mycmdout.txt
EDIT: Be sure to use double >> as to append result to file.

How to empty a file in fish?

In bash shell scripting, I would typically run :> file to empty a file.
Now using fish, things are slightly different and the above command doesn't work.
What is fish equivalent?
Although it's not as short as :, true is a command that will work everywhere and produces no output:
true > file
Probably the easiest way that will be work in both Fish and Bash is to do echo "" > file
EDIT: Commenter was absolutely right echo "" > file produces a file with a newline, the correct command I was thinking of to create an empty file is cat /dev/null > file.
There is, and always was the magic method called touch which set change time to actual or create non-existent file. For compatiblity I suggest you to use this way in all scripts that you write (even if you write bash code).

How do I start a Windows program with spaces in the path from Perl?

If I do:
my program = "C:\\MyPath\\MyProg.exe";
system(("start", $program));
MyProg starts up just fine and my script resumes after the system() command. But if there are spaces in the path like
my program = "C:\\My Path\\MyProg.exe";
system(("start", $program));
It seems to run cmd, not MyProg.
I've tried quoting with things like:
my program = "C:\\My Path\\MyProg.exe";
system(("start", '"' . $program . '"'));
But nothing seems to help.
Of course I can get around it with fork() but I'd like to understand why I can't pass a path with spaces as an argument.
That's because the built-in start command is a bit weird when it comes to quotes. You can reproduce this on the command line with start "C:\My Path\MyProg.exe" and see the same result. To properly execute it you need a set of empty quotes before it: start "" "C:\My Path\MyProg.exe".
So your end result should be:
my program = "C:\\My Path\\MyProg.exe";
system('start "" "' . $program . '"');
Edited to include the suggesstion from ikegami. My perl is a bit rusty as I haven't used it in years.
Try ...
my program = "C:/\"My Path\"/MyProg.exe";
I am not an perl expert but I found the following link.
http://bytes.com/topic/perl/answers/697488-problem-system-command-windows

Best way to open an URL in Vim

Lets say that you have either URL or a link on a webpage that is a text file. How would be the easiest way for the user to be able to open that file in a Vim?
Right click and save link as?
Use a specific file extension that defaults to Vim?
Command line command?
Depending on how your vim binary was built you can just give vim the url:
vim http://www.google.com/
Vim spawns curl and grabs the file, then opens it.
Assuming you want to just open a link in vim, how about this:
curl http://www.google.com | vim -
EDIT
to make this command easier you can always user your browser of choice's "Copy link address" option.
EDIT
Given #speshak's answer and my own, I would say the "easiest" way would be option 3, "a command line command".
Solution 1: use command
" gvimrc
for g:chrome_exe in [
\'C:\...\Google\Chrome\Application\chrome.exe',
\]
if !filereadable(g:chrome_exe)
continue
endif
command -nargs=+ URL :exe "silent !start ".g:chrome_exe." <args>"
break
endfor
Now when you type: :URL https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en
it will open google news
Solution 2: use function
or if you have a file that records a lot of URLs, and you want to use hotkey to open it, then you can try in this way
" .gvimrc
let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
normal "*yy
" let result = getreg("x")
" return result
:execute "silent !start ".g:chrome_exe2." ".getreg("*")
endfunction
map ,url :call OpenURL()<CR>
and then, you can open it with ,url
" test.txt
https://www.google.com/
https://news.google.com/topstories?hl=en-US&gl=US&ceid=US:en
Explanation of command
URL is a name, choose by you. (remember User-defined commands must start with an uppercase letter)
what is the command
command -nargs=+ Say :echo "<args>"
Now when you type :Say Hello World
Vim echoes "Hello World".
nargs
-nargs=0 No arguments
-nargs=1 One argument
-nargs=* Any number of arguments
-nargs=? Zero or one argument
-nargs=+ One or more arguments
I have used links before since RedHat days. The command would be
links http://www.google.com
If links is not installed, you can do sudo apt-get install links on Ubuntu 14.04 LTS to install it.
Hope it helps.