odd batch file programming error :Invalid argument/option: - 'eq' - windows

I have a line of BAT code written like this:
tasklist /FI "IMAGENAME eq WerFault.exe"|find /C "WerFault.exe" > temp.txt
It went on well for quite a few days but suddenly I got this error message:
error :Invalid argument/option: - 'eq'
The code hasn't been changed before and after this error message, I can't figure out what's the problem.

You, or people coming here from a search, may have been trolled by text encoding madness and the borderline sabotaging behavior of cmd.exe, combined with websites such as this one.
What they did:
Omit one quotation mark
Replace the other quotation mark with Unicode character 201c
What's probably going on:
The user copy-pastes the line and gets an error
The user notices the missing quotation mark and adds it
It now looks identical to the other quotation mark, so there's no way to see what is going on.
Not only does it look identical, copy-pasting it back out, such as to Stackoverflow, replaces it with a standard, undirected double quotation mark! In other words, it is indistinguishable from the correct symbol even when copying it back out of the window for analysis!
The above is true even for copy-paste within cmd (which conveniently doesn't work with standard hotkeys) but not for bringing back a line via the up-key.
What I have once again learned from this:
Do not trust what you see when Unicode is involved.
Also, cmd.exe is generally not very safe or sane. When in doubt, re-type the command manually.
This might not be the cause here, but if others come here for the same reason as I, maybe this answer will save them a few minutes.

Related

Ruby - stop program from executing in certain way

I wrote a parser, which recognizes elements of text based on certain pattern.
My program is able to recognize paragraph, chapter etc. The problem is it shouldn't recognize elements, when there's a quote. For example:
Paragraph 1
Something here...
would be proceed as Paragraph.
And:
Paragraph 1
"Paragraph 2"
shouldn't. But as my program is based on regexp patterns, it looks for the word "Paragraph". I'm going line by line and recognize patterns for each line. I don't know how to tell my program: if you see quotes mark, leave text alone without doing anything? My mentor told me to use raise, but I'm not sure how to do it.
OK, so I'm still a bit of a beginner, I don't know if there is a way to direct the regex to ignore things inside quotes, but if I wanted to solve this problem, I would first make a copy of the text to be parsed, run a regex over that and delete everything inside quotes, then run the parser over the remaining text.
A bit kludgy and inelegant I admit, and may have performance issues over a large enough text, but it would get the job done.
See HERE for link to documentation of ruby regex. About a third of the way down it discusses quotes:
/\p{Pi}/ - 'Punctuation: Initial Quote'
/\p{Pf}/ - 'Punctuation: Final Quote'
You may be able to bake that into the regex with the ^ to direct it to ignore items in quotes.

Run command in cmd from 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.

Syntax error, unexpected end-of-file

I have a script that is throwing this error.
This usually means there is a loop (like an if or do) that is not correctly ended, or there are too many end clauses. I can't find the issue. Any good tips on how to identify this kind of syntax error?
It could also be a double-quote issue. Wondering if there is a way (in ultra-edit or text editor) to detect lines of script that have un-even numbers of double quotes.
In answer to: "It could also be a double-quote issue, possibly. Wondering if there is a way to detect any lines of script (in ultra-edit or text editor) where there are an un-even number of double quotes."
Sublime is a great editor that is available for most platforms.
For the first question, comment out blocks of code using =begin ... =end and/or # ... and narrow down the error.
For the second question, use syntax highlighting on the text editor. You can easily tell how long a single string literal is continued, and find unbalanced quotes.
Never mind, I found the issue. I commented out the newest definition that I had added and it ran. That let me know it was that definition. I then took that out and went through it with a darn comb. Found that I was checking a value, but hadn't allowed for it to be nil or empty. Added that in and now I'm good.

cygwin terminal prompt color causes havoc

I am sorry because this is not really a programmer question (well it becomes one when my bash script needs to change the prompt color), but this inconvenience has been bugging me ever since I started testing my code on Cygwin. When I type a very long command line with a lot of arguments, it breaks to the next line after 61 characters, not at the end of the line (80 in my case), as you would expect it to. When you use backspace, home, end, or left/right arrow keys, the cursor doesn't move as it should. Also, if one of your previous commands in your command history is long (longer that 61 characters), pressing up arrow key after reaching that command will produce a scrambled line. I first though this was a bug in Cygwin and tried to ignore it. After some (very long) research, I found out that the problem was in my profile file, and further research showed that the problem is caused by my prompt coloring.
Apparently, this is the cause of all the trouble:
PS1="\e[0;32m\w> \e[1;32m"
But this works quite fine (except it's the wrong color):
PS1="\w> "
Any ideas what might be the problem? Am I doing something wrong here?
I figured this out a while ago so I can't exactly explain it, but it needs to be like this:
PS1='\[\e[0;32m\]\w> \[\e[1;32m\]'
Basically you need to surround the escape sequences with \[ and \].

Windows 'choice' command messing up Ruby 'gets' method

Open up irb and
type gets. It should work fine.
Then try system("choice /c YN") It should work as expected.
Now try gets again, it behaves oddly.
Can someone tell me why this is?
EDIT: For some clarification on the "odd" behavior, it allows me to type for gets, but doesn't show me the characters and I have to press the enter key twice.
Terminal input-output handling is dark and mysterious art. Anyone trying to make colorized output of bash work in windows PowerShell via ssh knows that. (And various shortcutting habits like Ctrl+Backspace only make things worse.)
One of the possible reasons for your problem is special characters handling. Every terminal out there can type characters in number of different modes, and it parses its own output in search for certain character sequences in order to toggle states.
F.e. here one can find ANSI escape code sequences, one of possible supported standards among different kind of terminals.
See there Esc[5;45m? That will make all the following output to blink on magenta background. And there is significantly more stuff like that out there.
So, the answer to your question taken literally is — your choice command messes something with output modes using special escape sequences, and ruby's gets breaks in that quirk special mode of terminal operation.
But more useful will be the link to HighLine gem documentation. Why one might want to implement platform-specific and obtrusive behavior when it is possible to implement the same with about 12 LOC? All the respect for the Gist goes to botimer, I've only stumbled into his code using search.

Resources