Pipeline, redirection and environment variables

Pipeline characters, redirects, and environment variables

My thoughts

The so-called redirection is to output the content originally output here to another location.

I / O redirection

Standard input redirection:
You can enter from the keyboard or from other files or commands.
Symbols used

command < File takes the file as standard input for the command
 command << Delimiters are read from standard input and do not stop until they are encountered
 command <File 1> File 2 takes file 1 as the standard input of the command and outputs the standard to file 2

Standard output redirection

Output to the screen, or write the data information originally to be output to the screen to the specified folder.
Symbols used:

command > File redirects standard output to a file(Empty the data of the original file)
Command 2> File redirects the error output to a file(...)
command >>File redirects standard output to a file(Add)
Command 2>> File redirects the error output to a file(...)
command >> Document 2>&1
 Or command &>> file

Write standard output and error output to the file together(Append to original content)

Why is there 2 but no 1 on it, and the original descriptor 1 can be written but not written

Output redirection example

ji@ji-virtual-machine:~$ echo 'welcome to my world!' > readm.txt 
ji@ji-virtual-machine:~$ cat readm.txt 
welcome to my world!
ji@ji-virtual-machine:~$ echo 'welcome to my world!' >>readm.txt 
ji@ji-virtual-machine:~$ cat readm.txt 
welcome to my world!
welcome to my world!

Redirect error output to file

ji@ji-virtual-machine:~$ ls -l xxxxx 2> readm.txt 
ji@ji-virtual-machine:~$ cat readm.txt 
ls: cannot access xxxxx: There is no such file or directory

input redirection

Use the wc -l command to see how many lines there are in the file

ji@ji-virtual-machine:~$ wc -l < readm.txt 
1

Equivalent to cat readm.txt|wc -l

Pipe command

The standard normal data originally output to the screen by the previous command is regarded as the standard input of the next command.
Take the command to find the restricted login user as an example

grep "/sbin/nologin" /etc/passwd

The command to count text lines is wc -l
In this way, the information originally to be output to the screen can be filtered and counted

ji@ji-virtual-machine:~$ grep "/sbin/nologin" /etc/passwd|wc -l
0

View the file list and attribute information in the / etc directory in the form of page turning

ls -l /etc/ | more

In the root state, a command can be used to reset the password by combining the pipe character with the -- stdin parameter of the passwd command

echo 'your_pwd' | passwd --stdin root

A command to send mail

echo 'Content' | mail -s 'Subject' your_pwd

The pipe command character is used more than once on the command line. It can be used multiple times, A|B|C

Command line wildcards

It's the ones in regular
[0-9] matches any of the numbers 0-9
*Represents 0 or more characters
? Represents matching a single character
[abc] match any of the three characters a, B and C

ji@ji-virtual-machine:~$ ls -l /dev/sda*
brw-rw---- 1 root disk 8, 0 12 January 14:17 /dev/sda
brw-rw---- 1 root disk 8, 1 12 January 14:17 /dev/sda1
brw-rw---- 1 root disk 8, 2 12 January 14:17 /dev/sda2
brw-rw---- 1 root disk 8, 5 12 January 14:17 /dev/sda5
ji@ji-virtual-machine:~$ ls -l /dev/sda?
brw-rw---- 1 root disk 8, 1 12 January 14:17 /dev/sda1
brw-rw---- 1 root disk 8, 2 12 January 14:17 /dev/sda2
brw-rw---- 1 root disk 8, 5 12 January 14:17 /dev/sda5
ji@ji-virtual-machine:~$ ls -l /dev/sda[1-9]
brw-rw---- 1 root disk 8, 1 12 January 14:17 /dev/sda1
brw-rw---- 1 root disk 8, 2 12 January 14:17 /dev/sda2
brw-rw---- 1 root disk 8, 5 12 January 14:17 /dev/sda5
ji@ji-virtual-machine:~$ ls -l /dev/sda[235]
brw-rw---- 1 root disk 8, 2 12 January 14:17 /dev/sda2
brw-rw---- 1 root disk 8, 5 12 January 14:17 /dev/sda5

Reused escape characters

Backslash (): changes a variable after the backslash to a simple string
Single quotation mark (''): escape all variables as simple strings
Double quotation mark (""): keep the variable attribute in it without escape
Backquote (` `): returns the result after executing the command
example:
Define the variable first, and then output it. Lower case price is not allowed

ji@ji-virtual-machine:~$ PRICE=5
ji@ji-virtual-machine:~$ echo "Price is $PRICE"
Price is 5

The $$function after the combination of dollar symbol and variable extraction symbol enables the display of the process ID number of the current program

ji@ji-virtual-machine:~$ echo "Price is $$PRICE"
Price is 2597PRICE

At this time, use the \ backslash to escape; Here, the dollar sign $extracts the value of the variable

ji@ji-virtual-machine:~$ echo "Price is \$PRICE"
Price is $PRICE
ji@ji-virtual-machine:~$ echo "Price is \$$PRICE"
Price is $5

Execute the command and return the result

ji@ji-virtual-machine:~$ echo `uname -a`
Linux ji-virtual-machine 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

environment variable

Variables are the data types that the operating system uses to hold variable values.

Location of environment variables

Each path value is separated by a colon:

ji@ji-virtual-machine:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

The env command looks at the environment variables in the system, but there are a lot of them
Common environment variables

HOME User's home directory(Home directory)
SHELL User is using Shell Interpreter name
HISTSIZE Number of history command records output
HISTFILESIZE Number of history command records saved
MAIL Message save path
LANG System language and language family name
RANDOM Generate random numbers
PS1 Bash Interpreter prompt
PATH Defines the path where the interpreter searches the user to execute the command
EDITOR User default text editor

As a multi-user and multi task operating system, Linux can provide an independent and appropriate working environment for each user, that is, the same variable will have different values by different users.

Source of variable

Variables are composed of fixed variable names and variable values set by users or the system. We can also create them ourselves.. But capitalize
For example:
Create a variable as a file directory, which can be cd entered into the directory
No spaces are allowed when assigning variables

root@ji-virtual-machine:/home/ji# WORKDIR = /home/workdir
WORKDIR: Command not found
root@ji-virtual-machine:/home/ji# WORKDIR=/home/workdir
root@ji-virtual-machine:/home/ji# cd $WORKDIR 

Of course, the variable is not global. You can use the export variable name as global

## Ordinary users do not have this variable
ji@ji-virtual-machine:~$ echo $WORKDIR
## Become a global variable; But it ubuntu doesn't seem to work for me
root@ji-virtual-machine:/home/ji# export WORKDIR

Posted by ericorx on Wed, 01 Dec 2021 00:57:42 -0800