Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of variable operation in shell

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the shell variable operation example, the article is very detailed, has a certain reference value, interested friends must read!

7. Shell variables

env command to view system environment variables

The set command allows you to view not only system variables but also custom variables.

custom variables

variable name rule

Variable names can be letters, numbers, and underscores; the first digit cannot be a number.

Variable values with special symbols need to be enclosed in single quotes (disambiguation)

When the expression is complex, it needs to be enclosed in double quotes, and the accumulation of variables

Gets the value of a variable and inserts it into characters

The first one doesn't get the value of the variable.

The second is to enclose the variable name in curly brackets to get to

global variables

When multiple terminals are opened, use the command echo $SSH_TTY to view the current terminal

Customize a variable in terminal 1, terminal 2 is unable to view

bash is a child bash of shell, which can be checked by pstree. If there is no such command, use the following command to install it

yum install psmisc

Now it is under the second bash, the environment variable set by the first bash does not take effect under the first bash, if you want the first bash to take effect on the second bash, you can use global variables

cancel variable

unset b

echo $b

VIII. Configuration of environmental variables

Environment variable configuration files can be divided into: system level configuration files and user level configuration files

System hierarchy configuration files (files under/etc)

1./ etc/profile user environment variables, interaction, login to execute

2./ etc/bashrc Users do not need to log in, shell execution takes effect

User hierarchy profiles (files in the user's home directory)

1.~/. bashrc

2.~/. bash_profile

Generally, we do not edit the system-level configuration file. When necessary, we can edit the user-level configuration file.bash_profile.

source .bash_profile and.. bash_profile works the same way, loading the configuration in the configuration file

~/.bash_history, used to record command history

~/.bash_logout, used to define some operations that the user needs to do when exiting

ps1 is defined in/etc/bashrc and is used to define the string display on the left side of the command.

Change W to w and find an absolute path

Modify the square brackets, where normal user shows normal user, root user shows #

color display

9. Shell special symbol_sut command

Format: cut -d 'Split character'[-cf] n, where n is a number, change command options are as follows:

-d followed by a split character, split character

What number of characters follows-c?

What block is followed by-f?

The cut command is used as follows:

cat passwd this file, head only look at the first two lines, cut -d intercept the division symbol ":", -f 1 means to intercept the first paragraph, 1, 2 means the first two paragraphs, 1-3 means the first three paragraphs

sort_wc_uniq command

Introduction to sort command

The sort command is used for sorting

Format: sort [-t delimiter] [-kn1,n2][-nru]

Here-n1 n2 refers to a number, and the other options are as follows

-t is followed by the segmentation character, which has the same effect as the cut-d option. What is the interception symbol?

-n Use pure numeric sorting, letters and special symbols are 0

-r Reverse sort

If sort does not add any options, it is the default ascending output, we look at the passwd configuration file first five lines what is the difference

View the first five lines of passwd, output to sort for execution, sort in ascending order by default

Use sort to sort, numerical sort-n -n for positive order, special symbols and letters for 0

-r Reverse sort

wc command introduction, used to statistics

wc is used to count the number of lines, characters and words in a document

-l Count the number of rows

-m Number of statistical characters

-w Counts the number of words, distinguished by spaces

Command uniq removes duplicate lines

uniq is used to delete duplicate rows, the-c option is more commonly used

Before uniq can be used, files must be sorted first, otherwise it won't work

Count how many duplicate rows there are

tee redirect and display content

The echo content through the pipe symbol, output to tee command to 6.txt and then displayed on the screen above

tr, used to replace characters

-d means to delete a character, followed by the character to be deleted

-s for deleting duplicate characters

For example, if you need to replace a character in a file, change lowercase to uppercase.

split document

-b indicates that the document is cut according to size. The default unit is byte.

-l indicates that the document is cut according to the number of rows

Cut a document randomly, for example 5.txt, cut 1KB

Specifies that the directory file starts with 123

Eleven, shell special symbols

$

$can be used as an identifier in front of a variable, or it can be combined with! and put them together and use them

;

If you want to run more than one command on a single line, you need to precede the command with;

~

Indicates the user's home directory, root's home directory is/root, and ordinary users 'home directory is/home/username.

&

If you want to put a command in the background, you need to add the symbol &, which is usually used when the command runs for a long time. It can be used with sleep.

Redirect>>> 2>> &>

Replace (>) and add (>>)

2> Error redirects

2>> Error append redirect

&> indicates error and correct redirect input to a file to go

Square brackets [] indicate a range

&& indicates that the following command will be executed only after the previous command is successfully executed. If the previous command is not successfully executed, the following command will not be executed.

||If two commands are used to split, the second command will not be executed if the first is successful, and the second command will be executed if the first is wrong.

Article 1 is implemented successfully, Article 2 is not implemented

If the first fails, execute the second.

normal symbol

* Any character

?any one character

#comment character

\extension character

|pipe character

The above is all the content of this article,"Shell variable operation example", thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report