Bash Script for Lazy People
What is Bash?
Bash is a command processor that typically runs in a text window where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration. The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh. Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.
Basic Command
Let’s start learn basic command line interface.
ls
show list content, examplels Documents
cd
change directory, examplecd Documents
mv
rename or move file, examplemv foo.txt Documents/bar.txt
mkdir
create a new directory, examplemkdir cloudjumper
touch
create a new file, exampletouch foo.txt
rm
remove file or directory, examplerm foo.txt
orrm -f cloudjumper
clear
clear command line screencp
copy file, examplecp foo.txt Documents
cat
display content of a file to the screen, examplecat foo.txt
chown
change owner file, examplechown foo.txt
chmod
change file permission, examplechmod 777 foo.txt
orchmod +x foo.txt
sudo
perform task to root permissiongrep
search file or output particular string or expression, examplegrep ssh foo.txt
- etc.
Why use Bash Script?
Shell script or bash script can be used to :
- Eliminate repetitive tasks.
- Saving time.
- Presents a structured, modular, and formatted sequence of activities.
- With bash functions, you can supply dynamic values to commands by using command line arguments.
- Simplify complex commands into one active, executable command.
- Used as often as possible by users. One bash function for multiple uses.
- Create a logical flow.
- Used at the start of the server (server start-up) or by adding a scheduled cron job.
- Debug command.
- Create interactive shell commands.
Using Bash
Create file hello.sh
and change permission to 777
or +x
to make it executable.
Every bash script must start with the following line :
|
|
Hello World
|
|
or with function
|
|
Run file with command ./hello.sh
and we got an output Hello world!!!
Conditions
If Conditions
|
|
Run file with command ./hello.sh
and we got an output var value is: true
Case Conditions
|
|
Run file with command ./hello.sh
and we got an output var value is: true
Looping
While Do
|
|
For
|
|
My Bash Script Usage
I use bash script to saving time and eliminate repetitive task. So, I can be more productive for lazy people like me.
Push into Git
|
|
Run this file with command ./deploy.sh -m "Your message"
Running Docker Compose Command
I use shell script to simplified task. For example, I will run RabbitMQ as a container using docker with volume mount. First, I will download docker compose
configuration file in here. So, I will execute cUrl
command like below.
|
|
Then, I should create a docker network
.
|
|
Then, I should create a volume rabbitmq-data
and rabbitmq-log
to persist data.
|
|
Then, I will execute rabbitmq.yaml
to run RabbitMQ with docker compose
|
|
And to stop it, I will execute command
|
|
With bash script, I can simplified all task with one command line. First, I will create file rabbitmq.sh
and fill code like below.
|
|
To start RabbitMQ service by typing following command.
|
|
To stop RabbitMQ service by typing following command.
|
|
My Pouncher.sh
I create shell script file pouncher.sh
to saving my time for running docker container with docker compose
like Kafka-CLI, MySQL, PostgreSQL, RabbitMQ, Redis, Sonarqube and SQLServer. I took the pouncher
name from the baby dragon’s name in the How to Train Your Dragon file because it fits the character’s energetic and playful nature.
Just download file from my Github here or you can use cUrl
.
|
|
Then make file is executable chmod +x pouncher.sh
.
See help command by typing ./pouncher.sh --help
For example, I will run MySQL database. I just typing a command ./pouncher.sh -n mysql -c start
or stop service by typing ./pouncher.sh -n mysql -c stop
. See, how it saves my time.
Reference
Wikipedia - Bash (Unix Shell)
Hostinger - Petunjuk Penggunaan Bash Script