Shell variables, pipe breaks, job controls, shell variables, and variable configuration files

Source: Internet
Author: User

Pipe character
|: Pipe break is the delivery of the contents of the previous file's output to the following command
[[email protected] ~]# ls |wc-l
21st
See how many files are in the current directory
[email protected] ~]# cat 1.txt |wc-l
1
View the number of rows in 1.txt
The two examples above are simple uses of pipe characters.

Pipe Fuchangying Command

Cut: Intercept the string, for example we want to ":" As a delimiter, take the first column,

[Email protected] ~]# cat/etc/passwd |head-5|cut-d ":"-f1

Root

Bin

Daemon

Adm

Lp

Here-D is the specified delimiter,-F followed by the number, refers to the interception range, the above example is to intercept the first paragraph, enter-F1, if you want to

Intercept two paragraphs, use "," separate, followed by a number. such as f1,2.

If it is 1-3, it will be followed by 1-3.

Specify the number of characters-C

[Email protected] ~]# cat/etc/passwd |HEAD-2|CUT-C4

T

:

Sort: Sorting, for example, we now get a list of numbers, or a column of strings, we're going to do a sort of small to large
[Email protected] ~]# sort/etc/passwd |head-1
Adm:x:3:4:adm:/var/adm:/sbin/nologin
We can see that the sort of SOTR is following the alphabetical sort. That's what sort does.

Sort-n will be sorted by number size.
[Email protected] ~]# sort-n 1.txt
<

{
}
Root:x:0:0:root:/root:/bin/bash
Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
Sync:x:5:0:sync:/sbin:/bin/sync
22223333
22222222222aaaaaaa
* We need to note that if we enter-N, then the special symbols and characters in the file are considered to be 0. So the special symbols and letters will be in the front line.

Sotr-r reverse sort, with-n just the opposite
[Email protected] ~]# sort-r 1.txt
Sync:x:5:0:sync:/sbin:/bin/sync
1111111111133333333333
}

Wc-l count rows
[email protected] ~]# Cat 1.txt | Wc-l
20

Wc-m Statistic character Count
[email protected] ~]# Cat 1.txt | Wc-m
499

Wc-w statistic word number, he is separated by the space or the white space character
[email protected] ~]# Cat 1.txt | Wc-w
20

Uniq to repeat, he generally and sort together, first sort, then go heavy (most commonly used)
[email protected] ~]# cat 2.txt
123
ABC 1111,222
123
Abc
1
1
2
And then we sort of, go heavy
[Email protected] ~]# sort 2.txt |uniq
1
123
2
Abc
ABC 1111,222

Uniq-c count repetitions
[Email protected] ~]# sort 2.txt |uniq-c
2 1
2 123
1 2
1 ABC
1 ABC 1111,222
Tee: Similar to > Output redirection. > Output redirection is just writing content to the target file, and we don't see any content, success or not. At this point we will use tee instead of ">". Not only can it redirect content to the destination file, it can also print content to the screen.
[Email protected] ~]# sort 2.txt |uniq-c|tee a.txt
2 1
2 123
1 2
1 ABC
1 ABC 1111,222
So we can see what's in the output.

Tee-a is added, similar to the >> function. But the content is printed on the screen while appending
[Email protected] ~]# sort 2.txt |uniq-c|tee-a a.txt
2 1
2 123
1 2
1 ABC
1 ABC 1111,222
[email protected] ~]# cat A.txt
2 1
2 123
1 2
1 ABC
1 ABC 1111,222
2 1
2 123
1 2
1 ABC

TR: replacement character, which supports writing multiple characters
[Email protected] ~]# echo "Aminglinux" |tr ' [al] ' [Al] '
Aminglinux
[[email protected] ~]# echo "Aminglinux" |tr ' a ' a '
Aminglinux
Replace all with uppercase
[Email protected] ~]# echo "Aminglinux" |tr ' [A-z] ' [A-z] '
Aminglinux

Split: Cut to cut large files into small files. He has two usages, one is-B size The default unit is bytes, and one is the-l row count. (The system starts with X by default)
[Email protected] test]# Split-b a.txt
[[email protected] test]# ls
A.txt xae xaj xao xat xay xbd xbi xbn xbs xbx xcc xch xcm xcr xcw xdb xdg xdl xdq xdv xea xef xek xep xeu xez xfe xfj
Xaa xaf Xak xap xau xaz xbe xbj xbo xbt xby xcd xci xcn xcs xcx xdc xdh xdm xdr xdw xeb xeg xel xeq xev xfa xff XFK
Xab xag xal xaq xav xba xbf xbk xbp xbu xbz xce xcj xco xct xcy xdd xdi xdn xds xdx xec xeh xem xer xew xfb xfg XFL
Xac xah xam xar xaw xbb xbg xbl xbq xbv xca xcf xck xcp xcu xcz xde xdj xdo xdt xdy xed xei xen xes xex xfc xfh XFM
Xad Xai Xan Xas xax xbc xbh xbm xbr xbw xcb xcg xcl xcq xcv xda xdf XDK XDP xdu xdz xee xej xeo xet Xey XfD xfi
This completes the a.txt by 1000b cutting.

