Fix the Bat File to make it work in Windows - bash

I'm not familiar with working on Windows command prompt. I've to fix this batch file to make it work in Windows. I understand that this isn't a doubt. But this is stopping my progress and any help would be appreciated.
ndk-build clean &&
ndk-build APP_PLATFORM=android-15 NDK_LIBS_OUT=./sdk_core_bin NDK_OUT=./sdk_core_obj &&
echo "moving .so files to /src/main/libs" &&
cp -R ./sdk_core_bin/. ./src/main/libs &&
rm -r ./sdk_core_bin &&
rm -r ./sdk_core_obj

This is not a batch file. You probably want to convert a bash shell script to a batch file. There are multiple problems with this:
You can't have && at the end of the line because Windows commands can only be in one line unless you escape the new line with ^
Windows path is separated by backslash \ instead of slash. And it searches in the current folder first so you don't have to use . at the beginning of the path
echo is an internal command that doesn't accept parameters so you can't use quotes with it
The result might be like this
ndk-build clean && ^
ndk-build APP_PLATFORM=android-15 NDK_LIBS_OUT=sdk_core_bin NDK_OUT=sdk_core_obj && ^
(echo moving .so files to src\main\libs) && ^
copy sdk_core_bin src\main\libs && ^
rd /s sdk_core_bin && ^
rd /s sdk_core_obj

Related

ksh - why presence of the "\" file in current working directory changes behavior of variable processing

I had an issue with third party ksh script.
Found out, that it was failing because of file named "\" in user home directory.
Here is a simple testcase:
$ mkdir -p ~/dir1 && cd ~/dir1 && touch '\' && x="\* a" && echo $x
\ a
$ mkdir -p ~/dir2 && cd ~/dir2 && x="\* a" && echo $x
\* a
The question is, why the presence of "\" file in a working directory changes the result.
Is this expected?
Thanks.
T.
Looks like the expected behaviour.
If you want the same behaviour in both cases, either use set -o noglob inside your script, or run the script with the -f option to disable file name substitution.
The default is that the * is a special character when interpolating so will match whatever file exists (in your case dir1 will contain only one real file with the name of the backslash character.)
The second directory dir2 has no real files so ksh just shows the pattern exactly as you typed it.

How to change drive in Makefile?

The target (rule) in my Makefile has a series of commands. First few commands should be executed from D:\ drive. After this, I should change my directory to C:\ so that I can execute a file in C:\ drive. But, I am not able to change drive. I tried the following ways:
C: (it works in cmd but not in Makefile)
cd /d C: (not working)
cd C: (not working)
Please let me know how to change drive from within a make rule.
This can be achieved by using either ; or && \
cd d:\folder1 && \
dir && \
echo "all files in d:\folder1 are listed"
cd c:\folder2 && \
dir && \
echo "all files in c:\folder2 are listed"
is equivalent to
cd d:\folder1; dir; echo "all files in d:\folder1 are listed"
cd c:\folder2; dir; echo "all files in c:\folder2 are listed"

Read a file line by line and run a java program

I need to run a java program that merge multiple files with a *bam extension. the structure of the program is:
java -jar mergefiles.jar \
I=file1.bam \
I=file2.bam \
I=file3.bam \
O=output.bam
So, I am trying the run this program for all *bam files in a directory. Initially, I try to create a list with the names of the *bam files (filenames.txt)
file1.bam
file2.bam
file3.bam
and using the 'while' command, like:
while read -r line; do
java -jar MergeFiles.jar \
I=$line \
O=output.bam
done < filenames.txt
However, the program executed for each *bam file in the text file but not all together (merge only one file per time, and overwrite the output). So, how I can run the program to merge all *bam files recursively?
Also, there are other option in the bash (e.g. using a loop for) to solve this issue?
Thanks in advance.
In your question you specify that you would like to use all .bam files in a dir, so instead of creating a file with the filenames, you should probably use globbing instead. Here's an example:
#! /bin/bash
# set nullglob to be safe
shopt -s nullglob
# read the filenames into an array
files=( *.bam )
# check that files actually exist
if (( ${#files[#]} == 0 )); then echo "no files" && exit 1; fi
# expand the array with a replacement
java -jar MergeFiles.jar \
"${files[#]/#/I=}" \
O=output.bam
The problem with your current solution is that the while loop will only read one line at a time, calling the command on each line separately.

mkdir && cd windows alias

I am trying to make a directory and immediately change into it with a DOSKEY. I thought this would work but it gives me the error The filename, directory name, or volume label syntax is incorrect.
DOSKEY md=mkdir $* && cd $*
Does anyone know why this is wrong and how to fix it?
I ended up finding the answer here
DOSKEY md=mkdir "$1"$tcd "$1"
I was trying to use && instead of $t
Does this work for you?
C:>doskey aaa=MKDIR "$*"$tCD "$*"
C:>doskey /macros
aaa=MKDIR "$*"$tCD "$*"
C:>aaa arf
C:>
13:33:59.15 C:\Users\pwatson\x\arf
C:>

what does 'cd $_' mean?

I have seen this command in a tutorial to create a new directory:
mkdir my-new-project && cd $_
I know mkdir my-new-project command is used to create a new directory, but what does cd $_ do?
$_ expands to the last argument to the previous simple command* or to previous command if it had no arguments.
mkdir my-new-project && cd $_
^ Here you have a command made of two simple commands. The last argument to the first one is my-new-project so $_ in the second simple command will expand to my-new-project.
To give another example:
echo a b; echo $_
#Will output:
#a b
#b
In any case, mkdir some_dir && cd $_ is a very common combo. If you get tired of typing it, I think it's a good idea to make it a function:
mkdircd() {
#Make path for each argument and cd into the last path
mkdir -p "$#" && cd "$_"
}
*
The bash manual defines a simple command as "a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. " where control operator refers to one of || & && ; ;; ( ) | |& <newline>.
In practice$_ works like I've described but only with ||, &&, ;, or newline as the control operator.
7. How to create directory and switch to it using single command. As you might already know, the && operator is used for executing multiple commands, and $_ expands to the last argument of the previous command.
Quickly, if you want, you can create a directory and also move to that directory by using a single command. To do this, run the following command:
$ mkdir [dir-name] && cd $_
For those coming from Udacity's Version Control with Git, HowToForge offers a great explanation, here.

Resources