Converting .sh to .bat - bash

I have a .sh file that I would like to convert to a .bat file. Below you can see my shell script and below that my sad attempt to convert the shell script to a batch file. I was able to covert some parts but other parts "stumped" me, like trying to echo the output of the node pong.js $1 command to __pongjs_output.txt.
# Run a .php file both on pong.js and php and diff the output.
# Run on pong.js
node pong.js $1 > __pongjs_output.txt
# Run on node and replace some property names.
php $1 > __php_output.txt
echo "$1:"
diff __pongjs_output.txt __php_output.txt && echo "ok"
rm __pongjs_output.txt __php_output.txt
My attempt at converting the shell file to a batch file:
#ECHO off
REM Run a .php file both on pong.js and php and diff the output.
REM Run on pong.js
ECHO node pong.js %1 > __pongjs_output.txt
REM Run on node and replace some property names.
ECHO php %1 > __php_output.txt
ECHO %1:
FC __pongjs_output.txt __php_output.txt
IF errorlevel 0 ECHO ok
DEL __pongjs_output.txt __php_output.txt

Try this (remove ECHO from before node and php):
#ECHO off
REM Run a .php file both on php.js and php and diff the output.
REM Run on pong.js
node pong.js %1 > __pongjs_output.txt
REM Run on node and replace some property names.
php %1 > __php_output.txt
ECHO %1:
FC __pongjs_output.txt __php_output.txt
IF errorlevel 0 ECHO ok
DEL __pongjs_output.txt __php_output.txt

Related

Save the result of a dir command to text file

I have tried running dir as echo to txt file but it just saves as a txt file with "dir" in it
#echo off
cd C:\Users\comic\Desktop\CLI\Batch\logs\timestart
echo %username% Is running main.bat at %time% on %date% > log%date%.txt
echo Logging timestart to log.txt at %time% on %date%
cd C:\Users\comic\Desktop\CLI\Batch\logs\filelog
echo dir > log%date%.txt
exit
The issue is that you are echoing the string dir to a file. If you want to save the command output to a file, you should use dir > log%date%.txt. You should also keep in mind that the > operation will overwrite the existing file. If you want to add on to the file, you can use the >> operation.

How to bat function redirects to a file?

Everyone
How do bat functions redirect to a file?
Example:
:func
call run-server.bat %* 2>&1 >>./server.log
goto:eof
call :func %* 2>&1 >>./run.log
In Windows (just like in UNIX/Linux) you can redirect to an output file using the > character:
> : write to file (recreate a new file if needed)
>> : append to file (create the file if it does not exist yet)
Examples:
echo tralala >writetofile.txt
echo tralala >writetofile.txt
echo tralala >>appendtofile.txt
echo tralala >>appendtofile.txt
file "writetofile.txt":
tralala
file "appendtofile.txt":
tralala
tralala
The expression 2>&1, which is used for redirecting error output to the standard output in Linux and UNIX environments, is not used in Windows.
#echo off
set "var=a string with a caret^ and an > arrow"
call :SomeFunc var
exit /b
:SomeFunc
setlocal EnableDelayedExpansion
set "param1=!%1!"
echo !param1!
exit /b

Enable Logging to Batch File Commands

I want to enable logging to a batch file which should log all the contents of command prompt including input/output/error. So I have tried something like the following but it results in a empty log file without any contents. It looks, I am missing something. Below is the batch file.
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root% >> %LOGFILE%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
The log file is empty because you suppressed the output with the command
echo off
Use echo on instead before commands that shall be logged. The command cd usually doesn't send anything to stdout. That's why you got an empty file.
Instead of this ping thingy I recommend
timeout 6
Try this one:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
echo on
call :logit >>%LOGFILE%
exit /b 0 ​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
#timeout 6 >nul
Edit: (beacause asked in a comment)
The log file name can also include the date and time. You need to change the SET LOGFILE command appropriately. You can experiment, starting with this:
SET LOGFILE=C:\Users\xason\Desktop\Logs\log-%date%-%time%.txt
cd command has not STDOUT. You probably need to append STDOUT of your batch file to a text file. Run your batch file from cmd:
batch_file.bat >> "C:\Users\xason\Desktop\Logs\logs.txt"
Or, inside your batch file:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit >>"%LOGFILE%"
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
Or even:
#echo off​
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
​
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora >> %LOGFILE%
ping 127.0.0.1 -n 6 > nul

Windows Batch script to redirect stdout to stdin of an EXE we've just run