We can also give him a name after cutting, without the system giving us the names but we can only customize its prefixes.

S[[email protected] test]# split-b 100k a.txt ABC

[[email protected] test]# ls
ABCAA Abcab a.txt Xaa xab

Split-l cut by line, for example, we all cut according to line 1000.
[Email protected] test]# split-l a.txt
[Email protected] test]# Wc-l *
Xaa
Xab
Xac
This way, each file is 1000 rows.

After the target file, you can directly empty the contents.
[Email protected] ~]# > A.txt
[email protected] ~]# cat A.txt

Annotation characters
#: The shell entered after the # number does not come back into effect, and its role is to explain the explanation.

$: variable prefix,! $ combination, which indicates the row bit

Multiple names are written to one line, separated by semicolons.
[email protected] ~]# ls 1.txt; Wc-l 2.txt
1.txt
7 2.txt

~: The user's home directory, which represents a match in a regular expression

&: Command put in the background.

: Output redirection will replace the contents of the target
Append redirect to append content to the end of the target content
2>: Error output redirection, same as output redirection
2>>: Best error redirection, same as append redirect.
&>: Directly enter the correct and error directly into the target file.

"": One of the specified characters, often used for substitution, such as "0-9", "A-za-z", "abc".

|| : In the shell represents or, it is used in the middle of two commands. If the first command succeeds, it does not execute the second command, and if the execution is unsuccessful, it executes the second command.
[[email protected] ~]# ls 1a.txt| | Wc-l 2.txt
LS: Unable to access 1a.txt: No file or directory
7 2.txt
The first command did not succeed, and it executed the second one.
[[email protected] ~]# ls 1.txt| | Wc-l 2.txt
1.txt
If the first command succeeds, it will not execute the second one.

&&: Allow two commands to execute continuously, only the previous command succeeds, the subsequent command will continue to execute.
[[email protected] ~]# ls 1.txt&& wc-l 2.txt
1.txt
7 2.txt
Only the previous command executes successfully, and the latter only executes
[[email protected] ~]# ls 1a.txt&& wc-l 2.txt
LS: Unable to access 1a.txt: No file or directory
If the previous command is unsuccessful, the latter name will not be executed.

We can put | | and && understand as a judgment, | | The following command is only executed when the previous command is unsuccessful,&& only the command that follows is executed when the previous command succeeds.

Job Control
When we were editing a file and suddenly we had to pause to do something else, I just pressed CTRL + Z to throw the existing work backstage, and then we did what we needed.
[Email protected] ~]# vim 1.txt
Then press CTRL + Z
[1]+ has stopped Vim 1.txt
The work of editing 1.txt is put in the background.
If we're going to continue editing 1.txt, we'll enter FG, so we can put the task back in the background.

If we throw two commands in the background at the same time, we can print out any tasks in the background with jobs.
[[email protected] ~]# jobs
[1]-has stopped Vim 1.txt
[2]+ has stopped Vim 2.txt
Then we want to recall one of the tasks, as long as the input command fg+ task ID number can be, for example, we want to recall 2.txt editing task, we will enter FG 2.

We can also put commands in the background to run. Input Command BG 2
[[email protected] ~]# BG 2
[2]+ Vim 2.txt & (this is equivalent to running in the background)
Another example
[[email protected] ~]# Sleep 100
^z
[2]+ has stopped sleep 100
[[email protected] ~]# BG 2
[2]+ Sleep &
[[email protected] ~]# jobs
[1]+ has stopped sleep 180
[2]-Run in Sleep &

In addition, we can also put the command in the back-end, is to add "&" after the command
[Email protected] ~]# sleep &
[1] 1223
[[email protected] ~]# jobs
[1]+ Run in Sleep &
In this case, you can put a command in the background to proceed, but at the other terminal we do not see the task of our terminal, but can view the process.

Variable
The CentOS system variables include path,home,pwd,logname and so on, so where are these variables defined? We can get these system variables by command env.
Env is to look at the environment variables commonly used in the system, in addition to Env, and set, this command can not only look at the environment variables within the system, but also can see the user-defined environment changes amount.

