It is quite easy to run a batch file on Windows.
Just create a file, change the extension to .bat and invoke the script in PowerShell or double click to run it. Windows users are spoiled.
Run shell scripts in Linux
If you want to create a script and run it in Ubuntu, a few extra steps are required.
Shell scripts vs batch files
First, Ubuntu runs shell scripts, not batch files, so your file must have a .sh extension.
Second, Ubuntu prohibits users from being allowed to run scripts without run permissions applied to the file.
Ubuntu follows the principle of least privilege, so it always assigns files the minimum permissions it thinks you need to do your job. For the most part, this means read and write access.
So to run a script a quick chmod operation is required, lets your attempt to run a shell script in Ubuntu encounter a permission denied Error.
And finally, when you run a Linux shell script in an Ubuntu terminal window, you need to add a slash to the filename. If you don’t, Ubuntu will search the OS PATH for your program.
The ./ notation tells Linux to look for the script you’re trying to run in the current directory you’re working in.
Shell script stepping
Follow these steps to create and run your own shell script from your Ubuntu home directory:
- Create a directory to store your shell script
- Create a file in your scripts folder with a .sh extension
- Add execute permission to the file to avoid permission denied errors during execution
- Use the ./ (slash) notation and run the shell script by name in the Terminal window
- Press CTRL + C to stop the running linux script if it does not complete successfully
The steps outlined above are evident in the six commands provided below:
$ mkdir scripts $ cd scripts $ touch script.sh $ echo 'echo hello-world' >> script.sh $ chmod -R 777 . $ ./script.sh hello-world
And it’s that easy to create and run a script in Linux or Ubuntu.