I know the normal behaviour when running an EXE in a batch script is for the batch script to wait for the EXE to exit before continuing, but is there any way to get the batch script to continue execution, but redirect its stdout to the stdin of the EXE?
Basically I'm trying to achieve this neat trick or something similar...
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
C:\cygwin\bash.exe <--- Do some magic here
echo This is a bash shell script...
ls -la /cygdrive/c/
exit
echo We're back to the windows batch script again
REM Note: being able to return to the batch script isn't important
Any ideas on how to achieve this? Thanks.
This should work:
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
(
ECHO echo This is a bash shell script...
ECHO ls -la /cygdrive/c/
ECHO exit
) | C:\cygwin\bash.exe
echo We're back to the windows batch script again
EDIT: Reply to the comment
If you want not to add ECHO to each bash line, you may use this method:
#ECHO OFF
echo This is a windows batch script...
dir /p C:\
rem Get the number of the first line in this file that start with "###"
for /F "delims=:" %%a in ('findstr /N "^###" "%~F0"') do set "line=%%a" & goto break
:break
rem Pass from that line to end of this file to bash.exe
more +%line% "%~F0" | C:\cygwin\bash.exe
echo We're back to the windows batch script again
goto :EOF
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
EDIT #2:
I borrowed SonicAtom's method and slightly modified it in order to make it simpler. Here it is:
#ECHO OFF
setlocal
rem This is a magic trick to run the bottom half of this script as a bash script
if not defined _restarted (
set _restarted=true
cmd.exe /Q /D < "%~F0"
rem Put any cleanup commands here
echo/
echo Bash script finished
pause
exit /B
)
cls
"C:\cygwin\bin\bash.exe"
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
No replies?
Well I found the answer myself:
test.bat:
#ECHO OFF
rem This is a magic trick to run the bottom half of this script as a bash script
if not exist _.bat (
echo #ECHO OFF >_.bat
echo cmd.exe -c ^< %~nx0 >>_.bat
_.bat
rem Put any cleanup commands here
del _.bat
echo.
echo Bash script finished
pause
exit /B
)
cls
"C:\cygwin\bin\bash.exe"
#################################################################
# The remainder of this file is a bash script running in cygwin #
#################################################################
echo This is a bash script!!
ls -la /cygdrive/c/
How it works:
The file starts off as a batch script. It writes another (temporary) batch script which runs another cmd.exe shell, directing the original batch file to cmd.exe as stdin. The original batch file then acts like a text file containing commands typed on the keyboard. It runs bash.exe, and continues to provide stdin to bash. The only disadvantage is that the #ECHO OFF doesn't take effect in the part of the batch file that runs before bash.exe is called. This is because there doesn't seem to be a way to turn off keyboard echoing in cmd.exe.

BATCH- How to put a variable into the move command?

Okay, this is my first question on Stackoverflow, so I'll try to make it a good one.
I've searched all over the web and I couldn't find any information to this. I have created a little batch file that prompts you to put in the name of a file that you would like to move. After that, it asks your your usersname. The problem I'm having is cmd tells me I have incorrect syntax.
Can anyone see what I did wrong?
Below is the code I am using. I just pasted in the part that is having trouble.
Thanks guys!
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename%
echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move C:\Users\%USERNAME%\Desktop\%filename%.bat
C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\StartMenu\Programs\Startup\%filename%goto COMPLETED
goto FAILED
I editted a bit your script:
Surrounded filepaths with double quotes
Added /y parameter to move command to override automatically the file in new location (if exists)
Spotted some issues:
goto MOVE
:MOVE has no sense as it will continue anyway that path.
goto FAILED GOTO COMPLETED - there're no such labels in your script.
Please shout if you have other problems.
#echo off
echo Place the file you wish to move to the Windows Startup Folder on your desktop.
echo When you have placed it there, type in the name of your file, NOT INCLUDING the extension.
echo Example: The file's name is: myfile.bat You type in: myfile
set/p "filename=>"
echo %filename% echo Next, type in your username.
echo Example: acly6
set/p "USERNAME=>"
echo %USERNAME%
ping 192.2.0.0 -n 1 -w 500 > nul
goto MOVE
:MOVE
echo Moving your file to your startup folder.
move /Y "C:\Users\%USERNAME%\Desktop\%filename%.bat" "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\"
ping 192.2.0.0 -n 1 -w 1000 > nul
echo Checking Volumes...
ping 192.2.0.0 -n 1 -w 3000 > nul
if EXIST "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\%filename%" goto COMPLETED
goto FAILED
:FAILED

Resources