Basic Of Shell Scripting - Part 1

Basic Of Shell Scripting - Part 1

What is Shell Scripting?
List of commands in a program/code that is run by the Unix shell.

Why it is used?
Repetitive tasks that may be time-consuming to execute by typing one line at a time.
Running a list of commands at a specific time using cronjobs.

Basic Shell Script Structure:

#!/bin/bash --> Shebang
echo "Hello World" # "echo" to print the statements
echo $(date) # "date" is command and "$(command)" is to print execute the command
useradd Pranav # Simple command to add a user

What is Shebang?
The “#!” combo is called a shebang by most Unix geeks. This is used by the shell to decide which interpreter to run the rest of the script and is ignored by the shell that runs the script.

Types Of Variables:
1. Local Variables
2. Global / Enviorment Variables

  1. Local Variables:
    These are the variables created by the user dynamically and used for the specific script or shell. eg. Pranav

  2. Global Variables:
    These are variables that are already defined in the system and called in any script or shell. eg. date, SHELL, HOSTNAME

How To Declare The Variables:
1. Temporary Variable Declaration:

root@tryit-session:~#
root@tryit-climbing:~# name=Pranav                                                                                                                  
root@tryit-climbing:~# echo "${name}"                                                                                                               
Pranav                                                                                                                                              
root@tryit-climbing:~#

2. Permanent Variable Declaration
1. Go to /etc/bashrc OR /etc/profile and enter below text
2. name=Pranav
3. Now you will be able to use ${name} in any script or shell

What is Alias?
Aliases are mostly used to replace long commands, improving efficiency and avoiding potential spelling errors

root@tryit-climbing:~# alias list="ls -l"                                                                                                           
root@tryit-climbing:~# list                                                                                                                         
total 0                                                                                                                                             
-rw-r--r-- 1 root root 0 Apr 21 19:29 f1                                                                                                            
-rw-r--r-- 1 root root 0 Apr 21 19:29 f2                                                                                                            
-rw-r--r-- 1 root root 0 Apr 21 19:29 f3

Types Of Alias:

  1. Local A temporary alias will be active only as long as your current shell is active eg. alias l="ls -al"
  1. Permanent A permanent alias will remain in effect across different sessions.
    1. vi /etc/bashrc
    2. alias update='sudo -- sh -c "apt update && apt upgrade"'
    3. Now "update" will work as an alias for all the shell

Types Of Shell Script:

  1. Non-Interactive Script
    A shell script where no input is required from the user
    eg.

     #!/bin/bash
     echo "My Name is Pranav"
     useradd Pranav
     echo "Script is completed"
    
  1. Interactive Script
    A shell script where the user can give the dynamic input which will be used in the script
    "read" is used for reading the input for the shell script
    "${variablename}" is used for calling the variable value
    eg.
#!/bin/bash
echo "Enter the name of the user and user to add"
read name
echo "My name is ${name}"
useradd ${name}
echo "Script is completed"