day3 record big data learning Shell

Posted by banks1850 on Fri, 14 Jan 2022 16:15:42 +0100

1, Shell overview

Shell is a command line interpreter that receives the application / user commands and then invokes the kernel of the operating system.

1.1 Shell parser provided by Linux

[root@hadoop100 ~]# cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

1.2 the default parser of CentOS is bash

[root@hadoop100 ~]# echo $SHELL
/bin/bash

2, Getting started with Shell scripts

2.1 script format

Script to #/ Start with bin/bash (specify parser)

2.2 first Shell script: helloworld

Requirements: create a Shell script and output helloworld

[root@hadoop100 ~]# touch helloworld.sh
[root@hadoop100 ~]# vim helloworld.sh 

#!/bin/bash
echo "helloworld"

2.3 common execution methods of scripts

2.3.1 relative path or absolute path of bash or sh + script (script + x permission is not required)

[root@hadoop100 ~]# sh helloworld.sh 
helloworld

2.3.2 execute the script using the absolute path or relative path of the input script (must have executable permission + x)

2.3.2.1 firstly, HelloWorld + x permissions for SH script

[root@hadoop100 ~]# chmod +x helloworld.sh 

2.3.2.2 execution script

[root@hadoop100 ~]# ./helloworld.sh relative path

Note: the essence of the first execution method is that the bash parser helps you execute the script, so the script itself does not need execution permission. The second execution method is essentially that the script needs to be executed by itself, so it needs execution permission

2.3.3 add "." before the path of the script

[root@hadoop100 ~]# . helloworld.sh 
helloworld

The first two methods are to open a sub shell in the current shell to execute the script content. When the script content ends, the sub shell closes and returns to the parent shell.
The third way is to add a before the script path The script content can be executed in the current shell without opening the sub shell!
The difference between opening a child shell and not opening a child shell is that the inheritance relationship of environment variables, such as the current variable set in the child shell, is invisible to the parent shell.

3, Variable

3.1 system predefined variables

$HOME,$PWD,$SHELL,$USER etc.
[root@hadoop100 ~]# echo $HOME
/root

3.2 user defined variables

3.2.1 basic grammar

(1) Define variable: variable = value
(2) Undo variable: unset variable
(3) Declare static variable: readonly variable. Note: unset is not allowed

3.2.2 variable definition rules

(1) Variable names can be composed of letters, numbers and underscores, but they cannot start with numbers. Environment variable names are recommended to be capitalized.
(2) There must be no spaces on either side of the equal sign
(3) In bash, the default types of variables are all string types, and numerical operations cannot be performed directly.
(4) If there are spaces in the value of the variable, it needs to be enclosed in double quotation marks or single quotation marks.

3.2.3 case practice

#Define variable A
[root@hadoop100 ~]# A=5
[root@hadoop100 ~]# echo $A
5
#Undo variable A
[root@hadoop100 ~]# unset A
[root@hadoop100 ~]# echo $A

#In bash, the default types of variables are all string types, and numerical operations cannot be performed directly
[root@hadoop100 ~]# C=1+2
[root@hadoop100 ~]# echo $C
1+2

#The variable can be promoted to a global environment variable, which can be used by other Shell files (in the current Shell object)
[root@hadoop100 ~]# C=2
[root@hadoop100 ~]# touch test.sh
[root@hadoop100 ~]# vim test.sh
#!/bin/bash
echo "helloworld"
echo $C
[root@hadoop100 ~]# export C
[root@hadoop100 ~]# bash test.sh
helloworld
2

3.3 special variables

3.3.1 $n

3.3.1.1 basic grammar

$n (function description: n is a number, $0 represents the script name, $1- 9 generation surface The first one reach The first nine individual ginseng number , ten with upper of ginseng number , ten with upper of ginseng number need want use large Include number package contain , as 9 represents the first to ninth parameters. Parameters above ten need to be contained in braces, such as 9 represents the first to ninth parameters. Parameters above ten need to be contained in braces, such as {10})

3.3.1.2 case practice

[root@hadoop100 ~]# touch parameter.sh 
[root@hadoop100 ~]# vim parameter.sh 
#! /bin/bash
echo "$0  $1   $2"

[root@hadoop100 ~]# chmod 777 parameter.sh 
[root@hadoop100 ~]# ./parameter.sh hh aa
./parameter.sh  hh   aa

3.3.2 $#

