Author's note: This article is an updated version of a post from my previous blog. Although its focus may differ (in some way or another) from the current editorial line of this publication, I'm sharing it due to its informational value. I hope you find it useful, and I appreciate your consideration. Cheers!
Introduction
A common question from users migrating from Windows to Linux is: "How do I run .bat files on Linux?" In Windows, .bat files allow you to automate tasks by executing a series of sequential commands. In Linux, the functional equivalent is .sh files, also known as Bash scripts.
In this article, I will explain to you how to run .bat files on Linux using Wine, and also I’ll show you how to create and use .sh scripts to automate tasks on your Linux system, similar to what you can achieve on Windows with .bat files.
Executing .BAT files directly on Linux
Using Wine
Wine is a compatibility layer that allows you to run Windows programs and scripts on Linux systems. Although it's not always perfect, Wine can run many .bat files.
Installing Wine
To install Wine on Debian/Ubuntu based systems, execute
sudo apt-get install wine
For Fedora/CentOS, use
sudo dnf install wine
Running .BAT files with Wine
Once Wine is installed, you can run a .bat file with the following command
wine cmd /c file.bat
This command runs the .bat file in a simulated Windows console on Linux.
Limitations of Wine
Compatibility: Wine does not guarantee that all Windows programs or scripts will work properly.
Alternatives: If Wine cannot run your .bat file, consider rewriting it as a .sh script, or try other tools such as PlayOnLinux or Proton for more complex applications (note that using other compatibility tools does not guarantee the expected result)
Linux equivalent: .sh files
What is a Bash script?
A .sh file is a Bash script, a set of commands that are executed sequentially. These scripts allow you to automate tasks in Linux similar to how .bat files work in Windows, but with more flexibility.
To create a .sh script
Choose a text editor: You can use any text editor to create your .sh script. Some popular options are:
Nano (easy and available in most distributions):
nano script.sh
Vim (more advanced):
vim script.sh
Gedit (graphical interface, similar to Notepad on Windows)
gedit script.sh
Writing the script
Shebang (#!/bin/bash): This line should be at the top of the file. It tells the system to execute the script using bash.
#!/bin/bash
Basic structure
Here's a simple example of a script that updates the system and installs two programs:
#!/bin/bash
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install program1 program2
Interactive example
.sh scripts can also interact with the user by prompting for input. Here's an example of an interactive .sh script:
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name! This is an example of an interactive script."
This script prompts the user for their name and then prints it to the terminal.
Syntax Comparison Between .bat and .sh
Saving the file
Save the file with the .sh
extension, for example: installer.sh
Making the script executable
To run the file, you must first make it executable with the following command
chmod +x installer.sh
You can also do this graphically: go to the file's properties, select the "Permissions" tab, and check the "Allow execution as program" box.
Running the script
There are two ways to execute the script:
1. From the terminal:
./installer.sh
2. Using the bash directly:
bash installer.sh
The difference is that the ./installer.sh
command requires the file to be executable, while bash installer.sh
can run the file without special permissions.
Scripting Basics
Using Variables
Variables allow you to store values for later use:
name="World"
echo "Hello, $name!"
Conditionals
You can execute commands based on conditions:
if [ -f file.txt ]; then
echo "The file exists"
else
echo "The file does not exist"
fi
Loops
Use loops to repeat commands:
for i in {1..5}
do
echo "Number $i"
done
Practical script examples
System update script:
#!/bin/bash
sudo apt-get update && sudo apt-get upgrade
Backup script:
#!/bin/bash
tar -czvf backup.tar.gz /path/to/backup
Log analysis script:
#!/bin/bash
grep "error" /var/log/syslog > errors.txt
Advanced Topics
Debugging scripts
You can run a script in debug mode using the -x parameter:
bash -x script.sh
This will show every command that is executed and help you find errors.
You can also use set -e
in your scripts. This command causes the script to stop immediately if an error occurs:
#!/bin/bash
set -e
sudo apt-get update
sudo apt-get install program1
Environment Variables
Environment variables allow you to set global configurations:
export PATH=$PATH:/new/path
Scheduling tasks with cron
You can automate the execution of your script using crontab:
1. Edit the crontab with
crontab -e
2. Add this line to run your script every day at 8:00 a.m:
0 8 * * * /path/to/script.sh
Wrong permissions
If you get a "Permission denied" error, make sure you've given your script execution permissions:
chmod +x script.sh
Syntax errors
If you get errors like "command not found", check the syntax of the script. Make sure all commands are spelled correctly and paths are correct.
Problems with sudo
If your script fails to execute commands with sudo, make sure the user has the appropriate privileges or run the entire script as superuser (root):
sudo ./script.sh
Using sudo in scripts
Use sudo only when necessary. If the entire script requires superuser privileges, you can run it like this
sudo ./installer.sh
Input Validation
It's important to validate the input your script receives to avoid errors or security issues. Here's an example of how to validate user input:
#!/bin/bash
read -p "Enter your age: " age
if ! [[ "$age" =~ ^[0-9]+$ ]]; then
echo "Error: You must enter a valid number." >&2
exit 1
fi
echo "Your age is: $age"
This script checks that the input is a valid number before continuing.
File Permissions
Make sure your scripts have the correct permissions. Use chmod to give them the proper permissions:
chmod 755 script.sh
Useful command line tools for creating scripts
When working with scripts, these command line tools can be very useful:
grep: allows you to search for patterns in files.
sed: useful for making substitutions and edits in text files.
awk: helps you manipulate and extract data from files.
Glossary
Shebang: the line at the beginning of a script that tells the system which interpreter to use (there are several interpreters or shells; bash, dash, zsh, fish, nushell, etc.)
chmod: command to change the permissions of a file
Cron: tool to schedule the execution of commands at regular intervals
Variable: Container that stores values to be used within a script
Debugging: Process of identifying and correcting errors in a script
Conclusion
.sh scripts are a powerful tool that allows you to automate a variety of tasks in Linux. Now that you know the basics and some advanced topics, I encourage you to keep exploring and practicing. Soon you'll be writing scripts that will save you a lot of time!
Related Resources