Pipes, redirection, and environment variables

1 Input and output redirection

Standard input redirection (STDIN, the file descriptor is 0): input from the keyboard by default, and can also be input from other files or commands.
Standard output redirection (STDOUT, file descriptor is 1): output to the screen by default.
Error output redirection (STDERR, file descriptor is 2): output to the screen by default.

Symbols used in input redirection and their functions

symbol effect
命令 < 文件 Use a file as standard input to a command
命令 << 分界符 Read from standard input until a delimiter is encountered
命令 < 文件1 > 文件2 Take file 1 as standard input to the command and standard output to file 2

Symbols used in output redirection and their functions

symbol effect
命令 > 文件 Redirect standard output to a file (clear the data of the original file)
命令 2> 文件 Redirect the error output to a file (clear the data of the original file)
命令 >> 文件 Redirect standard output to a file (append to original content)
命令 2>> 文件 Redirect error output to a file (append to original content)
命令 >> 文件 2>&1or
命令 &>> 文件
Write standard output and error output together to the file (append to the end of the original content)
[root@localhost ~]# wc -l anaconda-ks.cfg 
46 anaconda-ks.cfg
[root@localhost ~]# wc -l < anaconda-ks.cfg 
46
[root@localhost ~]# cat anaconda-ks.cfg | wc -l
46
[root@localhost ~]# grep '/sbin/nologin' /etc/passwd | wc -l
33
[root@localhost ~]# ls -l /etc/ | head -n 2
[root@localhost ~]# ls -l /etc/ | tail -n 2

Change password without asking:

[root@localhost ~]# echo root | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.

–stdin:This option is used to indicate that passwd should read the new password from standard input, which can be a pipe.

Use the pipe character to send an email with the text "Hello, this is the email text" and the subject "This is Subject" to the root user without interaction
insert image description here

2 Wildcards on the command line

*Match zero or more characters,
?match a single character,
[0-9]match a single digit character between 0 and 9,
[abc]match any one of the three characters a, b, and c

[root@localhost ~]# ls -l /dev/sda*
brw-rw----. 1 root disk 8, 0 Aug 11 16:38 /dev/sda
brw-rw----. 1 root disk 8, 1 Aug 11 16:38 /dev/sda1
brw-rw----. 1 root disk 8, 2 Aug 11 16:38 /dev/sda2
[root@localhost ~]# ls -l /dev/sda?
brw-rw----. 1 root disk 8, 1 Aug 11 16:38 /dev/sda1
brw-rw----. 1 root disk 8, 2 Aug 11 16:38 /dev/sda2

Define variables, and use single quotes and double quotes:

Backslash \: Makes a variable following a backslash a plain string.
Single quotes '': Escape all variables in them as simple strings.
Double quotes "": retain the variable attributes inside, without escaping.
Backtick ``: return the result after executing the command in it.

[root@localhost ~]# PRICE=5
[root@localhost ~]# echo "Price is $PRICE"
Price is 5
[root@localhost ~]#  echo "Price is $$PRICE" 
Price is 2086PRICE
[root@localhost ~]# echo "Price is \$$PRICE"
Price is $5

$$作用是显示当前程序的进程ID号码

[root@localhost ~]# alias hhh="grep -n network anaconda-ks.cfg"
[root@localhost ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias hhh='grep -n network anaconda-ks.cfg'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

variable name effect variable name effect
HOME The user's home directory (ie home directory) PATH Defines the path where the interpreter searches for commands executed by the user
SHELL The name of the shell interpreter that the user is using LANG System language, language family name
HISTSIZE The number of historical command records output RANDOM generate a random number
HISTFILESIZE The number of historical command records saved MAIL mail storage path
PS1 The prompt of the Bash interpreter EDITOR user default text editor

Guess you like

Origin blog.csdn.net/Michael_lcf/article/details/85864647#comments_25493036