Linux Shell Scripting and Basic commands
Table of contents
No headings in the article.
Title: Day 3 Task: Basic Linux Commands and Introduction to Shell Scripting for DevOps
Introduction: In the world of DevOps, having a solid understanding of Linux commands and shell scripting is essential. Linux commands enable efficient file management, system control, and automation, while shell scripting allows for the execution of multiple commands in a systematic and automated manner. In this blog post, we will explore basic Linux commands and delve into the fundamentals of shell scripting, along with examples to help you grasp the concepts.
Basic Linux Commands:
To view what's written in a file: The
cat
command is used to display the contents of a file. For instance:cat example.txt
will show the contents of the fileexample.txt
.To change the access permissions of files: The
chmod
command is used to modify file permissions. For example, to give the owner read, write, and execute permissions, you can use:chmod u=rwx file.txt
.To check which commands you have run till now: The
history
command displays a list of previously executed commands, showing the command history.To remove a directory/folder: The
rmdir
command is used to remove an empty directory. For example:rmdir directory_name
will delete the specified directory.To create a fruits.txt file and view the content: The
touch
command is used to create a file. To createfruits.txt
, you can use:touch fruits.txt
. To view its contents, use thecat
command.To add content in devops.txt (One in each line): You can use the
echo
command to add content to a file. For example:echo "Apple" >> devops.txt echo "Mango" >> devops.txt echo "Banana" >> devops.txt
To show only the top three fruits from the file: The
head
command can be used to display the first few lines of a file. For example:head -n 3 fruits.txt
will display the top three lines.To show only the bottom three fruits from the file: The
tail
command is used to display the last few lines of a file. For instance:tail -n 3 fruits.txt
will show the bottom three lines.To create another file Colors.txt and view the content: Use the
touch
command to create theColors.txt
file:touch Colors.txt
. Then use thecat
command to view its contents.To find the difference between fruits.txt and Colors.txt files: The
diff
command is used to compare and find differences between two files. For example:diff fruits.txt Colors.txt
will display the differences, if any.
Introduction to Shell Scripting for DevOps: Shell scripting involves writing computer programs to automate tasks and perform operations within a Linux shell. It allows for the execution of a series of commands in a sequential manner. Shell scripts can handle file manipulation, program execution, and printing of text. They provide flexibility, efficiency, and repeatability in automating tasks.
#!/bin/bash vs. #!/bin/sh: The #!/bin/bash
is called a shebang or hashbang and specifies the interpreter (in this case, Bash) that should be used to execute the script. Bash is the default shell on most Linux distributions and provides extended functionality.
Alternatively, #!/bin/sh
specifies the generic Bourne shell. While it is compatible with Bash, it may lack some advanced features. Choosing between the two depends on the specific requirements of the script and the desired compatibility.
Shell Script Examples:
Shell Script to print "I will complete #90DaysOfDevOps challenge":
#!/bin/bash echo "I will complete #90DaysOfDevOps challenge"
Shell Script to take user input, input from arguments, and print the variables:
#!/bin/bash read -p "Enter your name: " name echo "Hello, $name!" echo "Argument 1: $1" echo "Argument 2: $2"
Example of if-else in Shell Scripting by comparing two numbers:
#!/bin/bash num1=10 num2=20 if [ $num1 -gt $num2 ]; then echo "$num1 is greater than $num2" else echo "$num1 is less than or equal to $num2" fi
Conclusion: Mastering basic Linux commands and understanding shell scripting is crucial for DevOps professionals. With a solid foundation in Linux commands, you can efficiently manage files, directories, and permissions. Shell scripting empowers you to automate tasks, execute commands sequentially, and enhance your productivity. Embrace these skills, explore advanced features, and embark on your DevOps journey with confidence. Happy scripting!