Understanding And Using The Shell Scripts In Linux

If you use Linux in your daily life. Learning shell scripts in Linux will give you several benefits. Learning shell scripts is not mandatory, but it enables you to accomplish several tasks with fewer efforts. Let’s understand and learn using shell scripts in Linux. 

What Is Shell In Linux?

In Linux, a Shell is like a layer around the Kernel allowing access to all its services through shell scripting. A shell can take inputs from the command line interface (CLI) or graphic user interface (GUI) to make changes to the Kernel or OS services.

The Kernel manages all the essential resources for the Linux system with File management, Process management, Device management I/0 management, Memory management, among others. Every time a user logs in or starts the terminal, the shell automatically starts acting as a language interpreter to take instructions from input devices such as keyboards or touchpad from files.

What Are Shell Scripts In Linux?

Shell Scripts are files that include several commands that can run directly on the Kernel. So as the user inputs the command, the shell reads them from file and executes them just as in a case of the command line interface. Shell here acts as both a command-line interface and a scripting language interpreter to the system.

What Are The Benefits Of Using Shell Scripts In Linux?

Most often, we need to run specific commands multiple times to perform certain tasks. You can here use shell scripts or shell programs to ease the burden. You can create a file with particular commands and save them in a local directory of the shell. Each of these shell scripts has a sh file extension and can be called on anytime to get the desired result.

You can write shell script syntax just as you do in a programming language. Some of the primary reasons and benefits of using shell scripts include:

  • Helps in automation
  • Avoiding repetition of work
  • Comprehensive system monitoring
  • Adding new specific functionality to the shell
  • Maintaining routine backups for the system

How To Create And Run Shell Scripts In Linux?

Creating Shell Scripts In Linux:

A shell script is just like any text file to write programs or commands. So the first thing you must have is a text editor for writing a shell script. Some of the best editors include vim, nano, gedit, kate, etc. These text editors must have syntax highlighting that differentiates color-coded views for the elements inside the script. This syntax highlighting is also useful in avoiding common errors and semantic writing.

Placing Shell Scripts:

When executing, shell searches only specific directories for files when there are no explicit pathnames. The script needs to be saved inside these ideal directories for the shell to find and execute the command.

Understanding The format Of Shell Scripts:

You can write syntax in the script file just like any programming language. Here is a simple script to begin with. Here echo command is being applied with a string argument.

#!/bin/bash#
This is my first script.
echo ‘Hello World!’

The first character (#!) uses a special construct termed as a shebang. This shebang informs the system of the interpreter’s name to execute this script. All shell scripts in Linux are required to use this construct as the first line.

Anything that starts with # is taken as a comment and ignored. These are informative for users and have no impact on the script. You must understand the comments can also be used at the end of the line. For instance like this:

  • echo ‘Hello World!’ # This is a comment too

Similarly, you can also use comments in the command line.

Adding Executable Permissions:

The next step after writing a shell script is to add executable permission to the file. Here you can do that easily with the chmod command.

[me8linuxbox ~] S ls -l hello_world
-rw r-r– l me me 63 2019-12-12 10:10 hello_world
[[email protected] ~] $ chmod 755 hello_ world[
[email protected] ~ ] $ ls -l hello_ world
rwxr-Xr-x 1 me me 63 2019-12-12 10:10 hello_ world

Generally, there are two standard settings for file permissions in scripts 755- Anyone can execute the script.700-Only the owner can execute the script.Note: Script files must also be readable for the bin to execute them from ibe command line.

Script File Location

After writing scripting and setting permission, we can execute the script from the shell with the following syntax.

[[email protected] ~]$ ./hello _world
Hello World!

In case the system doesn’t find the script it will throw errors such as.

[[email protected] ~ ]$ hello _world
bash: hello _world: command not found

Here you must understand the importance of the location of the script. The system will search only certain directories for scripts in case the path of the file is not given. By default /bin is the directory system will search for the script. All these directories are put inside the PATH environment variable. This PATH variable holds a list of all directories that the system can search into. You can check the contents of this PATH variable through the command line using:

 [[email protected] ~] $ echo $PATH
/home/me/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/
bin:/usr/qgames

So when you search a script without any specified path, the system can search only in the above directories to get the desired result from a script.Several Linux distributors often configure this PATH variable with bin directory in the home directory. Similarly, you can create a directory pro side the default location and move a file to that directory using the following commands.

[[email protected] -1$ mkdir project1
[[email protected] ~] $ mv hello world project1
[[email protected] ~]$ hello_world
Hello World!

In case the PATH environment doesn’t have a specific directory, then you can also add them in the PATH variable using this line in the bashe file

export PATH=/bin:”$PATH”

After execution, the directory will be included in the PATH environment from a new terminal session.
Shell also needs to re-read the file to update with the latest changes in the .bashrc file. This is done through sourcing in the following way:

[[email protected]~ ] $. .bashrc 

Here () dot works for source command, a shell builtin that acts as a file of shell commands treating this as input from the keyboard only. Note: In Ubuntu, the system automatically updates a -/lbin directory for the PATH variable on executing .basbrc file. So once you log out and login directories are updated and work seamlessly.

Ideal Locations For Shell Scripts In Linux

The -/bin directory is the ideal place to save scripts mainly for personal use. Directory at /usr/local/bin is the traditional location for scripts that everyone on a system can use. Whereas /usr/local/sbin directory is intended for scripts to be used by system administrators. On the other hand, local compiled software or programs should be saved in the /usr/local directories, not in the /usr/bin or /bin. Linux Filesystem Hierarchy Standard allows only specific directories and files in the system that are maintained and supplied by Linux distributors.

Formatting Shell Script

Shell scripting’s primary goal is to ease in performing tasks. There are several formatting tricks, modifications, and adaptations to get the desired output. A script must be easy to read and understand for proper maintenance.

Long Options Names

Most of the commands can be used in both short and long name options such as Is command in the following way.

[[email protected] ~ ] $ ls -ad

Or

[[email protected] ~]$ ls –all –directory

On execution, both commands will give the same results. Though shorter versions are more preferred for using on the command line still when creating shell scripts these long options improve readability.

Indentation and line continuation In Shell Scripts:

In executing long commands, you can enhance the readability by allowing the command to spread in multiple lines. Here is a quick demonstration of this fact.

[[email protected] ~] $ find playground (-type f -not -perm 0600 -exec
chmod 0600 ‘ ‘; ) -or ( -type d -not -perm 0700 -exec chmod
0700 ‘{}’ ‘;’ )
This command can be quite complex to understand at first look. So in a script, it might be more suitable to understand if written in the following way.
find playground /
/ ( /
-type f /
-not -perm 0600 /
-exec chmod 0600 ‘{}’ ‘;’ /
/ ( /
-or /
-type d /
-not -perm 0700 /
-exec chmod 0700 ) ‘{}’ ‘;’ /

So you can apply line continuations and indentation to make the commands more readable. Similarly, this technique can be used in the command line too.

One significant difference you should note between a command line and script is that script can use tab characters for indentation while the command line uses tabs to achieve completion.

Conclusion

Shell scripting in Linux is an easy way to avoid repetitive tasks and offers automation to a certain level. There are specific rules and permissions for writing shell scripts to perform ideally. You can use several tricks to format scripts for ease working. Still, you have to be precise; otherwise, any small misjudgment or can harm the program. Sometimes a script can be slow in execution depending on the complexity of the tasks.

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/270104.html

(0)
上一篇 2022年6月24日
下一篇 2022年6月24日

相关推荐

发表回复

登录后才能评论