Bash Scripting Practice on Linux

Bash scripting is just such as programming because we can use loop and if else statement which can be powerfull help an System administrator to doing complex task using this method. But for beginer like me it's also need more time to spend with this to be comfy dong task with scripting.

Here I wanna demonstrate to you guys simple scripting.

Actually scripting is just like command line with several command inside the file which is if you run/execute the file will run the command inside the file. But it's more than that because you can add if else statement inside the file.

first you can create your files with .sh is usualy to initiate that this is an bash script files. but actualy script also will be run with out .sh extention
sysadmin@localhost:~$ touch report.sh

Then see the files is alread created with standard permission files you can also read about permission in my previous post here : http://andre-networking.blogspot.com/2013/01/linux-file-permissions.html
sysadmin@localhost:~$ ls -l
total 44             
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Desktop
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Documents
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Downloads
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Music    
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Pictures 
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Public   
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Templates
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Videos   
drwxr-xr-x 1 sysadmin sysadmin 4096 Dec 16 19:06 junk_folder
-rw-rw-r-- 1 sysadmin sysadmin    0 May 16 14:20 report.sh

Then we can use nano or any other text editor that you comfy to use
sysadmin@localhost:~$ nano report.sh 

put the script inside text editor
#!/bin/bash
echo "This is document report to administrator regarding disk usage" > laporan.txt
date >> laporan.txt 
pwd >> laporan.txt 
df -h >> laporan.txt
echo "==========================================" >> laporan.txt

you can see there is #!/bin/bash on the first line. It's to indicate that this is an bash file. but actualy the script will also running without this line. Every bash script is begin with this line called shebang or hasbang. This is an interpreter

That script will resulting date, current directory, and disk usage and put it all into laporan.txt. Pay attention to > and >> . >is to overwrite all inside the file. while >> is to append to end of line.

Give permission to user
sysadmin@localhost:~$ chmod u+x report.sh

and see the permission is already changed to executable
sysadmin@localhost:~$ ls -l
total 52
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Desktop 
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Documents
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Downloads
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Music
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Pictures
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Public
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Templates
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Videos
drwxr-xr-x 1 sysadmin sysadmin 4096 Dec 16 19:06 junk_folder
-rwxrw-r-- 1 sysadmin sysadmin  221 May 16 14:22 report.sh

Then try to run the bash 
sysadmin@localhost:~$ ./report.sh

The script will earn new files called laporan.txt
sysadmin@localhost:~$ ls -l
total 52
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Desktop 
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Documents
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Downloads
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Music
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Pictures
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Public
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Templates
drwxr-xr-x 1 sysadmin sysadmin 4096 May  2  2019 Videos
drwxr-xr-x 1 sysadmin sysadmin 4096 Dec 16 19:06 junk_folder
-rw-rw-r-- 1 sysadmin sysadmin  580 May 16 14:22 laporan.txt
-rwxrw-r-- 1 sysadmin sysadmin  221 May 16 14:22 report.sh

Then see what is inside the laporan.txt
sysadmin@localhost:~$ cat laporan.txt
This is document report to administrator regarding disk usage
Sat May 16 14:22:39 UTC 2020
/home/sysadmin 
Filesystem      Size  Used Avail Use% Mounted on
overlay         196G  155G   32G  84% /
tmpfs            64M     0   64M   0% /dev
tmpfs            95G     0   95G   0% /sys/fs/cgroup
/dev/sda1       196G  155G   32G  84% /etc/hosts
shm              64M     0   64M   0% /dev/shm
tmpfs            95G     0   95G   0% /proc/acpi
tmpfs            95G     0   95G   0% /proc/scsi
tmpfs            95G     0   95G   0% /sys/firmware
==========================================

Here we go!!!

Tulis komentar anda... Conversion Conversion Emoticon Emoticon

Thanks for your comment