I am trying to convert a whole BATCH script to SHELL script with the help of this sort of converter manual.
I have almost finished, but I am struggling to convert this FOR LOOP:
for /f "tokens=*" %%a in ('%adb% shell mkdir /usr/ui/^|find /i "File exists"') do (
if not errorlevel 1 goto :cannot_patch
)
I know that for /f
is
Loop command: against a set of files - conditionally perform a command against each item.
However, as I am a noob to SHELL SCRIPT (and BASH as well), my best try was:
for -f "tokens=*" a in ( '$ADB shell mkdir /usr/ui/^|find /i "File exists"' ); do
if [ $? -nq 1 ]
then
cannot_patch
fi
done
which does not work, resulting in a Syntax error: Bad for loop variable
.
Any hint, link, or suggestion would be very much appreciated.
EDIT
I am trying to understand what exactly ('%adb% shell mkdir /usr/ui/^|find /i "File exists"')
is doing.
I thought those were sh commands, but it turns out I was wrong and that find /i
is trying to
Search(ing) for a text string in a file & display all the lines where it is found.
(https://ss64.com/nt/find.html)
|
is the pipe operator and "File exists"
should be the error thrown by mkdir
in case the command tries to create a directory that already exists.
So I think I could probably write this easier, but still, what does the ^
symbol in /usr/ui/^
do? Is it a regex?
EDIT2
It seems indeed that @glenn_jackman is right: probably I'd better understand what the code is trying to do.
So to give a better context, here is a bit more code of the original batch:
for /f "tokens=*" %%a in ('%adb% shell mkdir /usr/ui/^|find /i "File exists"') do (
if not errorlevel 1 goto :cannot_patch
)
:cannot_patch
echo Error: Cannot create directory!
echo Patch is already installed or system files exist and might be overwritten.
choice /m "Do you want to continue"
if errorlevel 2 goto :END
goto :continue_patch
To my understanding, the code is trying to run the adb shell mkdir
command and, if it fails (throwing the "File exists" error), it will ask to the user if he/she wants to continue regardless.
So in this case, I guess the real problem is trying to write a code that does the same in SH, probably without the need of a for loop.
Still, I am finding it out...