J.Carter J.Carter - 10 months ago 45
Linux Question

Conversion of dpn, type commands from windows to bash

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.


Answer Source

The sequence %~dpn is used to get:

  1. %~d - The drive
  2. %~p - The path
  3. %~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"