Automate Tasks with Bash Scripts
đ€ Research suggests that in an eight-hour day, an average developer is only productive for two hours and 53 minutes. Wasting this productive time doing mundane repetitive tasks doesnât really seem to be an option now. An ideal goal should be to automate anything that can be automated. Save the effort and time for actual meaningful work.
This article will give a very basic introduction, enough for any beginner to write his/her first shell script. Basically, bash scripts bind multiple commands together, so that with one command you can execute a whole workflow. Script files carry the extension .sh.
Writing a Shell Script File
Letâs write a simple script that will print âMy first shell script!â on the terminal screen as you run it. Open your code editor and create a file, name it my-first-script.sh. Consider the following lines.
Shebang
#!/bin/bash is called a shebang. It merely tells the interpreter about the shell your script is going to run on. In this case, it is pointing towards the absolute path of the bash shell. __Next line is an echo statement which will print the string. Save the file.
Running the Script
Open the terminal in the same directory where the script file is located. Now, type sh before the filename in the terminal to run your script.
sh my-first-script.sh
đ„ And boom! It prints the string on the screen. As simple as that.
You can also run the script file using bash instead of sh. Another way is to use chmod. You can set an executable permission using the chmod command as follows:
chmod +x my-first-script.sh
So, you can run it by any of the following commands:
sh my-first-script.sh
OR
bash my-first-script.sh
OR by chmod
./my-first-script.sh
With that done, letâs consider an example script that will be actually useful in a real-life scenario.
Automate Git Repository Creation
Letâs say you want to create a new repository on GitHub. Without automation, it involves multiple steps and can take a few minutes. Letâs make it easier. How about a single command that creates a repository of the name you want, declares it private or public, adds a description, clones the repository locally in the current directory, updates README.md and pushes all changes? Sounds good? Itâs pretty simple.
Create a file called repo.sh or whatever you want to name it. Letâs take a look at the following code.
$1, $2 and $3 are shell parameters that will be written after the main command. The first one will be the name of the repository, followed by the description and third being the type of repository. We assign these parameters to variables. Next up are the commands which use GitHub CLI.
gh re --new "$NAME" --description "$DSC" --type "$TYPE"
gh re --clone --repo "$NAME"
The first line creates a new repository and sets its name, description, and type according to the parameters. The second one clones the repository. You can find all of these in GitHub CLI made with NodeJS.
The rest of the code is pretty self-explanatory. We move to the cloned repository, commit and push the changes and display a success message. Letâs run this script. Write this command in the terminal:
sh repo.sh MyRepo Test Private
There you go! It sets up a private repository named MyRepo. Now, what if you want this command to be accessible globally in your bash shell? To do that, we add it as a function in our .bashrc file. Find your .bashrc file. Now we need to create a function, youâll be using the name of the function to call this script. I named it ghinit.
Note that we donât need shebang in .bashrc. Now in any directory, simply type ghinit and parameters to run this script and set up a new repository.
Handy isnât it? There is a lot more that can be done with scripts, go explore. đ