3.3.2.1 basic grammar

$# (function description: get the number of all input parameters, commonly used for loop)

3.3.2.2 case practice

#! /bin/bash
echo "$0  $1   $2"
echo $#
[root@hadoop100 ~]# chmod 777 parameter.sh 
[root@hadoop100 ~]# ./parameter.sh hh aa
./parameter.sh  hh   aa
2

3.3.3 $*

3.3.3.1 basic grammar

$*	(Function Description: this variable represents all parameters in the command line, $*Take all parameters as a whole)
$@	(Function Description: this variable also represents all parameters in the command line, but $@(treat each parameter differently)

3.3.3.2 case practice

[root@hadoop100 ~]# vim parameter.sh 
#! /bin/bash
echo "$0  $1   $2"
echo $#
echo $*
echo $@

[root@hadoop100 ~]# ./parameter.sh 1 2 3
./parameter.sh  1   2
3
1 2 3
1 2 3

3.3.4 $?

3.3.4.1 basic grammar

$? (function description: the return status of the last executed command. If the value of this variable is 0, it proves that the last command was executed correctly; if the value of this variable is non-0 (the specific number is determined by the command itself), it proves that the last command was executed incorrectly.)

3.3.4.2 case practice

[root@hadoop100 ~]# vim parameter.sh 
[root@hadoop100 ~]# echo $?
0

4, Operator

4.1 basic grammar

" ( ( transport count type ) ) " or " ((expression)) "or“ ((expression)) "or" [expression] "

4.2 case practice

#Calculate the value of (2 + 3) X4
[root@hadoop100 ~]# S=$[(2+3)*4]
[root@hadoop100 ~]# echo $S
20

5, Conditional judgment

5.1 basic grammar

(1)test condition
(2) [condition] (note that there should be a space before and after the condition)
Note: if the condition is not empty, it is true, [test] returns true and [] returns false.

5.2 common judgment conditions

(1)Comparison between two integers
= string comparison
-lt Less than( less than)			-le Less than or equal to( less equal)
-eq Equals( equal)				-gt Greater than( greater than)
-ge Greater than or equal to( greater equal)		-ne Not equal to( Not equal)

(2)Judge according to file permissions
-r Have read permission( read)			-w Have write permission( write)
-x Have permission to execute( execute)

(3)Judge by file type
-f The file exists and is a regular file( file)
-e File exists( existence)		-d The file exists and is a directory( directory)

5.3 case practice

# Is 23 greater than or equal to 22
[root@hadoop100 ~]# [ 23 -ge 22 ]
[root@hadoop100 ~]# echo $?
0

# helloworld. Whether SH has write permission
[root@hadoop100 ~]# [ -w helloworld.sh ]
[root@hadoop100 ~]# echo $?
0

# helloworld. Does the SH file exist
[root@hadoop100 ~]# [ -e helloworld.sh ]
[root@hadoop100 ~]# echo $?
0

# Multi condition judgment (& & indicates that the next command is executed only after the previous command is executed successfully, | | indicates that the next command is executed only after the previous command fails to execute)
[root@hadoop100 ~]# [ test ] && echo OK || echo notOK
OK
[root@hadoop100 ~]# [ test ] && [ ] || echo notOK
notOK

6, Process control (key)

6.1 if judgment

6.1.1 basic grammar

6.1.1.1 single branch

if [ Conditional judgment ];then 
  program 
fi 

perhaps

if  [ Conditional judgment ] 
  then 
    program 
fi

6.1.1.2 multi branch

if [ Conditional judgment ] 
  then 
    program 
elif [ Conditional judgment ]
	then
		program
else
	program
fi

matters needing attention:
(1) [conditional judgment], there must be a space between brackets and conditional judgment
(2) There should be a space after if

6.1.2 case practice

Input A number, if it is 1, output A, if it is 2, output B, if it is other, output nothing

[root@hadoop100 ~]# touch if.sh
[root@hadoop100 ~]# vim if.sh 

#!/bin/bash
if [ $1 -eq "1" ]
 then
echo "A"
elif [ $1 -eq "2" ]
 then
echo "B"
fi

[root@hadoop100 ~]# chmod 777 if.sh 
[root@hadoop100 ~]# ./if.sh 1
A
[root@hadoop100 ~]# ./if.sh 2
B
[root@hadoop100 ~]# ./if.sh 3

6.2 case statement

6.2.1 basic grammar

case $Variable name in 
  "Value 1") 
    If the value of the variable is equal to the value 1, execute program 1 
    ;; 
  "Value 2") 
    If the value of the variable is equal to the value 2, execute program 2 
    ;; 
  ...Omit other branches 
  *) 
    If none of the values of the variables are the above values, execute this procedure 
    ;; 
