python, run win cm line many quotes for command line with arguments - cmd

The following command line (assigned to the var "cmdlin4popup") runs just fine when I paste it into a Windows cmd.exe (terminal?) window:
"E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141045__Tmp_7602977475348411.py_.txt" 9 "Backup Report" 65
But subprocess.Popen(cmdlin4popup) triggers an error message:
OSError: [WinError 193] %1 is not a valid Win32 application
I think that the problem is the placement of quotes, i.e., the number of double quotes and their placement, but I can't figure out what Python needs. I'd appreciate any suggestions about where to find the solution.
None of the following alternate placements of quotes ran successfully:
cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt" 9 "Backup Report" 65
cmdlin4popup = """E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"""
cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" "E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"
cmdlin4popup = "E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs" """E:\Zmani\Logging\202212141059__Tmp_2248283756147461.py_.txt 9 "Backup Report" 65"""
I am pretty sure that I tried all of the following alternate methods to run each of the above command lines, again with no success:
subprocess.Popen(cmdlin4popup)
subprocess.run(cmdlin4popup)
subprocess.call(cmdlin4popup)
os.startfile(cmdlin4popup)
If the solution was in either of the following postings, I didn't understand the solutions: Python run command line (time), Command line passing quotes within quotes
As an interim kludge workaround (FYI for anyone else dealing with this problem), I wrote the cmdlin4popup var's value to a text file with the extension '.cmd', and successfully used subprocess.run (as recommended by Bill Horvath):
ret_val = subprocess.run(cmdlin4popup)
But it would be great to understand what the problem is.

subprocess.Popen expects a list. And if you have to have the double-quotes to make it work, add single-quotes around them:
cmdlin4popup = ['"E:\Apps\UtilitiesByMarc\inputBox wscript.popup_aaa.vbs"', '"E:\Zmani\Logging\202212141045__Tmp_7602977475348411.py_.txt"', 9, '"Backup Report"', 65]
Also, consider using subprocess.run instead, which is recommended in the documentation.

Related

Neuroimaging: AFNI bash

This is the error that I receiveI am new to this, and I want to run the following script in shell
but I am getting errors, COLUMN.nii file is the nifty file with a collection of masks with value ranging from 1 to 10 and I want to separate these masks into individual nifty file using this AFNI command in a for loop.
Any suggestions are welcomed,
thanks
K.
for i in {1..10};
  do
