AMP AMP

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: snap1

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

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 ..
-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

Give the execute permission for script file:

[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 

Type the following command to see the date:

[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.

FAQ
Q
Can we run a shell script on Windows?
A
With the arrival of Windows 10's Bash shell, you can now create and run Bash shell scripts on Windows 10. You can also incorporate Bash commands into a Windows batch file or PowerShell script.
Q
When should shell programming/scripting not be used?
A
Generally, shell programming/scripting should not be used in the below instances. When the task is very much complex like writing the entire payroll processing system. Where there is a high degree of productivity required.
Q
Is scripting harder than programming?
A
Coding with programming languages is relatively difficult as many lines of code are required for a single function. Creating a code function with a scripting language is easier as it requires only a few short and specific lines to be written.
Q
What is the difference between scripting and programming?
A
Programming languages are generally compiled and create an executable file, whereas Scripting languages are interpreted and do not create an executable file. Programming languages are most often used to build something from scratch, whereas Scripting languages can be used to combined existing components or modules.
Q
What is meant by a scripting language?
A
A scripting language or script language is a programming language for a runtime system that automates the execution of tasks that would otherwise be performed individually by a human operator. Scripting languages are usually interpreted at runtime rather than compiled.