Improve Command Line Productivity - RHCSA (RH134).pdf

support8872 948 views 18 slides Oct 07, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Slides on how to improve command line productivity - Part of RHCSA (RH134) syllabus


Slide Content

Improve Command line utility:
Shell Scripts

Shell
●CLI which allows user to interact with OS
●For example :-
○In linux sh, bash etc. is a shell

●Use echo $0 to get your shell name
●Available shells are stored in /etc/shells

For knowing the current shell name
●Command: echo $0
To know the specific version of the shell being used
●Command: echo $BASH_VERSION

List all the available shells on the system
●Command: cat /etc/shells

Verify which shell is assigned to a user
●Command: cat /etc/passwd

Shell Scripts
Executable file comprising a series of shell commands executed in sequence.
Within the file, you can include:
●Shell declaration (#!/bin/bash)
●Commentary lines (# comments)
●Various commands (echo, cp, grep, etc.)
●Control statements (if, while, for, etc.)
●Ensure executable permissions (e.g., -rwx r-x r-x)
●Call script using absolute path (e.g.,-/home/userdir/script.bash)
●Calling from the current directory, use ./script.bash

Example

If-Then & loop scripts
●If-Then: Conditional execution of code.
● Syntax:
`if [ condition ]; then action; fi`

●loop: Repeated execution of code.
●Syntax for 'for' loop:
`for i in {start..end}; do action; done`

Create a shell file and open the nano editor
●Command: nano <file_name> .sh
Execution of if-then

Make the script executable
●Command: chmod +x <file_name>
Run the script
●Command: ./<file_name>


Note: Since we did not create any such file with the name example.txt
so it’s giving the output as File example.txt does not exist

For loop execution
Create a shell file and open it in nano editor
●Command: nano <file_name> .sh

Make the script executable
●Command: chmod +x <file_name>
Run the script
●Command: ./<file_name>

While loop execution
Create a shell file and open it in nano editor
●Command: nano <file_name> .sh

Make the script executable
●Command: chmod +x <file_name>
Run the script
●Command: ./<file_name>

grep
●Command-line utility used for searching text patterns within
files
●Syntax:
●[options]: Optional flags that modify the behavior of grep.
●pattern: The text pattern to search for.
●[file...]: files to search within
●To search for more than one keyword use :
egrep –i “keyword|keyword2” file

grep options
●-i: Ignore case distinctions.
●-n: Display line numbers along with matching lines.
●-l: Display only the names of files containing matches.
●-c: Display count of matching lines
●-o: Show only the matching part of the lines
●-v: Display lines that do not match the pattern
●-w: Match whole words
●-r or -R: Recursively search directories for matching files

egrep
●Stands for "Global Regular Expressions Print"

●more suitable for complex pattern matching because it
allows metacharacters like +, ?, and | without needing to
escape them

●can search for multiple patterns in a single command