Linux study notes [Huadian] (1)-Shell command

This semester happens to be learning linux, take this opportunity to write a note. I didn't know at the time, but I actually downloaded a Chinese version of ubuntu, which was really disgusting to use. . .

Linux commonly used commands


/etc/passwd (not a file, not a command)

cat /etc/passwd

The user can view the user configuration information by opening this file.

Insert picture description here

Insert picture description here

If the format is displayed successfully:

User name: password: user id: group id: account description: home directory: login shell (default is bash)


passwd

passwd 用户名

Ordinary users can only modify their own passwords, root can modify anyone's passwords, and does not need to know the previous password.


id

id

Can view the information of the currently logged in user

Insert picture description here


variable

创建或修改一个变量:变量名=变量值
显示变量的值:echo $变量名
删除变量:unset 变量名
导出环境变量:export 变量名

In short, you will understand how to use it after a meal.

Insert picture description here


View environment variables

set
env

Can be used to view environment variables and print out a bunch of things

Insert picture description here


PATH variable

echo $PATH

PATH is the search path of the shell command. It will search for the executable file with the same name as the command in the directory contained in the PATH

Insert picture description here


Environment variable PS1

echo $PS1

The main command prompt. Can be customized. The default is username@hostname: current working directory: $ (normal user) or # (root)

Insert picture description here


Environment variable PS2

echo $PS2

Generally used when the command is too long and one line cannot be completed, the next line is followed by the previous line, and there will be ">" at the beginning of the line.

Insert picture description here


Environment variable HOME

echo $HOME

User's home directory: /home/username

Insert picture description here


source or.

source 脚本文件

Execute the script file in the current shell
Here is a script that I have written to create variables

Insert picture description here

Compared with the bash command, it is obvious that the source is carried out in the current shell, rather than starting a new process.

Insert picture description here


pwd

pwd

View current directory path

Insert picture description here


cd

cd 路径名
cd ..(返回上层目录)
cd -(返回上一次cd的目录,可以理解为反复横跳)
cd (返回HOME)

The usage is very simple, but far more powerful than CD on windows.

Insert picture description here


man

man [页号] 命令名

You can view the help information of a command.
Page number 1: Linux built-in
Page number 2: Linux system calls
Page number 3: Library functions

Insert picture description here


| (Pipeline)

ls | less

Use the stdout of the left command as the stdin of the right command to realize the function of the pipeline.

Insert picture description here


Redirect

> (Output redirection)

ls > 文件名

Redirect the stdout of the instruction on the left to the file on the right (overwrite).

Insert picture description here

ls > 文件名 2>&1

Redirect stderr to a file that has already redirected stdout (sounds a bit convoluted). Because there is no error message in the example, the result remains unchanged.

Insert picture description here

set –o noclobber

Because> will erase the content of the original file, it is more dangerous. The set -o noclobber command can ensure that if the file exists, I will not perform redirection.

>|

If I have to enforce it anyway, I just use >|.

>> (Add redirection)

ls >> 文件名

The usage is similar to the previous one, except for appending

<(Input redirection)

cat < 命令或文件

Change the stdout on the right to stdin on the left.

Insert picture description here

<< (limit symbol)

wc << 界限字符
  ...
界限字符

This is not a redirect, although it looks very similar. It is mainly used as a delimiter.

Insert picture description here


*,?,[,],[-],{,}

Used for matching, similar to regular expressions, but simpler. See examples for specific usage.

Insert picture description here


read

read file

Read the string from the terminal and place it in the variable on the right.

Insert picture description here

A bash script has been written before, including a read command.

Insert picture description here


test

test can be used for integer test, string test, file test and logic test.

Insert picture description here

In Linux, 0 means true and 1 means false, which is the opposite of high-level programming languages. And this adds a parameter that at least the main function in c must have (although it is usually not written in a game...).

int main(int argc,char **argv,char **env)
{
    
    
	return 0;
}

Of course, programmers are relatively lazy, and the test command can be written as "[".

Insert picture description here


expr

When performing expression calculations, pay attention to spaces, otherwise it will be mistaken for string.

Insert picture description here


if statement

The following bash grammars will feel more obscure. The teacher said that those who can write scripts in bash are great gods, and we recommend that we write them in python. . .
The senior said that because he couldn't write shell scripts, he suffered a little at work, hahaha.

if [ expression ];
then
statements
elif [ expression ];
then
statements
else
statements
fi

case statement

In class, the teacher said that this is all anti-human grammar. . . .

case $var in
val1)
statements;;
val2)
statements;;
*)
statements;;
esac

for statement

for var in list
do
statements
done

while statement

while expression
do
statements
done

util statement

until expression
do
statements
done

function

function 函数名()
{
    
    
statements
}

Special shell variables

They are all variables used to express specific meanings, which are more important. The teacher said that they must be tested, hahahaha.

$# 传递给脚本或函数的参数个数
$0 脚本程序自身的名称(命令行名称)
$1 传给脚本或函数的第一个参数
$2 ...
$3 ...
$4 ...
$? 上一条语句的返回值
$$ 执行本脚本的进程的PID值
$* 将所有的参数作为一个字符串
$@ 将所有的参数分解为包含若干字符串的数组

This chapter ends here, and I will write about vim in the next issue.
Recently, I was busy taking part in the pre-reduction exemption and missed some classes. I would like to thank students mj and lqz for helping me record the class video.

Guess you like

Origin blog.csdn.net/weixin_41650348/article/details/108535467