90 Days of journey with trainwithshubham shell scripting command make us master in devops journey
Shell scripting is a powerful way to automate tasks on Unix-like operating systems such as Linux. Here are some common shell script commands:
Shebang: Indicates the interpreter to be used for executing the script.
#!/bin/bash
Comments: Used for adding comments in the script for documentation.
# This is a comment
Variables: Used to store data.
variable_name="value"
Echo: Prints text or variables to the terminal.
echo "Hello, world!" echo "The value of variable is: $variable_name"
Read: Reads input from the user.
read -p "Enter your name: " name
If Statement: Conditional execution based on a condition.
COPY
if [ condition ]; then # code block fi
For Loop: Executes a block of code a fixed number of times.
COPY
COPY
for i in {1..2}; do # code block done
While Loop: Executes a block of code repeatedly as long as a condition is true.
while [ condition ]; do # code block done
Functions: Blocks of reusable code.
codemy_function() { # code block }
Command Substitution: Executes a command and stores the output in a variable.
result=$(command)
Conditional Execution: Execute commands based on the success or failure of previous commands.
command1 && command2 # command2 runs if command1 succeeds command1 || command2 # command2 runs if command1 fails
File Operations:
Create a file:
touch filename
Remove a file:
rm filename
Check if a file exists:
if [ -f filename ]; then ...
Check if a directory exists:
if [ -d directoryname ]; then ...
Directory Navigation:
Change directory:
cd directoryname
List files and directories:
ls
Make directory:
mkdir directoryname
Remove directory:
rmdir directoryname
String Manipulation:
Substring: Extracts a portion of a string.
substring=${string:position:length}
Concatenation: Combines two or more strings.
concatenated="$string1$string2"
File Permissions:
Change Permissions:
chmod
command.chmod permissions filename
View Permissions:
ls -l
command.
Input/Output Redirection:
Redirect Output: Sends the output of a command to a file.
command > output_file
Redirect Input: Provides input to a command from a file.
command < input_file
Piping:
Pipe (|): Sends the output of one command as input to another command.
command1 | command2
Advanced Control Structures:
Case Statement: Executes different blocks of code based on matching patterns.
case expression in pattern1) # code block ;; pattern2) # code block ;; *) # default code block ;; esac
Debugging:
Print Debugging: Inserting
echo
statements for debugging purposes.Debugging Flags: Using
set -x
to enable debugging mode.#!/bin/bash set -x # script content
Functions:
Passing Arguments: Functions can accept arguments.
codemy_function() { arg1=$1 arg2=$2 # code block } my_function value1 value2
Exit Codes:
Exit Status: Every command returns an exit status.
Checking Exit Codes:
$?
variable holds the exit status of the last command.command if [ $? -eq 0 ]; then echo "Command successful" else echo "Command failed" fi
Hshsh Operations:
Using
expr
:expr
command for basic arithmetic.sum=$(expr $num1 + $num2)
These command make you expert in Linux as well Devops journey