[Email protected] ~]# env | Head-5
Xdg_session_id=1
Hostname=localhost.localdomain
Selinux_role_requested=
Term=linux
Shell=/bin/bash
The left side of the equals sign is the name of the variable and the right is the value. System variables are usually uppercase letters, but the values of variables are not necessarily letters, sometimes letters, and sometimes numbers.

We can also customize variables, such as
[Email protected] ~]# a=111
[Email protected] ~]# echo $a
111
This is our custom variable, not our system's own variable, this variable can be found with the SET command
[[Email protected] ~]# Set | grep 111
a=111

Rules for variable names: first the system's environment variables are built into the system, we have no right to interfere with him, but we can modify the configuration file. We can also customize the variable, first we first give the variable a name, the name of the variable is composed of letters, numbers, underscores, but the first cannot be a number.
[Email protected] ~]# a1=2
[Email protected] ~]# echo $a 1
2
[Email protected] ~]# a=4
[Email protected] ~]# echo $a

4
[Email protected] ~]# _a1=5
[Email protected] ~]# echo $_A1
5
[Email protected] ~]# 1a=5
-bash:1a=5: Command not found
It can be seen that the first variable in addition to the number, can be executed.

The value of a variable must be enclosed in single quotation marks when it has a special symbol, for example, we want to execute the variable a=a b C.
[Email protected] ~]# A=a b C
-BASH:B: Command not found
Because there are spaces in the back of the value of the existence, so the variable is not yo formation, he will only think A=a, and then the b,c he will understand as a command, will error. So we're going to use a ' enclosed ', here's a single quote, so you can take it off.
[[email protected] ~]# a= ' a b C '
[Email protected] ~]# echo $a
A b C
If you use double quotes "", there will be cases where special symbols are not recognized.
[Email protected] ~]# a= "A$BC"
[Email protected] ~]# echo $a
A
So, when you have a special symbol, use a single quote '
[Email protected] ~]# a= ' A$BC '
[Email protected] ~]# echo $a
A$bc

The summation of variables
Our variables can be summed up and output, such as
[Email protected] ~]# a=1
[Email protected] ~]# b=2
[Email protected] ~]# echo $a $b
12
This allows the simultaneous output of two variables

[Email protected] ~]# a= ' A$BC '
[Email protected] ~]# echo $a
A$bc
Add single quotation marks to take off the meaning

[Email protected] ~]# c= "a$b" C
[Email protected] ~]# echo $c
A2c
In this case, $b is considered as an assignment variable, because the front is already b=2,c=a so the output here is a2c. If our variables are more complex, then we'd better use single quotes.

Global variables
Let's do an experiment first and define Aming=linux.
[Email protected] ~]# Aming=linux
[Email protected] ~]# echo $aming
Linux
Then we open another answer or go to another bash
[Email protected] ~]# bash
[Email protected] ~]# echo $aming

[Email protected] ~]#
At this point we find that the aming we have just defined does not have the value of the custom result, we call such a definition variable as a non-global variable, also called a local variable, he only takes effect under the current terminal.

So how do we turn it into a global variable? Export with command
[Email protected] ~]# export Aming=linux
[Email protected] ~]# echo $aming
Linux
[Email protected] ~]# bash
[Email protected] ~]# echo $aming
Linux
This becomes a global variable. And this global variable can only be from top to bottom, not from bottom to top, and not on another terminal.

Cancel variable unset
[Email protected] ~]# unset aming
[Email protected] ~]# echo $aming
This cancels the custom variable.

Export syntax: Export variable name = variable value, global variable is only valid under child shell, enter bash to enter a bash.

Configuration file for Environment variables
The environment variable file is divided into two dimensions, one dimension is the system level (etc), and the other dimension is the user level (~/in the home directory). System level has/ETC/PORFILE,/ETC/BASHRC, these two files we had better not to edit it.
The user hierarchy includes ~/.bashrc,~/.bash_profile. We can edit these two files in the user's home directory. ~/.bash_profile will call ~/.BASHRC, and ~/.BASHRC will call ~/.BASHRC.
The difference between profiles and BASHRC is that profile is automatically loaded when the user logs on, and that it calls BASHRC. While BASHRC is executing the shell script, the user can execute the script without logging in, and executing the script will invoke some of the configuration in BASHRC.
~/.bash_history.
~/.bash_logout is used to define some of the actions that a user needs to make when exiting

Shell variables, pipe breaks, job controls, shell variables, and variable configuration files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.