esac

matters needing attention:
(1) The end of the case line must be the word "in", and each pattern match must end with a closing parenthesis ")".
(2) Double semicolon ";" Indicates the end of the command sequence, which is equivalent to break in java.
(3) The last "*" indicates the default mode, which is equivalent to default in java.

6.2.2 case practice

Input A number, if it is 1, output A; if it is 2, output B; if it is other, output C

[root@hadoop100 ~]# touch case.sh
[root@hadoop100 ~]# vim case.sh 
#! /bin/bash
case $1 in
"1")
        echo "A"
;;
"2")
        echo "B"
;;
*)
        echo "C"
;;
esac

[root@hadoop100 ~]# chmod 777 case.sh 
[root@hadoop100 ~]# ./case.sh 1
A
[root@hadoop100 ~]# ./case.sh 2
B
[root@hadoop100 ~]# ./case.sh 4
C

6.3 for loop

6.3.1 basic grammar

for (( Initial value;Cycle control conditions;Variable change )) 
  do 
    program 
  done
for variable in Value 1 value 2 value 3 
  do 
    program 
  done

6.3.2 case practice

From 1 to 100

[root@hadoop100 ~]# touch for1.sh
[root@hadoop100 ~]# vim for1.sh 
#! /bin/bash
sum=0
for((i=0;i<=100;i++))
do
	sum=$[$sum+$i]
done
echo $sum

[root@hadoop100 ~]# chmod 777 for1.sh 
[root@hadoop100 ~]# ./for1.sh 
5050

Print all input parameters

[root@hadoop100 ~]# touch for2.sh
[root@hadoop100 ~]# vim for2.sh 
#! /bin/bash
for i in $*
 do
        echo "haha love $i"
 done

[root@hadoop100 ~]# chmod 777 for2.sh 
[root@hadoop100 ~]# ./for2.sh A B C
haha love A
haha love B
haha love C

6.4 while loop

6.4.1 basic grammar

while [ Conditional judgment ] 
  do 
    program
  done

6.4.2 case practice

From 1 to 100

[root@hadoop100 ~]# touch while.sh
[root@hadoop100 ~]# vim while.sh 
#! /bin/bash
i=0
sum=0
while [ $i -le 100 ]
 do
  sum=$[$sum+$i]
  i=$[$i+1]
 done
echo $sum

[root@hadoop100 ~]# chmod 777 while.sh 
[root@hadoop100 ~]# ./while.sh 
5050

7, Read read console input

7.1 basic grammar

read(option)(parameter)
Options:
-p: Specify the prompt when reading the value;
-t: Specifies the time (in seconds) to wait while reading the value.
parameter
	Variable: Specifies the variable name of the read value

7.2 case practice

Within 7 seconds of prompt, read the name entered on the console

[root@hadoop100 ~]# touch read.sh
[root@hadoop100 ~]# vim read.sh 
#! /bin/bash
read -t 7 -p "Please enter your name within 7 seconds" NAME
echo $NAME

[root@hadoop100 ~]# chmod 777 read.sh 
[root@hadoop100 ~]# ./read.sh 
Please enter your name within 7 seconds haha
haha

8, Custom function

8.1 basic grammar

[ function ] funname[()]
{
	Action;
	[return int;]
}
funname

(1) The function must be declared before calling the function. The shell script runs line by line. It won't compile first like other languages.
(2) Function return value can only be through $? If the system variable is obtained, it can display plus: return. If it is not added, the result of the last command will be used as the return value. Return followed by the value n(0-255)

8.2 case practice

Calculate the sum of the two input parameters

[root@hadoop100 ~]# touch fun.sh
[root@hadoop100 ~]# vim fun.sh 
#!/bin/bash
function sum(){
 s=0
 s=$[$1+$2]
 echo "$s"
}

read -p "Enter the first number:" n1
read -p "Enter the second number:" n2
sum $n1 $n2

[root@hadoop100 ~]# chmod 777 fun.sh 
[root@hadoop100 ~]# ./fun.sh 
Enter the first number: 5
 Enter the second number: 7
