Shell Scripting

                                Basic Shell Scripting


Basic Linux commands:

File descriptors

<, >, >>, 1>, 2>, 2>&1, &>

 Defaults to a success output
>   Write the output to a new file if doesn't exist and if the file exists it truncates or overwrite the 
     content of the file and write the new content to the file.
>> Write the output to a file if doesn't exist and if the file exists it appends the output into it.
1>  Success output 
2>  Error output

STD INPUT/OUTPUT options: 

1>file_name 2>file_name      -> Success and error output into same or different file_name
1>file_name 2>&1          -> Error out will be stored where you success output stores
&>file_name  -> Error out will be stored where you success output stores

Tips:

1>file_name 2>&1 and &>file_name are same but different syntax

 

Read file contents

(vi,vim, nano, cat, less, )

a) how to set line number in vi editor

vi file_name

: set number

b) how to set line number with cat command

cat -n file_name

c) how to clear screen 

clear 

OR

ctrl + l


Read a file contents with conditions 

more, tail, head, grep, awk, sed

a) more command

more -n : it will display the text upto the specified n number of lines of the file  
more +n : it will display the text from the specified n number of lines of the file  

Commands:

more -2 lines.txt
more +4 lines.txt

Example: 

--- Original file ---

# cat lines.txt
line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17
line18
line19
line20

Output:

# more +4 lines.txt    ------------------ output from line no 4
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15
line16
line17
line18
line19
line20


# more -2 lines.txt   ----------------- output only 2 lines
line1
line2
--More--(9%)


b) head command


head -> by default head command display 10 lines from a file
head -n -> it displays the top n number of lines from a file


Command:

head -2 lines.txt

Example: 
# head -2 lines.txt   -------------------- display 2 lines from the top
line1
line2


c) tail command examples

tail : by default the last command display last 10 lines from a file
tail -n : it displays the last n number of lines from a file


Command:

tail -2 lines.txt

Example: 

# tail -2 lines.txt   ----------------------- display last 2 lines

line19
line20


Read, view, and display the required range of lines from a specified file


a) If I want to display lines from 5 to 10 out of 20 lines file.

Command: 

head -10 lines.txt | tail -6
OR
awk 'NR>=5 && NR<=10 {print}' lines.txt
OR
sed -n '5,10p' lines.txt

Example:

# head -10 lines.txt | tail -6
line5
line6
line7
line8
line9
line10

# awk 'NR>=5 && NR<=10 {print}' lines.txt
line5
line6
line7
line8
line9
line10

root@ip-172-31-15-20:~# sed -n '5,10p' lines.txt
line5
line6
line7
line8
line9
line10


Grep command Examples

it's a filter command and used to search a string in a given file/multiple files and from the text.

Example file:

# cat file1.txt
THE FIRST LINE HAS BEEN WRITTEN IN UPPRE CASE.
the second line has been written in lower case.
The Third Line Has Initial Letters Are In Upper Case.
One line above the line is empty.
Here is the details of the file.
And this is the last line of the file.

a) Print the line which matches the search string "above" from a single file.

Syntax:
grep "string/pattern" file
cat file_name |grep "string/pattern"

Command: 
grep "above" file1.txt
cat file1.txt |grep "above"

Output:
# grep "above" file1.txt
One line above the line is empty.

# cat file1.txt |grep "above"
One line above the line is empty.

b) Print the line which matches the search string "above" from more than one file.

Syntax:
grep "string/pattern" file/files
command:
grep "above" file1.txt file2.txt
output:
root@ip-172-31-15-20:~# grep "above" file1.txt file2.txt
file1.txt:One line above the line is empty.
file2.txt:One line above the line is empty.

c) Grep command options:

basic options         : -i, -w, -v, -o, -n, -c, -A, -B, -C, -r, -l, -h
Advance options : -e, -f and E

-i   ------> To ignore case for matching/searching

Command: 
grep "one" file1.txt

Example:

# grep "one" file1.txt  -------------> no result without ignore case sensitive
# grep -i "one" file1.txt
One line above the line is empty.    ----------------------> result with ignore case sensitive

-w  ------> To match a whole word or exact word

Command:
 
grep -w "line" file2.txt

Example:

# grep "line" file2.txt  -------------------> multiple lines with all matches
the second line has been written in lower case.
Three lines above the line is empty.
Here is the details of the file lines.
And this is the last line of the file.

# grep -w "line" file2.txt  ---->  exact work matches the second line has been written in lower case
Three lines above the line is empty.
And this is the last line of the file.


Comments

Popular posts from this blog

Auditing in Oracle database

Steps to enable archive log mode in Postgres database

rs.stepDown() in MongoDB replication