A view of Basic commands, creation of script file, and creation of cron job in Shell Scripting.
Basic commands, creation of script file, and creation of cron job in Shell Scripting
Introduction:
A shell script is a listing of commands in a computer program that is run by the Unix shell which is a command-line interpreter. It has comments that illustrate the steps. The various operations performed by shell scripts are program execution, file manipulation, and text printing.
Steps:
Basic commands:
List the directory:
[linuxhelp@linuxhelp ~]$ ls
Desktop Documents Downloads minio Music Pictures Public Templates Videos
Create a directory:
[linuxhelp@linuxhelp ~]$ mkdir scripts
Switch to scripts directory:
[linuxhelp@linuxhelp ~]$ cd scripts
Type the following command to find what type of shell we using:
[linuxhelp@linuxhelp scripts]$ echo $SHELL
/bin/bash
Type the following command to know the Present working directory:
[linuxhelp@linuxhelp scripts]$ pwd
/home/linuxhelp/scripts
Type the following command to see the date:
[linuxhelp@linuxhelp scripts]$ date
Thu Apr 22 02:44:44 PDT 2021
Create test.sh script file:
[linuxhelp@linuxhelp scripts]$ vim test.sh
[linuxhelp@linuxhelp scripts]$ ls -la
total 8
drwxrwxr-x. 2 linuxhelp linuxhelp 21 Apr 22 02:46 .
drwx------. 19 linuxhelp linuxhelp 4096 Apr 22 02:46 ..
-rw-rw-r--. 1 linuxhelp linuxhelp 31 Apr 22 02:46 test.sh
Give the execute Permission for that script file:
[linuxhelp@linuxhelp scripts]$ chmod +x test.sh
Now, check the permission for the test file:
[linuxhelp@linuxhelp scripts]$ ls -la
total 8
drwxrwxr-x. 2 linuxhelp linuxhelp 21 Apr 22 02:46 .
drwx------. 19 linuxhelp linuxhelp 4096 Apr 22 02:46 ..
-rwxrwxr-x. 1 linuxhelp linuxhelp 31 Apr 22 02:46 test.sh
Run the Script file by using following command:
[linuxhelp@linuxhelp scripts]$ ./test.sh
Welcome To Linuxhelp
List the directory:
[linuxhelp@linuxhelp scripts]$ ls -la
total 8
drwxrwxr-x. 2 linuxhelp linuxhelp 21 Apr 22 02:46 .
drwx------. 19 linuxhelp linuxhelp 4096 Apr 22 02:46 ..
-rwxrwxr-x. 1 linuxhelp linuxhelp 31 Apr 22 02:46 test.sh
Create one more file:
[linuxhelp@linuxhelp scripts]$ vim test1.sh
[linuxhelp@linuxhelp scripts]$ chmod +x test1.sh
Run the Script file by using following command:
[linuxhelp@linuxhelp scripts]$ ./test1.sh
List the directory:
[linuxhelp@linuxhelp scripts]$ ls -la
total 28
drwxrwxr-x. 2 linuxhelp linuxhelp 76 Apr 22 02:53 .
drwx------. 19 linuxhelp linuxhelp 4096 Apr 22 02:53 ..
-rw-rw-r--. 1 linuxhelp linuxhelp 380 Apr 22 02:53 store.txt
-rw-------. 1 linuxhelp linuxhelp 12288 Apr 22 02:51 .store.txt.swp
-rwxrwxr-x. 1 linuxhelp linuxhelp 21 Apr 22 02:53 test1.sh
-rwxrwxr-x. 1 linuxhelp linuxhelp 31 Apr 22 02:46 test.sh
[linuxhelp@linuxhelp scripts]$ vim store.txt
[linuxhelp@linuxhelp scripts]$ date
Thu Apr 22 02:54:50 PDT 2021
Type the following command to create a cron job:
[linuxhelp@linuxhelp scripts]$ crontab -e
no crontab for linuxhelp - using an empty one
crontab: installing new crontab
Check the content in cron file:
[linuxhelp@linuxhelp scripts]$ crontab -l
01 03 22 04 05 mkdir cron /home/linuxhelp/scripts/
Type the following command to see the date:
[linuxhelp@linuxhelp scripts]$ date
Thu Apr 22 03:01:02 PDT 2021
List the directory:
[linuxhelp@linuxhelp scripts]$ ls -la
total 28
drwxrwxr-x. 3 linuxhelp linuxhelp 88 Apr 22 03:01 .
drwx------. 20 linuxhelp linuxhelp 4096 Apr 22 02:58 ..
drwxrwxr-x. 2 linuxhelp linuxhelp 6 Apr 22 03:01 cron
-rw-rw-r--. 1 linuxhelp linuxhelp 380 Apr 22 02:54 store.txt
-rw-------. 1 linuxhelp linuxhelp 12288 Apr 22 02:51 .store.txt.swp
-rwxrwxr-x. 1 linuxhelp linuxhelp 21 Apr 22 02:53 test1.sh
-rwxrwxr-x. 1 linuxhelp linuxhelp 31 Apr 22 02:46 test.sh
With this method Basic commands, creation of script file and creation of cron job comes to an end.