Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting for DevOps Engineers

Basic Linux Shell Scripting

1) What is Shell Scripting?

Shell scripting is a technique in which a programmer writes a series of commands for the shell to execute.

These commands are typically used for automating tasks, such as setting up a development environment, deploying code, or performing system maintenance.

A shell script can be used to automate the process of provisioning and configuring a new server, installing necessary software, and setting up security.

Shell scripting is used to automate repetitive tasks and streamline the development and deployment process. This can include tasks such as setting up and configuring development environments, building and testing code, deploying code to various environments, and monitoring and maintaining systems.

The commands in shell scripting are written in a script file and can be executed by the shell, usually Bash, to perform the desired tasks.

2) What is #!/bin/bash?

The “#!/bin/bash” is called a shebang and it is a special type of comment that is used to specify the interpreter that should be used to execute a script.

When a script is executed, the shell looks at the first line of the script file, and checks for the shebang. If it finds it, the shell uses the interpreter specified after the shebang to execute the script. In this case, the script is executed by /bin/bash, which is a common shell on Linux and Unix systems.

#!/bin/bash is a way to specify that the script should be executed by the Bash shell.

Yes, you can use “#!/bin/sh” instead of “#!/bin/bash” to specify that the script should be executed by the sh shell.

3) Write a Shell Script that prints “I will complete #90DaysofDeVops challenge”

4) Write a Shell Script to take user input, input from arguments, and print the variables.

5) Write an Example of If else in Shell Scripting by comparing two numbers

Linux shell commands that are useful for DevOps engineers:

  1. ls: Lists files and directories in the current directory.

     ls
    
  2. cd: Changes the current directory.

     cd <directory_path>
    
  3. pwd: Displays the current working directory.

     pwd
    
  4. mkdir: Creates a new directory.

     mkdir <directory_name>
    
  5. rm: Removes files and directories.

     rm <file_name>
     rm -r <directory_name>   # Recursive removal of directories
    
  6. cp: Copies files and directories.

     cp <source_file> <destination>
     cp -r <source_directory> <destination>   # Recursive copy of directories
    
  7. mv: Moves or renames files and directories.

     mv <source> <destination>
    
  8. cat: Displays the contents of a file.

     cat <file_name>
    
  9. grep: Searches for a pattern in files.

     grep <pattern> <file_name>
    
  10. chmod: Changes the permissions of files and directories.

    chmod <permissions> <file_name>
    
  11. chown: Changes the ownership of files and directories.

    chown <owner>:<group> <file_name>
    
  12. ps: Displays the running processes.

    ps aux
    
  13. kill: Terminates a process.

    kill <process_id>
    
  14. tar: Archives and compresses files and directories.

    tar -cvf <archive_file.tar> <file_or_directory>
    tar -xvf <archive_file.tar>   # Extracts files from an archive
    
  15. wget: Downloads files from the web.

    wget <url>
    

These commands provide a starting point for working with the Linux shell. Remember to refer to the respective command's documentation or use the man the command for more detailed information and additional options.

Some command Tips useful in Shell Script.

  1. echo: Prints a message or a variable value to the terminal.

     echo "Hello, world!"
     echo $variable_name
    
  2. read: Reads input from the user and assigns it to a variable.

     read variable_name
    
  3. if: Executes a set of commands based on a condition.

     if [ condition ]; then
         # commands to execute if condition is true
     else
         # commands to execute if condition is false
     fi
    
  4. for: Loops through a list of items and executes commands.

     for item in item1 item2 item3; do
         # commands to execute for each item
     done
    
  5. while: Executes a set of commands repeatedly as long as a condition is true.

     while [ condition ]; do
         # commands to execute
     done
    
  6. case: Executes different sets of commands based on the value of a variable.

     case $variable_name in
         pattern1)
             # commands to execute for pattern1
             ;;
         pattern2)
             # commands to execute for pattern2
             ;;
         *)
             # commands to execute if no pattern matches
             ;;
     esac
    
  7. grep: Searches for a pattern in a file or output.

     grep "pattern" file_name
     command | grep "pattern"   # Search in the output of a command
    
  8. sed: Performs text manipulation and substitutions in files.

     sed 's/pattern/replacement/' file_name
    
  9. awk: Processes and manipulates text files.

     awk '{print $1}' file_name   # Prints the first field of each line
    
  10. expr: Evaluates arithmetic expressions.

    result=$(expr 5 + 3)
    
  11. sleep: Delays script execution for a specified amount of time.

    sleep 5   # Pauses for 5 seconds
    
  12. date: Retrieves the current date and time or formats it.

    current_date=$(date)
    formatted_date=$(date +"%Y-%m-%d %H:%M:%S")
    
  13. basename: Extracts the filename or directory name from a path.

    filename=$(basename $path)
    
  14. dirname: Retrieves the directory path from a file path.

    directory=$(dirname $path)
    
  15. exit: Exits the script with a specified exit code.

    exit 0   # Successful exit
    exit 1   # Exit with an error
    

In this blog post, we covered the basics of Linux shell scripting for DevOps engineers. We explored essential concepts such as variables, input/output, conditionals, loops, functions, error handling, file operations, process management, and automation. Mastering shell scripting empowers DevOps engineers to automate tasks, improve productivity, and integrate systems effectively. By leveraging these skills, DevOps engineers can streamline processes and make a significant impact in the software development and operations landscape.