Run command in cmd from Ruby - ruby

I know this is probably a really simple question, but I am having a really hard time finding a solution. I am working on windows in cmd. I am simply trying to use this command:
find /i \"chef_server_url\" C:/chef/client.rb
I know the command works. It simply returns a url in a file that I wanted to check.
I want to execute this in code and return the results in my code. But I am having a hard time formatting the string especially with the escape characters
I am using %x[] to do so. So ideally I would just say
%x[find /i \"chef_server_url\" C:/chef/client.rb]
But the c:/chef/client.rb will not format correctly because it thinks /c and /c are not supposed to be that way

Are you trying to access the Chef Server URL from inside Chef? You can use Chef::Config[:chef_server_url] but the deeper question is what you trying to do?

The short answer to your question is that there is more than one way to run a system command in ruby:
The Kernel#system method takes a plain string, so as long as you can get your command accurately represented in a string, it should work. Something like system 'find /i "chef_server_url" C:/chef/client.rb' should work.
Backticks will do similar, which should look something like this: find /i "chef_server_url" c:/chef/client.rb (wrap in `backticks`, I'm not sure how to format on StackExchange).
I'd first recommend getting a string representation of your command, puts it out, copy paste to a command window. If it works, then send it through system.
However, like others here, I suspect there is a better solution to the underlying problem you're trying to solve.
I'm not anywhere near a windows computer for testing, so you'll have to experiment with these.

Related

Is there a way CMD can open a folder with an emoji in its name by using os.execute in Lua 5.2?

As soon as I try to access a folder/file containing an emoji in its name from my Lua 5.2 script, for example like this:
os.execute('start "" "' .. path .. "\\scripts\\menu\\📄 My Scripts" .. '"')
The Windows' Command Prompt simply refuses to open it with the following error message:
I'm aware Windows' Command Prompt doesn't support emojis and therefore is not possible to make it work just like that, but my doubt is if won't exist some workaround or whatever I can do to ensure any Windows/Unix user is going to able to get the folder open by my Lua script without any problem.
I have tried i.e. things like use the codes instead (1246 and U+1F4F0 in this page facing up case) without success. Couldn't I for example simply use some kind of "wildcard" instead? I mean, knowing it's always going to be the very first character in the name. Or, well, any other ideas will be welcomed, cause nothing I'm trying really seems to work...
Of course if it's going to represent any problem I'll simply refuse to use them, but it came in handy for some "first sight" folder distinction and, if possible, I'd like to can count this little visual resource 🙄
This is a Problem about how the string is constructed.
I found only one solution with [[command "path"]] (on Windows 11 and Lua 5.3)...
os.execute([[start ]] .. path .. [["\scripts\menu\📄 My Scripts"]])
-- My Testpath is/was: os.execute([[dir "%localappdata%\nvim\📄 Lua"]])
...the long string ([[]]) will not be interpreted (coercionated) by Lua.
That also have the side effect that you can use single backslashs with that kind of string.
Environment variable expansion (e.g. Windows: %localappdata%) only works inside doublequotes.
Single quotes instead ([[command '%localappdate%\path\']]) will not work (expanded).
os.execute accepts only ANSI-encoded strings (win-1252 in European Windows), but it is unable to encode an emoji.
Hint: you can create .bat-file to do the task for you and invoke it from Lua with os.execute.

How to reference and run a python document from the python interpreter

I just want to be able to run a python script from the interpreter, so that I can work on my changes to my script in notepad or other editor, save, and then interactively test changed code in the python interpreter.
Also, IDLE is not a solution. I'm operating on a government computer that is blocking the port it uses to communicate interaction between console and module.
To clear up any confusion, here's a demonstration of what I'm trying to do:
So, how do I do it?
EDIT:
Okay so I found a statement that does what I want. exec(open('dir').read()). The problem I think is that the directory I want to refer to contains periods. But I'm sure this will work, because open('dir').read() produces a string of the contents of a document specified, as long as I reference the likes of C:\myTest.py, and exec() obviously runs strings as input. So how can I reference files from the location I want?
Okay so the problem seems to be that Windows addresses often contain what python sees as 'unicode exits'. I'm not sure what they do or how they work, but I know they start with \ and are followed by a single letter and that there are enough of them to use up half the alphabet. There are a few solutions but only one is worth a damn for this application. I came across an operator that can be used in conjunction with strings, similarly to how + can be used to concatenate multiple strings, it seems r or R if you prefer (interestingly), can be used immediately before a string to tell the interpreter to take the string 'literally' as a string, and nothing else.
One would think that the quotes would be enough to express this, but they aren't and I'll probably eventually find out why. But for now, here's the answer to my question. I hope someone else finds it useful:
In plain text: >>> exec(open(R'C:\Users\First.Last\Desktop\myScript.py').read())

Custom Input For Command Prompt?

What I'm looking for seems to be very simple. I want to run a command prompt line, but have it to be easily editable for anyone to use. There are multiple commands to this script, but only one part needs to be changed, and asks the user for a custom input. I'm sure this is already online, however after looking for hours and finding nothing as I have no clue what it would even be called, this is my last resort. Any help would be appreciated as I'm not even quite sure how to describe this.
Your question is a bit vague but if I understand correctly it sounds like you are trying to read user input (ie. prompt the user for input) from a batch file. If so, this should work for you:
set /p CustomInput="Please enter blah blah blah.."
Then you can use %CustomInput% as a variable in the script.
I guess you may also add this line:
goto %CustomInput%

Interact with console program via batch script

I want to start a console program via batch script. Starting the console program works fine. I am starting it via call xxx.exe para para. The problem is that the console program wants an input like that after it is started.
call xxx.exe para para
please type in password:_
Is it possible to make the input of the password from the batch script.
Whether you are using batch or bash, as it seemed originally, you could try this simple piping:
echo YourPassword| program.exe parameters...
Note that if it is indeed a batch script, it is vital to make sure there's no extra space between your password and the |, or it will be passed along with the password, as part of the password. In bash, if I'm not much mistaken, such a space would be disregarded (or maybe it would only be so if you enclosed the echoed string in quotation marks, I'm not entirely sure).
Anyway, the above doesn't always work, as some programs implement password reading in a way that disregards the input stream piped from another command.
You tagged your question "Windows" and "Batch" and asked about "batch" in the question. The answer to that question is: Yes, use set like this:
set /p password=please type in password:
If you're really asking about 'bash' shell, you should re-tag your question (and change the text).

BATCH: How to capture the extension of the file, or how to do a pattern test like regexp

I'm writing a batch file for windows command prompt, I want to loop through files in a directory recursively using FOR /R .
However, inside the loop I want to check if the file of extension .txt or possibly other extensions in order to skip.
How can I do that? in FORFILES there is #ext what to use with for?
Also, is there something similar to regexp in command prompt?
%~xV will expand variable V to an extension only. If you think you need regexp, then ditch the batch altogether, and use e.g. Python, otherwise you're only going to be writing unmaintainable mess.
Also, for the love of Cthulhu, console window is not even remotely related to MS-DOS.
Complementing Cat++ answer, for the second question, FINDSTR command accepts regular expressions as search strings.
Although it is not a full featured regexp command, for search and replace, it is pretty useful in many simple situations.
Try HELP FINDSTR
Or for an introduction and a list of the supported regex expressions by FINDSTR see this doc http://technet.microsoft.com/en-us/library/bb490907.aspx .

Resources