DEVOPS
DEVOPS
DEFINITION
◼ Bash scripting is a great way to automate different types of tasks in a system. Developers can avoid
doing repetitive tasks using bash scripting.
◼ Bash scripting supports variables, conditional statements, and loops just like programming languages.
KEY FEATURES OF BASH SHELL SCRIPTING
Advantages Disadvantages
◼ It helps to avoid doing repetitive tasks ◼ A new process launched for almost every shell
command executed
◼ Easy to use
◼ Slow execution speed
◼ Frequently performed tasks can be automated
◼ Compatibility problems between different
◼ A sequence of commands can be run as a single
platforms
command.
HOW TO WRITE BASH SCRIPTS?
HOW TO WRITE BASH SCRIPTS?
13 find Search for files and directories based on various find /path/to/search -name "*.txt”
criteria
14 chmod Change the permissions of a file or directory. chmod +x script.sh
21 who Displays all the users who have logged into the system who
currently.
22 cal display calendar cal
a) ls
b) cd
c) pwd
d) dir
WHAT COMMAND IS USED TO LIST THE CONTENTS OF A
DIRECTORY IN LINUX?
a) ls
b) dir
c) list
d) show
TO CREATE A NEW DIRECTORY IN LINUX, YOU WOULD USE
THE COMMAND:
a) mkdir
b) newdir
c) create
d) touch
WHAT COMMAND IS USED TO COPY FILES OR DIRECTORIES IN
LINUX?
a) mv
b) copy
c) cp
d) xcopy
TO REMOVE A FILE IN LINUX, YOU WOULD USE THE
COMMAND:
a) delete
b) rm
c) del
d) erase
WHICH COMMAND IS USED TO CHANGE THE CURRENT USER'S
PASSWORD IN LINUX?
a) passwd
b) change
c) setpasswd
d) userpassword
THE COMMAND TO DISPLAY THE LAST FEW LINES OF A TEXT
FILE IN LINUX IS:
a) head
b) tail
c) cat
d) more
TO NAVIGATE TO THE HOME DIRECTORY OF THE CURRENT
USER IN LINUX, YOU WOULD USE THE COMMAND:
a) cd /
b) cd ~
c) cd home
d) cd ..
WHICH COMMAND IS USED TO FIND FILES IN LINUX BASED ON
THEIR NAMES?
a) locate
b) search
c) find
d) grep
THE COMMAND TO VIEW THE MANUAL (HELP) FOR A SPECIFIC
COMMAND IN LINUX IS:
a) man
b) help
c) info
d) view
ANSWERS:
1 > (Output Redirects standard output to a file, overwriting the file command > output.txt
Redirection) if it already exists.
2 >> (Append Output) Redirects standard output to a file, appending the output command >> output.txt
to the end of the file if it already exists.
3 < (Input Redirection) Redirects standard input from a file instead of the command < input.txt
keyboard
4 | (Pipe) Connects the output of one command to the input of command1 | command2
another, allowing the output of the first command to
serve as input for the second.
5 2> (Standard Error Redirects standard error output to a file command 2> error.txt
Redirection)
REDIRECTION OPERATORS
S. No Command Description Example
6 2>> (Append Standard Redirects standard error output to a file, appending command 2>> error.txt
Error) to the end of the file.
7 &> or >& (Redirect Redirects both standard output and standard error command &>
Standard Output and to a file. output_and_error.txt
Standard Error)
8 <() (Process Substitution) Allows the output of a command to be used as a diff <(command1)
file in another command. <(command2)
9 >() (Process Substitution Allows the output of a command to be used as a command > >(tee
for Writing) file to write to. output.txt)
10 << (Here Document) Allows input to be taken from the lines following cat << EOF
the operator until a specified delimiter is This is a multi-line
encountered. input. It ends at the
delimiter EOF.
EOF
WHICH REDIRECTION OPERATOR IS USED TO REDIRECT
STANDARD OUTPUT TO A FILE IN LINUX?
a) >
b) <
c) >>
d) |
WHAT DOES THE 2> REDIRECTION OPERATOR DO IN LINUX?
a) >
b) >>
c) <
d) |
THE COMMAND COMMAND1 | COMMAND2 IN LINUX USES
WHICH TYPE OF REDIRECTION?
a) Input Redirection
b) Output Redirection
c) Pipe Redirection
d) Error Redirection
WHAT DOES THE &> REDIRECTION OPERATOR DO IN LINUX?
◼ It stands for standard input, and is used for taking text as an input.
◼ Linux assigns unique values 0 = stdin
◼ By default, STDIN is connected to the keyboard, and commands read input from
the keyboard unless redirected.
STDOUT (STANDARD OUTPUT)
◼ By default, STDOUT is connected to the terminal, and commands display their output on
the terminal.
◼ File Descriptor: 1
STDERR (STANDARD ERROR)
◼ It is used to output error messages and diagnostic information separate from the regular
output.
a) STDIN
b) STDOUT
c) STDERR
d) STDFILE
WHAT IS THE DEFAULT FILE DESCRIPTOR FOR STANDARD
OUTPUT IN LINUX?
a) STDIN
b) STDOUT
c) STDERR
d) STDFILE
IN A LINUX COMMAND, WHICH SYMBOL IS USED TO
REPRESENT STANDARD OUTPUT?
a) >
b) <
c) &
d) |
WHICH FILE DESCRIPTOR IS USED FOR ERROR MESSAGES IN
LINUX?
a) STDIN
b) STDOUT
c) STDERR
d) STDFILE
WHAT DOES THE SYMBOL 2> SIGNIFY IN A LINUX COMMAND?
◼ The echo command in Linux is a built-in command that allows users to display lines of text or
strings that are passed as arguments.
◼ OPTION: This is optional and can modify the behavior of the echo command.
◼ STRING: The text or message that you want to display.
EXAMPLES
2. Print a variable
4. Escape Characters
BASH VARIABLES
◼ Syntax -
◼ Example -
◼ Accessing Variable Value
◼ Concatenating Variables:
◼ String Variables
◼ Numeric Variables
◼ Boolean Variables
SPECIAL VARIABLES
◼ Positional Parameters - $0, $1, $2, ...: Represent the script or command
name, and the command-line arguments.
a) Concatenating
b) Merging
c) Joining
d) Appending
WHICH OPERATOR IS COMMONLY USED FOR CONCATENATING
STRINGS IN MANY PROGRAMMING LANGUAGES?
a) +
b) *
c) /
d) -
WHAT IS THE RESULT OF CONCATENATING THE STRINGS
"HELLO" AND "WORLD"?
a) "HelloWorld"
b) "Hello World"
c) "Hello,World"
d) "Hello-World"
IN PROGRAMMING, WHAT DATA TYPE IS TYPICALLY USED TO
REPRESENT WHOLE NUMBERS (INTEGERS)?
a) String
b) Numeric
c) Boolean
d) Character
WHICH OF THE FOLLOWING IS A BOOLEAN OPERATOR OFTEN
USED FOR LOGICAL COMPARISONS?
a) &
b) |
c) !
d) ^
ANSWERS:
TEXT PROCESSING COMMANDS OVERVIEW
Objective:
◼ Understand the purpose and usage of these commands in Linux.
GREP - GLOBAL REGULAR EXPRESSION PRINT
Definition:
◼ grep searches for patterns in text files and displays matching
lines.
Basic Syntax:
Example:
CUT - REMOVING SECTIONS FROM EACH LINE
Definition:
◼ cut removes sections from each line of a file.
Basic Syntax:
Example:
AWK - TEXT AND PATTERN PROCESSING
Definition:
◼ awk is a versatile text-processing tool for pattern scanning and processing.
Basic Syntax:
Example:
INPUT AND OUTPUT REDIRECTION
Redirection Operators:
◼ <: Redirects input from a file.
◼ >: Redirects output to a file.
◼ >>: Appends output to a file.
COMMAND CHAINING
Definition:
◼ Command chaining involves executing multiple commands sequentially or
conditionally.
Operators:
◼ ;: Execute commands sequentially.
◼ &&: Execute the second command only if the first one succeeds.
◼ ||: Execute the second command only if the first one fails.
EXAMPLE: SEQUENTIAL COMMAND CHAINING (;)
Explanation:
◼ mkdir my_directory: Creates a new directory named "my_directory."
◼ cd my_directory: Changes the current working directory to "my_directory."
◼ ls -l: Lists the contents of the current directory in a detailed format.
Result:
These commands will be executed one after the other, regardless of whether the
previous command succeeds or fails.
CONDITIONAL COMMAND CHAINING (&&)
Explanation:
◼ gcc my_program.c -o my_program: Compiles a C program named "my_program.c" and
creates an executable named "my_program."
◼ ./my_program: Executes the compiled program.
Result:
The second command (./my_program) will only execute if the compilation (gcc) is successful. If
the compilation fails, the second command won't be executed.
CONDITIONAL COMMAND CHAINING (||)
Explanation:
◼ The rm non_existent_file.txt command attempts to remove a file (non_existent_file.txt).
◼ If the file doesn't exist, the rm command will fail, and the echo "File does not exist" command
after || will be executed.
Result:
◼ The second command (./my_program) will only execute if the compilation (gcc) is successful.
If the compilation fails, the second command won't be executed.
WHICH COMMAND IS USED FOR PATTERN MATCHING AND
SEARCHING IN A TEXT FILE IN LINUX?
a) cut
b) awk
c) grep
d) sed
WHAT DOES THE CUT COMMAND DO IN LINUX?
a) >
b) <
c) >>
d) &>
IN LINUX, WHAT IS COMMAND CHAINING USED FOR?
Definition:
◼ crontab is a time-based job scheduler in Unix-like operating systems.
Syntax:
EXAMPLES