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:
ls: Lists files and directories in the current directory.
ls
cd: Changes the current directory.
cd <directory_path>
pwd: Displays the current working directory.
pwd
mkdir: Creates a new directory.
mkdir <directory_name>
rm: Removes files and directories.
rm <file_name> rm -r <directory_name> # Recursive removal of directories
cp: Copies files and directories.
cp <source_file> <destination> cp -r <source_directory> <destination> # Recursive copy of directories
mv: Moves or renames files and directories.
mv <source> <destination>
cat: Displays the contents of a file.
cat <file_name>
grep: Searches for a pattern in files.
grep <pattern> <file_name>
chmod: Changes the permissions of files and directories.
chmod <permissions> <file_name>
chown: Changes the ownership of files and directories.
chown <owner>:<group> <file_name>
ps: Displays the running processes.
ps aux
kill: Terminates a process.
kill <process_id>
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
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.
echo: Prints a message or a variable value to the terminal.
echo "Hello, world!" echo $variable_name
read: Reads input from the user and assigns it to a variable.
read variable_name
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
for: Loops through a list of items and executes commands.
for item in item1 item2 item3; do # commands to execute for each item done
while: Executes a set of commands repeatedly as long as a condition is true.
while [ condition ]; do # commands to execute done
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
grep: Searches for a pattern in a file or output.
grep "pattern" file_name command | grep "pattern" # Search in the output of a command
sed: Performs text manipulation and substitutions in files.
sed 's/pattern/replacement/' file_name
awk: Processes and manipulates text files.
awk '{print $1}' file_name # Prints the first field of each line
expr: Evaluates arithmetic expressions.
result=$(expr 5 + 3)
sleep: Delays script execution for a specified amount of time.
sleep 5 # Pauses for 5 seconds
date: Retrieves the current date and time or formats it.
current_date=$(date) formatted_date=$(date +"%Y-%m-%d %H:%M:%S")
basename: Extracts the filename or directory name from a path.
filename=$(basename $path)
dirname: Retrieves the directory path from a file path.
directory=$(dirname $path)
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.