ShellScript - 1

ShellScript - 1

What is a Shell?

Shell is a program that takes the commands whatever the user is typing and it will give to the OS to execute. It acts as an interface between the user and the OS.

In most of the Linux systems bash is the default shell., Linux supports many shell types such as bash, ksh, sh, and csh. We can cat into /etc/shells to see all the available shells in the system.

echo $SHELL - it will give the current shell, to change the shell we can simply do /bin/bash.

What is a Shellscript?

Shellscript is a file that contains a series of commands, all the shellscript files end with .sh

Bash will take the commands one by one and take them to the OS, in case there is an error in 3rd line it will still execute the other commands, the behavior of shellscript is different from traditional programming languages, in the case of Java we will get an error.

Why Shellscript?

With shellscript we can automate tasks., let’s say we need to take a backup of the folder every Friday at 10 PM we can have a file with all the commands and configure it in the crontab, this way we don’t need to do it manually.

Suppose if the server resources are reached 80% then we need to send a mail notification including server details like hostname and ifconfig. So in this shellscript, we can have, THRESHOLD=80 , free, df, top, mail, ifconfig, hostname.

We can also use shellscript for cleanup purposes, if we need to delete a file exceeding 100MB we can also configure the shellscript in crontab and automatically larger size file gets deleted. Using shellscript we can also get resources of the AWS such as how many EC2 instances are active and how many S3 buckets there are., we will put this information in a file and we can generate the report by simply running the shellscript.

Shell scripts are not limited to simple system tasks; they can interact with APIs, perform complex processing, and integrate with other tools. For tasks requiring more advanced logic, error handling, or modularity, consider using Python or another scripting language in combination with shell scripts.

vi hello.sh - we are creating a shellscript with the name hello.sh ., and again it is not mandatory to give .sh as the file extension for shell scripts. The shell relies on the file's content and permissions, not the extension. You can execute a script without .sh if:

  1. The file has execute permissions (chmod +x script.sh).

  2. To specify the interpreter, it includes a shebang (e.g., #!/bin/bash) at the top.

So the first line should be shebang and then we can have our commands.

We can run the shellscript in multiple ways

./script.sh - here the file should have execute permissions

sh script.sh - it runs even if execute permissions are not there as sh shell by default has the permissions.

bash script.sh - same no need execute permissions

. script.sh

#!/bin/bash - it’s called a shebang line which will tell what shell it needs to use to execute the shellscript., in this case, it is the bash shell.

Let’s see a simple shellscript.,

To execute the shellscript in the debug mode we can use sh -x hello.sh command ., this will give the output like this

If we need to debug certain lines we can use set -x and set +x .,

So coming to the file naming conventions of the shellscript., a file can be a maximum of 255 characters, the file name is case sensitive, we cannot use reserve words as file names, and a name can contain alphabets, digits, dots, and underscores.

Comments :

We have single-line comments - # and multi-line comments as well, for the multi-line comments we need to have <<script which is opening for the comment, and for closing we need to just use the script with no need for >>

Variables :

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, or any other type of data. There are two types of variables in the Linux.

  1. System-Defined Variables

  2. User-defined Variables

To see all the system-defined variables we can use the env or printenv command

We can use echo $USER to display the current user., if we do echo USER it will just print USER so we use $ here to print a particular variable. We can also change the value., let’s say if we execute the history command we can see the last 1000 executed commands we can also change it using the export keyword.,

So in the above picture, we have changed the history size, this way we can make modifications., and these changes are limited till session time to do it permanently we need to make an entry in the .bash_profile present in the user home directory.

In shellscript we don’t have datatypes so we don’t use int, float, or string.

The next important one we have is Command Line Arguments

During shell script execution, values passing through the command prompt are called command line arguments. If we are running a shellscript we can specify the command line arguments as “sh script.sh arg1 arg2 arg3”

We can specify n number of arguments, there is no limitation

Each argument is separated by space

In case we have a shellscript with the name dbbackup.sh and it takes 2 arguments dbname and backuplocation, rather than hardcoding these in the script we can pass them as arguments which will make this script reusable also.

echo $0 - Gives the shellscript name

echo $1 - Gives the First Argument

echo $2 - Gives the Second Argument

echo $3 - Gives the third Argument

echo ${10} - Gives the 10th argument, in case of double digits we need to use parenthesis

echo $# - Gives the no of arguments

echo $* - Gives all the args into a single string

echo $@ - Gives each argument as a separate string

echo $$ - Gives the process ID, shellscript will have the processID created

echo $? - To know whether the previous command is executed successfully or not, if executed successfully we will get 0

In case we give Date then it will return 127 which is the error code for the command not found.

If we have a condition to execute only if the required number of arguments are passed then we can make use of $# which returns the no of arguments

So here we have used the if condition and also executed our program only if 2 arguments are passed.

Also with $? we can check the previous command execution status., let’s take this scenario ., only if Java is installed then only we need to install Jenkins

So that’s all about the Command Line Arguments.

Subscribe to our newsletter

Read articles from Terraform directly inside your inbox. Subscribe to the newsletter, and don't miss out.