3dcalc -a COLUMNS.nii -expr ‘equals(a, "${i}”)’ -prefix col_"${i}”.nii;
done
It seems that you may have edited your code using a word processing program, such as Microsoft Word, which doesn't put "normal" single- and double-quote characters into the file.
In your program the line
3dcalc -a COLUMNS.nii -expr ‘equals(a, "${i}”)’ -prefix col_"${i}”.nii;
has those "curly" single- and double-quote characters. Change this to
3dcalc -a COLUMNS.nii -expr 'equals(a, "${i}")' -prefix col_"${i}".nii;
When editing code I recommend that you use a programming-specific editor - there are many out there - rather than a word processing program.

os.execute() with command line options

The Question: How do I execute an OS Command with three command line options in Lua ?
I have a device connected to my PC. (Windows 7, USB cables, typical corporate)
The software which controls the device is located here...
C:\Program Files (x86)\PowerUSB\
The name of the executable file (aka "Program") is...
pwrusbcmd
That program wants three single digit parameters either 1 or 0, separated by spaces
I opened a command prompt box, switched to that directory, and tested all 8 cases. All worked fine.
I then switched to another subdirectory, and tried this command...
"C:\Program Files (x86)\PowerUSB\pwrusbcmd" 1 1 1
That also worked fine.
So I figured that the Lua command to execute that command would be either...
os.execute("C:\Program Files (x86)\PowerUSB\pwrusbcmd 1 1 1 ")
or
os.execute("C:\\Program Files (x86)\\PowerUSB\\pwrusbcmd 1 1 1")
Lua runs each, with no complaints, BUT, no action occurs on the device.
So I tried to alter the construction of the command itself, with the ".." connecting the two segments of the total string, like this...
os.execute("C:\\Program Files (x86)\\PowerUSB\\pwrusbcmd".." 1 1 1 ")
Still no action.
I looked here on StackOverflow, and found
THIS QUESTION
and THIS ONE
and THIS ONE
and THIS ONE
I am in sympathy with each person who wrote those questions. Much like user ID thatthing, I also tried..
square brackets
quote marks (")
single and double and triple backslashes
front slash and s (/s)
So far, I can't find a single syntax construction that works.
The only "fix" (misnomer if there ever was one) I could concoct on my own is to write eight different MS-DOS bat files, and give them unique names. This renders the machine de facto unusable.
How do I get Lua to execute this command ???
C:\Program Files (x86)\PowerUSB\pwrusbcmd 1 1 1
You forgot to add the double quotes around the command name, the easiest way is to use single quoted strings:
os.execute('"C:\\Program Files (x86)\\PowerUSB\\pwrusbcmd" 1 1 1')
Try os.execute([["C:\Program Files (x86)\PowerUSB\pwrusbcmd" 1 1 1 ]])
I believe your problem is the spaces in the file path.
I know you say you used square brackets, but I can't see what combination of them you have used. This works for me.

Bash shell, trying to create and evaluate a mask

I'm trying to create a mask and use the bitwise operator "&" to compare to another variable and see the output. Let there be code:
mask=00000
mesk=00010
mosk=$mask&$mesk
echo $mosk
echo meec
I'm trying to expand this functionality to be able to have more characters (different error/success codes), but those lines just don't work: Executing the script will print an empty line, then "meec".
I came from an object oriented programming background, and although I've read through several documents on this subject, it seems there's something I'm missing.
Any help would be appreciated.
Edit: For some reason, turns out the code doesn't work, it says "command 00010 not found" >_>
It's because usually the & character in the shell is the modifier to put a command in the background.
You have to use Arithmetic Expansion of Bash (for example) for it to work:
mosk=$(($mask & $mesk))

Echo misses ^ characters when long string

I have the following command in a windows batch script
echo =%%k-16,INDIRECT.EXT^("'C:\Users\...\Analysis\[ObsStreamflow.xlsx]Sheet1'^!A%%k"^),INDIRECT.EXT^("'C:\Users\...\Analysis\[sim%%j.xlsx]Sheet1'^!B!val!"^),^=C%%k/1000,^=D%%k-B%%k,^=ABS^(E%%k^),^=(E%%k^)^^2,=^(B%%k-B10^),=Sqrt^(B%%k^),=SQRT^(D%%k^),=^(J%%k - B13^)^^2 >>t%%j.csv
where the omitted file path is 38 characters long (I don't think I'm hitting the line limits, but just in case this is the problem). This is a single line in my .bat file, shown here as multiple lines just to make things more readable.
The output is mostly correct, except that where I have ^^2, it just becomes 2 (so I have =(E1)2 and =(J1-B13)2. If I omit the Indirect.Ext text, and just have
echo =%%k-16,a1,b1,^=C%%k/1000,^=D%%k-B%%k,^=ABS^(E%%k^),^=(E%%k^)^^2,=^(B%%k-B10^),=Sqrt^(B%%k^),=SQRT^(D%%k^),=^(J%%k - B13^)^^2 >>t%%j.csv
it prints correctly, so the relevant comments show as =(E1)^2 and =(J1-B13)^2, which is what I am after.
I've not had any luck finding an answer, everything I have found just points to using ^^ to get echo to return ^. I cannot break this command into multiple lines, I need it to be a single row in csv format.
Any suggestions for a fix much appreciated, I only really need to use this for a week or so, don't need an elegant solution, just one that works. - I'm very new to bat scripts (and indeed programming in general), will keep trying different ideas in the mean time.
It's only the exclamation mark that creates the problems for you.
If at least one ! is in your line (and delayed expansion is enabled), then a second caret escape phase will be started.
In this phase quotes aren't regarded, only carets.
A small test
setlocal EnableDelayedExpansion
echo one^1
echo two^^2
echo two^^2 With exclam!
echo five^^^^^& With exclam!
Output
one1
two^2
two2 With exclam
four^& With exclam
So in your sample, you need five carets.
Four to create one caret and the last one to escape the ), as the escape of the special character is only once required.
Not sure what your specific problem is but you can use a trick in Windows to emulate echo -n (echo without a newline).
The commands:
<nul: >file.csv set /p junk=first field
<nul: >>file.csv set /p junk=,second field
>>file.csv echo ,third field
will result in a single line:
first field,second field,third field
That may make it easier for you to avoid the specific problem and, as a bonus, clean up your script so it's a little more readable (such as one field per script line).
It works because set /p var=prompt is the input command. It first outputs prompt without a newline then waits for the user to enter something, assigning it to the var environment variable.
By getting input from nul:, you basically give it an empty string so it doesn't wait. The prompt is output to file.csv without the newline.
In any case, for something this complex, I'd be bypassing cmd.exe for something a little more powerful such as the UNIX text processing tools under CygWin or MinGW (which require installation but are well worth it), or even VBScript scripts (which should be on Windows by default), where you can more easily control the output.

Using pipe symbol and "print" in Windows

I am trying to make a shell script work in Windows. Sorry but I'm not very experienced in Windows (or even that much in shell to be honest). The script works well except for this one line:
print "9\n0\n1\n5\n0\n0\n\n" | /usr/ts23/mm_util
The mm_util is an interactive utility that takes numbers as input. It chooses selection 9 first, then 0, then 1, etc. I've changed the path to use the utility, which has an identical interface in Windows but the output is just the first screen. The "9" input isn't entered, and because of this the output (that is parsed) is incorrect. How can I change this so that the "9" is entered on the first screen?
Here is a method that does not require a file. It works on the command line:
(for %N in (9 0 1 5 0 0 "") do #echo(%~N)|c:\Users\ts23\mm_util
The "" is to get an empty line in the output, as you had in your original question. Your answer does not have the blank line.
The %~N notation strips enclosing quotes from the value.
The echo( is non-intuitive syntax that can reliably print a blank line, in case %~N expands to nothing.
Don't forget to double the percents if you put the code in a batch script.
Try to put that nine-linebreak-zero-stuff in a text file, and then execute print textfile.txt | /usr/ts23/mm_util
And bear in mind that Windows uses the pre-UNIX convention that the linebreak is CR LF, not just LF.
The way I got the output I wanted was by using this:
C:\Users\ts23\mm_util < test.txt
And then just put the following inside test.txt
9
0
1
5
0
0
The output I got was what I needed, hopefully this will help someone trying to do something like this in the future.

Resources