Linux Shell script -introduction to implemention
Shell Scripting:
Shell scripting involves writing scripts or programs using a command-line interface to automate tasks, manage resources, and perform operations in an operating system.
Consider a list of daily responsibilities, including checking your email, backing up essential documents, and updating your schedule. You can create a shell script containing the instructions for each of these daily chores instead of having to perform them manually every day. When you run the script, the shell goes through each directive individually and completes the duties automatically, so you don’t have to. Shell scripting is a necessary skill for anyone working with Unix-based operating systems due to its versatility, portability, and effectiveness.
What is shell scripting used for?
Shell scripting is primarily used to automate repetitive system tasks, such as backing up files, monitoring system resources, and managing user accounts. By turning a series of commands into a script, system administrators can save time, increase accuracy, and simplify complex tasks.
Basic shell scripting terms
Before we delve further into shell scripting, knowing a few definitions may come in handy when exploring this topic:
· Terminal: A terminal is a program that establishes a connection with the server.
· Shell: This program interprets shell scripting commands from the terminal and runs on the server. This is the comma nd -line user interface you choose and includes shells such as the Bourne shell, Korn shell, Bourne-Again shell, and C shell.
· Script: A script is a short program that performs a specific task.
· Command-line shell: A command-line shell (also known as a command prompt) allows you to instruct your computer through textual commands.
· Shell script: A shell script is a script run through a command-line shell
Types of shell scripting:
5 types of shells
Different types of shells have different capabilities, so choose the type of shell that works best for your applications. Your shell choice will affect how you can execute programs and which system resources you can access. Some common types of shells you can choose from include:
1. Bash shell: This is the most commonly used shell by effective users in Linux and is the default shell for most Linux distributions. It is a powerful and versatile shell that is compatible with many Unix commands.
2. Zsh shell (Zsh): This is a powerful and feature-rich shell with many advanced features such as spell checking, globbing, and command line editing. It is also highly customizable and has a large user community.
3. Korn shell (ksh): This is a Unix shell that is compatible with the Bourne shell (sh) and offers many additional features to the current users such as command line editing, spelling correction, job control, and arithmetic expressions.
4. C shell (csh): This is a Unix shell that has a syntax similar to the C programming language. It includes many features such as command line editing, error status, fatal errors, history, and job control
5. Bourne shell:-The Bourne shell, also known as ‘sh,’ was the original UNIX shell or command-line interpreter developed by Stephen Bourne at AT&T Bell Labs. While known for its high operation speed, this type of shell cannot reference previous commands and has limited built-in functionality.
what is #!/bin/bash?
1. The line “#!/bin/bash” is known as the shebang or hashbang. It is a special character sequence that tells the system which interpreter to use for executing the script.
#!/bin/bash
in simple words, the she-bang at the head of the script tells the system that this file is a set of commands to be fed to the command interpreter indicated.
Operating systems have a choice of multiple shells. The shell is responsible not only for the little prompts but also interpreting the commands of the script. Thus the shell plays an important role especially when we implement big and complex logic using conditions, pipes, loops etc.
Shell Script Example: Completing the #90DaysOfDevOps Challenge:
- Let’s write a simple shell script that prints the message “I will complete the #90DaysOfDevOps challenge” when executed:
This script uses the “echo” command to output the message to the console. The shebang line at the top of the script specifies that Bash should be used as the interpreter.
To run this script, you can save it to a file with a “.sh” extension (e.g.“hello.sh”).
Thanks for reading
I hope found this useful?
✅Reshare this with others so that it helps more people
✅Tag your colleagues and friends who you think will need this