Linux study notes--day6 (3.20)-----shell (1)

What the shell does: Command interpreter    

As a human-machine interface, the shell is used to interpret the commands entered by the user, interpret the commands as binary codes that can be executed by the Linux kernel, and return the execution results to the standard terminal.

The location of the shell in the Linux system


Some basic shell commands

1. The history command history [-raw] histfiles is written to ~/.bash_history by default

The most recent 1000 entries are displayed by default. The configuration file is in /etc/profile


n: number, which means to list the most recent n command lines

-a: Add the newly added history command to histfiles. If there is a histfiles, it will be written to ~/.bash_history by default

-w: Write the current history memory content to histfiles.


The difference between -a and -w is as follows:



Invocation of historical passwords

Use "!n" to repeat the nth history command
Use "!!" to repeat the previous command

Use "!string" to repeat the last command starting with this string

Command alias configuration function (alias)

Demonstrate with grep command


This change is only currently available, not globally available. If you want to call it globally, you need to be under /etc/profile

Add alias grep='grep --color=auto', it is best to execute it here.


remove alias

unalias alias


Execute multiple commands sequentially

; Command 1; Command 2 Multiple commands are executed sequentially without any logical connection between the commands

&& Logical AND Command 1 && Command 2 When command 1 is executed correctly, then command 2 will be executed When command 1 is not executed correctly, then command 2 will not be executed

|| Command 1|| Command 2 Logical OR When command 1 is executed incorrectly, command 2 will be executed


As shown in the figure: There is no touch test.sh file, so ls will not display, output to the black hole, that is, incorrect, then good will not be executed, execute not good


last can output account number, terminal, source, date and time, and it is neatly arranged.

tee

tee [-a] file

-a : Add the data to the file in a cumulative manner

Chestnut:

[root@www~]# last | tee last.list| cut -d " " -f1

# This example allows us to save the output of last to the last.list file;
[root@www~]# ls -l /home | tee ~/homefile| more
# This example saves the data of ls Copy to ~/homefile, and the screen also has output information!
[root@www~]# ls -l / | tee -a ~/homefile| more
# Be careful! The files following tee will be overwritten. If the -a option is added, the information can be accumulated.


shell script

#! /bin/bash is required, indicating the type of shell


vim datewho.sh


There are two ways to execute this shell file, the first is to execute it directly with sh, and the second is to give permission first.



Variables and symbols in the shell

variable print echo


variable assignment


It is best to add {} to the final output.

The Law of Variables:

1. The variable and the variable content are connected by an equals sign. As shown above

2. There can be no direct spaces on both sides of the equal sign.

3. The variable name can only be English letters and numbers, but the beginning character cannot be a number

4. If there is a space character in the variable content, you can use double quotation marks or single quotation marks to combine the variable content

5. Special characters in single quotes are only general characters (plain text)

6. If the variable needs to be run in other subprograms, you need to use export to make the variable become an environment variable

7. The method to cancel the variable is to use unset: "unset variable name", for example, cancel the configuration of myname unset myname

Chestnut: How to make the name = zhangsan I just configured can be used in the next shell program?


[root@www~]#name=zhangsan

[root@www~]#bash <== enter the so-called subroutine

[root@www~]#echo $name <== subroutine: echo again;

       <== hehe! There is no content just configured!

[root@www~]#exit <== subroutine: leave this subroutine

[root@www~]#exportname

[root@www~]#bash <== enter the so-called subroutine

[root@www~]#echo $name <== subroutine: run here!

zhangsan <== look! Configuration values ​​appear!

[root@www~]#exit <== subroutine: leave this subroutine

Internal command

$# : the number of positional arguments passed to the shell program

$? : The completion code of the last command or the shell program (return value) executed inside the shell program. 0 means no error, other values ​​mean error

$0 : the name of the shell program

$* :      A single string of all parameters sent when calling the shell program, parameters saved in the form of "parameter 1", "parameter 2"...

$@    : Parameters saved in the form of "parameter 1 parameter 2"...

$n : the nth parameter

$$ : PID of this program



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813454&siteId=291194637