0% found this document useful (0 votes)
6 views74 pages

DEVOPS

Bash scripting is a command processor that automates tasks in a system, supporting variables, conditionals, and loops. It allows users to write scripts for task automation and includes features like interactivity and control structures. The document also covers general commands, redirection operators, and job scheduling with crontab.

Uploaded by

st9415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views74 pages

DEVOPS

Bash scripting is a command processor that automates tasks in a system, supporting variables, conditionals, and loops. It allows users to write scripts for task automation and includes features like interactivity and control structures. The document also covers general commands, redirection operators, and job scheduling with crontab.

Uploaded by

st9415
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

BASH SCRIPTING

DEFINITION

◼ Bash (Bourne Again SHell) is a command processor that


typically runs in a text window where the user types
commands that cause actions.
PURPOSE

◼ 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

• Interactivity: Bash allows users to interact with the system in a command-line


environment.
• Scripting: Users can write scripts to automate tasks by combining multiple commands.
• Variables: Store and manipulate data using variables.
• Control Structures: Implement conditional statements and loops.
• Functions: Encapsulate code for reusability.
BASH SHELL

Advantages Disadvantages

◼ It is simple. ◼ Any mistake while writing can be costly.

◼ 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?

◼ Create a file with the .sh extension.

◼ Write down the bash scripts within it

◼ Provide execution permission to it

◼ Execute the file


GENERAL PURPOSE COMMANDS

S. No Command Description Example

1 ls List files and directories in the current directory. ls

2 cd Change the current working directory. cd /path/to/directory

3 pwd Print the current working directory. pwd

4 cp Copy files or directories. cp file1.txt /path/to/destination

5 mv Move or rename files or directories. mv file1.txt newfile.txt

6 rm Remove/delete files or directories. rm file.txt


GENERAL PURPOSE COMMANDS

S. No Command Description Example

7 mkdir Create a new directory mkdir new_directory

8 rmdir Remove an empty directory. rmdir empty_directory

9 touch Create an empty file or update the timestamp of an touch new_file.txt


existing file
10 cat Display the contents of a file. cat file.txt

11 echo Display a message or the value of a variable. echo "Hello, World!"

12 Grep Search for a pattern in a file. grep "pattern" file.txt


GENERAL PURPOSE COMMANDS

S. No Command Description Example

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

15 chown Change the owner of a file or directory chown user:group file.txt

16 ps Display information about running processes. ps aux

17 kill Terminate a process kill PID

18 df Display information about disk space usage. df -h


GENERAL PURPOSE COMMANDS

S. No Command Description Example

19 du Display the disk usage of files and directories. du -h

20 man Display the manual (documentation) for a command. man ls

21 who Displays all the users who have logged into the system who
currently.
22 cal display calendar cal

23 clear clean up the terminal clear

24 history get list of previous commands may be obtained history


WHICH COMMAND IS USED TO DISPLAY THE CURRENT DIRECTORY
IN LINUX?

WHICH COMMAND IS USED TO DISPLAY THE CURRENT DIRECTORY IN


LINUX?

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.C 2.A 3.A 4.C 5.B

6.A 7.B 8.B 9.C 10.A


REDIRECTION OPERATORS
S. No Command Description Example

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) Redirects standard output to a file.

b) Redirects standard error to a file.

c) Appends standard output to a file.

d) Creates a new file.


TO APPEND THE OUTPUT OF A COMMAND TO AN EXISTING FILE
IN LINUX, YOU WOULD USE THE REDIRECTION OPERATOR:

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?

a) Redirects standard output and standard error to a file.

b) Redirects standard input from a file.

c) Redirects standard output to a file.

d) Redirects standard error to the screen.


ANSWERS
STDIN (STANDARD INPUT)

◼ 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)

◼ STDOUT is the standard output stream.

◼ By default, STDOUT is connected to the terminal, and commands display their output on
the terminal.

◼ File Descriptor: 1
STDERR (STANDARD ERROR)

◼ STDERR is the standard error stream

◼ It is used to output error messages and diagnostic information separate from the regular
output.

◼ Like STDOUT, STDERR is connected to the terminal by default.


WHICH FILE DESCRIPTOR IS ASSOCIATED WITH STANDARD
INPUT IN LINUX?

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?

a) Redirects Standard Output to a file.

b) Redirects Standard Input from a file.

c) Redirects Standard Error to a file.

d) Redirects both Standard Output and Standard Error to a file.


ANSWERS:
ECHO 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

1. Print a Simple Message

2. Print a variable

3. Display without newline

4. Escape Characters
BASH VARIABLES

◼ Data is stored at a particular memory address and then


◼ Variables let you store, read, access, and manipulate data. It can be accessed as well as modified when
required

◼ 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.

◼ Environment Variables: $HOME, $USER, $PATH, etc.: Represent system


or user-specific information
◼ Performing Arithmetic

◼ Local and Global Scope


IN PROGRAMMING, WHAT IS THE PROCESS OF COMBINING TWO
OR MORE STRINGS INTO A SINGLE STRING CALLED?

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

Text Processing Commands:


◼ grep, cut, awk.
◼ Essential tools for manipulating and extracting information from text
data.

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) Extracts specific columns from a text file

b) Searches for patterns in a text file

c) Replaces text in a file

d) Concatenates multiple files


WHAT IS THE PRIMARY PURPOSE OF THE AWK COMMAND IN
LINUX?

a) Pattern matching and searching

b) Text extraction based on delimiters

c) Replacing text in a file

d) Sorting lines in a file


TO REDIRECT THE OUTPUT OF A COMMAND TO A FILE IN
LINUX, WHICH SYMBOL IS COMMONLY USED?

a) >

b) <

c) >>

d) &>
IN LINUX, WHAT IS COMMAND CHAINING USED FOR?

a) Concatenating text files

b) Running multiple commands sequentially

c) Extracting specific columns from a text file

d) Searching for patterns in a file


ANSWERS:
SCHEDULING JOBS WITH CRONTAB

Definition:
◼ crontab is a time-based job scheduler in Unix-like operating systems.

Syntax:
EXAMPLES

◼ Example 1: Run a Command Every Minute

◼ Example 2: Schedule a Daily Backup at 2 AM

◼ Example 3: Schedule a Weekly Task on Mondays

◼ Example 4: Run a Script on the 15th of Every Month

◼ Example 5: Run a Command Every Hour

You might also like