Linux shell scripting is a powerful tool that automates repetitive tasks, enhances system administration, and improves workflow efficiency. A shell script is a text file containing a series of commands that the shell executes sequentially. These scripts can automate tasks such as file manipulation, system monitoring, and software deployment, improving efficiency and reliability.
Shell scripting is the process of writing and executing of series of instructions in a sheel to automate a task. Shell script is basically a scrip or program written in a shell language such as bash, sh, zsh or powershell.
The following are the everal shell environments are available in Linux, each with unique features:
Bash: This is the most commonly used shell, known for its scripting capabilities and backward compatibility with the Bourne shell.
Zsh: It is an extended version of Bash with improved customization and interactive features.
Ksh: This offers advanced scripting functionalities and performance improvements over Bash.
Fish: This focuses on usability with features like syntax highlighting and auto-suggestions.
A basic shell script to create multiple folders and muliple linux users at once
This is usually at the beginning of every shell script which helps to specify the interpreter to execute the script. It is a special notation used in Unix-like operating system like Linux, to specify the interpreter that should execute the script. In this case, #!/bin/bash specificially indicates that the Bash shell should be used to interpret and execute the script.
Bash is one of the shell tool used to interpret script. If we wanted to use another shell tool like sh, shebang would be updated to #!/bin/sh.
/bin/bash: This is the absolute path to the Bash shell execuable. It tells the system to use he Bash interpreter locaed in the /bin/bash to execute the script.
'mkdir shell-scripting'
'vim my_first_shell_script.sh'
'#!/bin/bash
mkdir Folder1 mkdir Folder2 mkdir Folder3
sudo useradd user1 sudo useradd user2 sudo useradd user3'
i = to activate the insert mode.
:wq = to save and exit vim.
'cd shell-scripting'
'ls -latr'
The above image indicates the various permissions which are assigned to the file that is "-rw-r--r--". This means
- The owner of the file has read (r) and write (w) permissions.
- Members of the file group has read (r) permission.
- Other groups have read (r) permission.
However, none of the categories have the execute (x) permission o the file.
Normally to execute this file, you run the below script command
'./my_first_shell_script.sh'
This will print permission denied because it doesn't have permission to execute (x).
The "./" prefix in the file indicates the command to locate the current directory.
- The dot (.) indicates the current directory.
- The slash (/) is a directory separator.
Step 5: Add the execute permission for the 'owner' to be able to execute the shell script and run it.
'chmod +x my_first_shell_script.sh'
'./my_first_shell_script.sh'
'ls'
'id user1'
'id user2'
'id user3'
In programming generally, not just shell scripting, variables are essential for creating dynamic and flexible programs.
In Linux shell scripting, variables store data and are declared without a datatype. Initialization assigns a value using = without spaces (e.g., var="Hello"). Use $var to reference it. Variables can be local or environmental (export VAR=value). Quoting prevents unintended expansion, ensuring proper handling of strings and special characters.
For example assign "John" as value to a variable "name".
'name="John"'
After assigning a value to a variable, as shown in the example above where "John" was assigned to the variable "name", you can utilize his variable in various ways in he scrip or program. To retrieve the value from the variable, it is done by echoing on the console using the "echo command" in shell scripting.
For example to retrieve "John" from the name variable by typing the command below and executing it.
'echo $name'
'name="John"
'echo $name'
'echo My name is $name'