1

Hello Unix & Linux Community,

I am here to ask how I can translate a Windows Batch File (.bat), into a Linux Bash File (.sh), because I want to allow a program I made, into Linux. But I have no idea how to get it to work in Linux. I understand that some things, like EXEs, are "non-existent" in Linux.

So, the code I want to translate is too long to fit here, so I have posted it elsewhere, and here is a link to it. If there is any way to do this, I would greatly appreciate it!

  • 2
    It's a 600 line script (even if most of it is bloat): what have you tried? This isn't a script writing service. – jasonwryan Jan 6 at 2:26
  • This is a good place to start: ss64.com/bash Many batch commands have near-equivalents in bash. – baum Jan 6 at 2:40
  • 1
    @jasonwryan They are asking "how I" can translate the file, which implies that they are asking if there is an automated way, IMO. – Sparhawk Jan 6 at 2:56
  • @Sparhawk Not really, It would be great if there was one, but I can try to do it manually. – TheCrafters001 Jan 6 at 3:15
2

I'm afraid there is no automated way to convert a BAT script into Bash. This leaves you with two options:

Option 1. Convert the script manually.

The script that you linked looks simple enough, which means it shouldn't take much time to convert it once you're familiar with the basics of Bash scripting. This book should be a good starting point in your studies. Appendix N of the book contains a nice glossary that could help you replace your old Batch idioms with Bash ones.

Option 2. Use wineconsole.

wineconsole is part of the Wine compatibility layer that allows executing BAT files on Linux systems:

$ wineconsole MyCode.bat

See the following question for details on how to do that. Although appealing, this may be a dead end if you want run other Linux programs from your script. Furthermore, not all users will have wine installed or consider it an acceptable tradeoff.

  • I understand that there is no automated way, but thank you for the tips! I think echo is the same in both BATCH and Bash Right? – TheCrafters001 Jan 6 at 2:49
  • @TheCrafters001 In the most basic use case, yes, but its command-line options are different. Type man echo in the console for the manual. – undercat Jan 6 at 2:51
  • So, Say I want to do something like set /p I would have to do (using the knowledge from the website linked above) export -p? – TheCrafters001 Jan 6 at 3:19
  • @TheCrafters001 No, there is no direct mapping between Batch and Bash. Just as an example, to emulate set /p you will likely want to use the read built-in. You do need to read the documentation (man bash, help export, help read, etc.) and/or tutorials to understand what commands and options you need to use to emulate the Batch behaviour. If you're stuck at some point, you should feel free to start a new question, as long as it focuses on one particular aspect. Asking multiple questions in the same topic, especially in comments, is discouraged. – undercat Jan 6 at 4:56
  • @undercat note that man echo will give you the manual of /bin/echo but when you run echo in the vast majority of shells, you run the _builtin` echo command instead. In bash, you want help echo to see its documentation. – terdon Jan 6 at 13:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.