I want to know what is the output of below line. Please explain me each symbol. Line is extracted from batch file.
'java -version 2^>^&1 ^| findstr /i "version"'
First take a look on Microsoft article about Using command redirection operators.
2>&1 redirects error messages written to error output stream STDERR to the standard output stream STDOUT. This results in getting standard messages as well as error messages written to same data stream.
The standard and error messages written to STDOUT of java.exe on printing version are used as input for command findstr by using | to get output just the line containing the version information and ignoring all other lines output by java.exe.
This command is obviously embedded in a FOR loop. Therefore it is necessary to escape > and & and | with ^ to get all those redirection operators applied to execution of java.exe instead of command FOR itself which would result in a syntax error.
Open a command prompt window and run there just java -version to see the version information output by Java if command processor could find a file java.* with a file extension listed in environment variable PATHEXT in current directory or a directory listed in environment variable PATH at all.
Next run in command prompt window java -version | findstr /i "version" to see what command findstr outputs on searching case-insensitive for a line containing the string version.
2>&1 respectively 2^>^&1 is unnecessary in my point of view.
Related
I have a scenario where a windows application that I execute in CI exits with -1073740791 eg Stack Overflow. One cmd, this can be caught obviously via %errorlevel% but on bash, at least this exit code maps to 127 in $?.
Obviously, bash on windows should not break scripting so anything above or beyond 0-255 is not fine.
Question is: Is there any special variables or mechanism directly in git-bash itself to catch this actual value ? In this case, the executable is testsuite (think off google benchhmark or google test) and exit code 127 - command not found is not helpful at all.
I had the same issue and i do not think that there is any way to do that within bash.
I decided to wrap my executable in a powershell call, append the exit code to stdout and extract it afterwards like this:
OUTPUT=$(powershell ".\"$EXECUTABLE\" $PARAMETERS 2>&1 | % ToString; echo \$LASTEXITCODE")
# Save exit code to separate variable and remove it from $OUTPUT.
EXITCODE=$(echo "$OUTPUT" | tail -n1 | tr -d '\r\n')
OUTPUT=$(sed '$d' <<< "$OUTPUT")
Some notes:
This solution does combine all stdout and stderr output into the variable $OUTPUT.
Redirecting stderr in powershell wraps the output in some error class. Calling ToString() on these returns them as normal text. This is what the | % ToString is for. Cmp. this SO Answer.
Invoking Powershell can be surprisingly slow due to Windows Defender. This can possibly be fixed by adding powershell to the list of Windows Defender exclusions.
My Python 3.7.1 script generates a fasta file called
pRNA.sites.fasta
Within the same script, I call following system command:
cmd = "weblogo -A DNA < pRNA.sites.fasta > OUT.eps"
os.system(cmd)
print(cmd) #for debugging
I am getting the following error message and debugging message on the command line.
Error: Please provide a multiple sequence alignment
weblogo -A DNA < pRNA.sites.fasta > OUT.eps
"OUT.eps" file is generated but it's emtpy. On the other hand, if I run the following 'weblogo' command from the command line, It works just find. I get proper OUT.eps file.
$ weblogo -A DNA<pRNA.sites.fasta>OUT.eps
I am guessing my syntax for os.system call is wrong. Can you tell me what is wrong with it? Thanks.
Never mind. It turned out to be that I was not closing my file, "pRNA.sites.fasta" before I make system call that uses this file.
I recently try linux (from windows), and I find it difficult to process my following windows command to linux bash.
The windows command was:
set /p cutoff=Set BLAST E-Value Cutoff[1e-]:
for %%F in (*.fa) do program.exe -parameter1 %%F -parameter2_cutoff 1e-%cutoff% -output_file %%~dpnF.fas & type %%F %%~dpnF.fas > %%~dpnF.txt
This script takes a numeric value from user and uses it to run a program in every .fa files on a folder with the desired cutoff. Here %%~dpnF takes only the filename (without file extension). In this very script, I join the content of each input file (.fa) and its generated output (.fas) and finally merge them in final output (.txt). Here, for each Input file, there will be a final output file.
To run it in ubuntu , I try
echo "Set BLAST E-Value Cutoff[1e-]:"
read cutoff
for $f in *.fa; do program -parameter1 $f -parameter2_cutoff 1e-$cutoff -output_file $~dpnF.fas & cat $f $~dpnF.fas > $~dpnF.txt; done
Immediately it shows that linux is not supporting dpn type of command in windows and also the scripts terminates abruptly, showing no output.
Although I understand the different file extensions are not very meaningful in linux, but I have to keep it this way for other programs to process them.
I appreciate any type of help.
Thanks
The sequence %~dpn is used to get:
%~d - The drive
%~p - The path
%~n - The file name
Check the meaning of all expansions here.
The drive has no meaning in Linux. The path, full or partial, could be extracted with the command dirname and the filename could be extracted with the command basename.
The sequence %%~dpn means to get the whole pathname from root (/).
In fact, you do not need that in Linux, if a list of files was created with *.f, the list of files will be relative to the "present working directory" (command pwd), no need to extend them.
And to strip the extension from a filename, use ${f%.*}.
That cuts the string in "$f" at the last dot . and anything that follows *.
Then just add the extension you want: ${f%.*}.fas
Also, the character & has the meaning of "run the previous command in the background", which is not what you want.
And finally, the for $f should be replaced by for f.
This is a cleaner translation:
echo "Set BLAST E-Value Cutoff[1e-]:"
read cutoff
for f in *.fa; do
program -parameter1 "$f" \
-parameter2_cutoff "1e-$cutoff" \
-output_file "${f%.*}.fas"
cat "$f" "${f%.*}.fas" > "${f%.*}.txt"
done
I am using this command to source get.tcl file and giving options 'verbose' and 'instant':
source -verbose -instant get.tcl
the above command worked for me in tcl 8.4 but showing this error in tcl 8.5
source (script wrong # args: should be "source_orig ?-encoding name?
fileName"
if I write only
source get.tcl
It get passed in tcl 8.5
Is there any change related to this in tcl 8.5?
The source command only accepts one option (since 8.5), -encoding, which is used to specify what encoding the file being read is in (instead of the default guess of encoding as returned by encoding system). All it does is read the file into memory and (internally-equivalent-to-) eval the contents.
You can write to any variable you want prior to doing the source, including global variables like argv. With that (plus appropriate use of uplevel and catch, as required, and maybe also interp create) you can simulate running the script as a subprocess. But it's probably easier to not have the file expect to be handling arguments like that, and instead for it to define a command that you call immediately after the sourcing.
You can pass arguments to your sourced file by doing the following:
set ::argv [list -verbose -instant]
source get.tcl
I recommend using:
set ::argv [list -- -verbose -instant]
The -- will stop tclsh from processing any arguments after the --.
Sometimes tclsh will recognize an argument that is meant for your
program and process it. Your programs will need to know about
the -- and handle it appropriately.
Following line in my nsis script is returning 7 :
ExecWait '"$mysqlfolder\bin\mysqldump" --user=$MySQLUser --password=$MysqlPassword --execute="tcm > D:\db_test.sql"' $2
I would like to know whats wrong with it.
Are you sure the exit code is not from mysqldump? You could verify this by running Process Monitor.
In this case the problem could be the stdout redirection (>), this type of redirection is implemented in cmd.exe so unless --execute calls cmd.exe it is not going to work.
If you want to capture the output (of the "root" child process) you have two options:
Prefix your command line with cmd.exe /C (Use ReadEnvStr "COMSPEC" if you need to support Win9x)
Use one of the exec plugins that will capture the output.