Improve Command Line Productivity - RHCSA (RH134).pdf
support8872
948 views
18 slides
Oct 07, 2024
Slide 1 of 18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
About This Presentation
Slides on how to improve command line productivity - Part of RHCSA (RH134) syllabus
Size: 430.03 KB
Language: en
Added: Oct 07, 2024
Slides: 18 pages
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