12

9, Shell tool (key)

9.1 cut

The cut command cuts bytes, characters, and fields from each line of the file and outputs them.

9.1.1 basic grammar

cut [Option parameters]  filename
 Description: the default separator is tab
 Option parameters		function
-f			Column number, which column to extract
-d			Separator to split the column according to the specified separator
-c			Specify specific characters

9.1.2 case practice

IP address printed after cutting ifconfig

[root@hadoop100 ~]# ifconfig | grep "netmask" | cut -d "t" -f 2 | cut -d " " -f 2
192.168.149.100
127.0.0.1
192.168.122.1

9.2 sed (understanding)

9.3 awk

A powerful text analysis tool, read the file line by line, slice each line with a space as the default separator, and then analyze the cut part

9.3.1 basic usage

awk [Option parameters] 'pattern1{action1}  pattern2{action2}...' filename
pattern: express AWK The content found in the data is the matching pattern
action: A series of commands executed when a match is found
 Option parameters		function
-F			Specifies the input file separator
-v			Assign a user-defined variable

9.3.2 case practice

[root@hadoop100 ~]# cp /etc/passwd ./

search passwd File to root Keyword and output the 7th column of the row
[root@hadoop100 ~]# awk -F: '/^root/{print $7}' passwd 
/bin/bash

Show only/etc/passwd The first and seventh columns of are separated by commas, and column names are added before all rows user,shell Add on the last line"ceshi,/bin/wancheng"
[root@hadoop100 ~]# awk -F : 'BEGIN{print "user, shell"} {print $1","$7} END{print "ceshi,/bin/wancheng"}' passwd
user, shell
root,/bin/bash
bin,/sbin/nologin
daemon,/sbin/nologin
adm,/sbin/nologin
.....
shutdown,/sbin/shutdown
ceshi,/bin/wancheng

9.4 built in variables of awk

variable			explain
FILENAME	file name
NR			Number of records read
NF			Number of fields for browsing records (number of columns after cutting)
cutting IP
[root@hadoop100 ~]# ifconfig | grep "netmask"
        inet 192.168.149.100  netmask 255.255.255.0  broadcast 192.168.149.255
        inet 127.0.0.1  netmask 255.0.0.0
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
[root@hadoop100 ~]# ifconfig | grep "netmask" | awk -F "inet" '{print $2}'
 192.168.149.100  netmask 255.255.255.0  broadcast 192.168.149.255
 127.0.0.1  netmask 255.0.0.0
 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
[root@hadoop100 ~]# ifconfig | grep "netmask" | awk -F "inet" '{print $2}'| awk -F " " '{print $1}'
192.168.149.100
127.0.0.1
192.168.122.1
query kong.txt Line number of the empty line
[root@hadoop100 ~]# touch kong.txt
[root@hadoop100 ~]# vim kong.txt 
[root@hadoop100 ~]# cat kong.txt 
haohaoxuexi tiantian

xiangshanghaha
[root@hadoop100 ~]# awk '/^$/ {print NR}' kong.txt
2

9.5 sort

The sort command is very useful in Linux. It sorts files and outputs the sorting results as standard

9.5.1 basic grammar

sort(option)(parameter)
option	explain
-n	Sort by value
-r	Sort in reverse order
-t	Sets the separator character used when sorting
-k	Specify the columns to sort

9.5.2 case practice

[root@hadoop100 ~]# touch sort.sh
[root@hadoop100 ~]# vim sort.sh 
s:10:3
ss:30:1
bd:20:4
xz:50:2

Sort in reverse order according to the third column divided by ":
[root@hadoop100 ~]# sort -t : -nrk 3 sort.sh 
bd:20:4
s:10:3
xz:50:2
ss:30:1

9.6 wc

The wc command is used to calculate numbers. Using wc instruction, we can calculate the number of bytes, words or columns of the file.

9.6.1 basic grammar

wc [Option parameters] filename

Option parameters		function
-l			Count file lines
-w			Count the number of words in the file
-m			Count the number of characters in the file
-c 			Count the number of bytes in the file

9.6.2 case practice

Statistics/etc/profile Number of lines, words and bytes of the file
[root@hadoop100 etc]# wc -w /etc/profile
253 /etc/profile
[root@hadoop100 etc]# wc -l /etc/profile
76 /etc/profile
[root@hadoop100 etc]# wc -m /etc/profile
1819 /etc/profile