Can I run Windows .bat files in Ubuntu?

I want to run a .bat file in ubuntu, but it just opens the text editor. Here's the code of the .bat file(you can play it):

@echo off
set /a guessnum=0
set /a answer=%RANDOM%
echo Welcome to the Guessing Game!
echo.
echo Try and Guess my Number!
echo.
echo.
goto guess
:guess
set /p guess=
set /a guessnum=%guessnum% +1
if %guess% GTR %answer% ECHO Lower!
if %guess% LSS %answer% ECHO Higher!
if %guess% EQU %answer% GOTO EQUAL
goto goto guess
:equal
echo Congratulations, You guessed right!!!
echo.
echo It took you %guessnum% guesses.
echo.
pause
2

2 Answers

By the looks of it, most of it (the parts using the “echo” command) should work, but not the batch-specific parts (e.g. “goto”). But you should be able to convert it to a shell script and then it might work, or you may be able to emulate it within Ubuntu.

Edit: I've converted it, it appears to be incomplete. It appears to have converted the "if" parts, but not anything else. Let me know if it works (I highly doubt it), I'll also try it personally. I hope it helps. (Moderators, please update this answer with a fully converted script if possible.)

export -a guessnum=0
export -a answer=${RANDOM}
echo Welcome to the Guessing Game!
echo ""
echo Try and Guess my Number!
echo ""
echo ""
export -p guess=
export -a guessnum=${guessnum} +1
if ${guess% GTR %answer} echo Lower!
if ${guess% LSS %answer} echo Higher!
if ${guess% EQU %answer} echo EQUAL
goto top
:equal
echo Congratulations, You guessed right!!!
echo ""
echo It took you ${guessnum} guesses.
echo ""
pause
2

If you install wine (see for more information) to your system you can use your batch file.

After you have installed wine (use the sudo apt install wine64 command to install it); run the command wineconsole. A window similar to the Windows "command prompt" will appear. Change to the directory containing your Windows batch file and run it by entering its name.

Or, you can switch to the directory containing your batch file in a Linux terminal (use the Ctrl+Alt+T keyboard shortcut to open a terminal) and run wineconsole your_batch_file.bat directly.

Note: Your batch file contains an error: You should put a :top label after the 8th line.

3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like