Preliminary study of LINUX shell

Table of contents

shell variables with $

insert image description here
insert image description here

Both of these belong to assigning values ​​to variables.
insert image description here
As for the above variable assignment, there is a problem, because QAQ has been assigned to str, and qaq cannot be identified. So when assigning a statement with spaces, " " should be added.

The symbol $ means to read the content of the value, such as:
insert image description here
echo means to display. That is, the content in the value of today is displayed.

read command

insert image description here
The read command assigns the characters entered on the keyboard to the variable, and the variable can be directly called in subsequent statements.
insert image description here
You can also read and assign multiple variables at the same time. If the number of inputs is greater than the number of variables, the last variable will get the remaining number of inputs. If it is less than, it is an empty string, as shown in the figure:
insert image description here
insert image description here

The role of positional parameters

File m1.c,
insert image description here
file m2.c,
insert image description here
file ex3
insert image description here
, change ex3 to an executable file
chmod +x ex3,
execute the script ex3 m1.c m2.c
is about to put m1.c into position parameter 1, m2.c into position parameter 2, and put them Use the cat function to integrate, the position parameter 0 is already the command name, namely ex3, and then use wc -l to calculate and output the total number of lines.

Assigning values ​​to variables using positional parameters

Create the script ex5, as shown in the figure:
insert image description here
Explanation: Assign the file name (pointer) of the file in the position parameter 1 to n1, and the same reasoning follows. And the following cat $n1 $n2 $n3 is to integrate the file content of the files whose parameters are 1, 2, and 3, and then calculate the total number of lines.

if statement

Create the script ex9
insert image description here
pwd to display the current directory, test -f file to judge whether the file exists, and it is a regular file, use fi to complete the end of if.

Create the script ex9_1 to
insert image description here
open the file if it is a regular file, enter the directory if it is a directory file, and open all files in the directory

wildcard

Create the script ex7
insert image description here
here is `not', and the backslash indicates the execution
date of the command is to see the time and date

Who is to display the current user, and wc -l indicates the number of lines that the who command appears.

test statement

Create script ex10
insert image description here
if [ -f ex1 ] means the same as if test -f ex1.
It should be noted here that in [ ], spaces should be added after the front brackets and before the back brackets, and spaces should be added after the commands -f, ls-l, echo, and the directory to be executed.

while statement

Create an ex11 script
insert image description here
with [ ] behind the while statement, specify the conditions in [ ], and pass in one or more files as shown in the above figure, and they will judge separately until the judgment is completed and jump out of the loop.

insert image description here

build ex12 script

for statement

1. Exercise
insert image description here
That is, for day, take these 4 values ​​in turn and display them.
Familiar with
the for in statement
do done statement

2. Create an ex13 script.
insert image description here
As a result of the operation,
insert image description here
pass the first parameter into dir. If dir is a directory file, enter dir for operation, and pass the second parameter into name (I don’t know why it is passed in here. name), and then judge the name file and perform corresponding operations.

function

1. Simple function call
Create the text ex14, as shown in the figure
insert image description here
Run the script
insert image description here
2, Reference the function
Create the script ex14_1, as shown in the figure
insert image description here
Run the script
insert image description here
It should be noted here that, in the future, remember to add spaces on both sides in [ ] behind while, for count= $(( $count+1)) must have two parentheses.

Clear the log files under /var/log

insert image description here
Write a script in the root directory containing /var
Use of the cat command
1. Display the entire file at once. $ cat filename
2. Create a file from the keyboard. $ cat > filename
can only create new files, and cannot edit existing files.
3. Merge several files into one file: $cat file1 file2 > file
4. Empty files, such as to empty
/www/aaa.txt
cat /dev /null > /www/aaa.txt;
Improved version:
insert image description here
real script:

insert image description here
insert image description here
The number represented in $UID is the user, and when its value is 0, it means the root user.
After exit is the return value, and the value range of the return value is 0~255.
-ne means to judge whether the two are equal.
tail command: tail -n filename > newfilename Save the last n lines of the filenname file to newfilename, and remember to rename.

Guess you like

Origin blog.csdn.net/s_1024/article/details/115052903