Introduction to Linux Study Notes (2)

1. Script

1.1 Executable script

Script, a program that is interpreted and executed

Three common script programs under Liuux
- Shellscript*.sh
- Perlscript*.pl
- Pythonscript*.py

A script program is essentially a text file.
1. It is a text file.
2. It has executable permissions.

Script programs are executed by an interpreter.
Shell script interpreter: /bin/sh
Perl script interpreter: /bin/perl
Python script interpreter: /bin/python3

When executing a script, the following two methods are equivalent./hello.py
/
bin/python3 hello.py

1.2 Shell script

ShellScript, a script written according to Shell syntax,
is the scripting language that comes with Linux and is equivalent to the batch script
under Windows.DOS

Creation of Shell Script
1. Edit a text file and save it as hello.sh

#!/bin/sh
echo "hello,world"

2. Add executable permissions

chmod +x hello.sh

3. Execute program

./hello.sh
/home/ysa/MyFolder/hello.sh		#绝对路径

Key points:

1. The interpreter must be declared on the first line.
#!/bin/sh
2. It must have x permission before it can be executed.
For example, a program can only be executed by itself (the author), and others cannot execute it.
rwxr--r--
3. When executing the program, the path must be added.
./hello.sh
/home/ysa/MyFolder/hello.sh

1.3 Python scripting

Creation of Python script
1. Edit a text file and save it as hello.py

2. Add executable permissions

chmod +x hello.py

3. Execute program

./hello.sh
/home/ysa/MyFolder/hello.py 	#绝对路径

2. Environment variables

2.1 Variables in SHELL

SHELL, is a script programming language
1. Define variables
2. Execute the command echo ls cd cp
3. If judgment logic, while loop logic
4. Customized functions...

Example:

#!/bin/sh
# 定义变量
OUTDIR=/opt
# 使用变量
echo "output to : ${OUTDIR}"
ls ${
    
    OUTDIR}

Key points:

1. Define a variable
NAME=value
with the left and right sides of the equal signDon't add extra spaces
2. Use a variable.
${NAME}
If there is already a delimiter, it can be abbreviated as $NAME
: For example:echo $NAME/build/

2.2 Environment variables

environment variables, which is a variable that exists in the current environment

Whether Linux or Windows, there are environment variables

For example, the most commonly used environment variables PATH, JAVA_HOME

Define environment variables

export OUTDIR=/opt/

Show environment variables

echo ${
    
    OUTDIR}

View all environment variables

printenv

Use of environment variables:

  • Can be used in the current terminal
  • Can be called in SHELL script

The environment variables defined in the command line are only valid for the current SHELL terminal.
When the terminal is closed, it disappears after restarting.

2.3 User environment variables

User environment variables: defined ~/.profilein

in home directory

ls -a

Among them, a means all, showing all files.
Note: Under Linux, .files starting withHidden files

1. Open with a text editor~/.profile

gedit ~/.profile

2. Add

export JAVA_HOME=/opt/jdk1.8

Save and close

3. Log out and it will take effect after logging in again.

echo $JAVA_HOME

Principle: When the user opens the terminal, .profile will be automatically run and variables will be injected into the current environment.

Key points:

1. Files starting with a dot. are hidden files.
ls -aView all files
. 2. This configuration is only valid for the current user
because each user has his own configuration file .profile
. 3. On some Linux systems, use.bash_profile

2.4 System environment variables

System environment variables: defined in /etc/profile.
The environment variables here are valid for all users.

run as root

su root
gedit /etc/profile

However, generally you do not modify /etc/profile
directly but /etc/profile.d/create a custom script

Demo:

1. Use gedit to create a script
gedit /etc/profile.d/myprofile.sh
2. Define environment variables
#!/bin/sh
export TOMCAT=/opt/tomcat
3. Log out and log in again.
After logging in again, the environment variables will take effect.

2.5 PATH environment variable

PATH, the most common environment variable,
is used to describe the search path of executable programs.

echo $PATH

Separate multiple paths with colonsusr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin

By default, the system searches for executable programs from the following directories
/usr/bin- for example, /usr/bin/tar
/usr/sbin- for example, /usr/sbin/useradd.
usr/local/bin
/usr/local/sbin
Among them, sbin is a program that can only be executed by the super user root.
The system comes with /usr/ Program, /usr/local/ is a program installed by the user

How to modify environment variables?
1. Edit /etc/profie.d/myprofile.sh
and set the PATH environment variable

export PATH=$PATH:opt/tomcat/bin

2. Log out and it will take effect after logging in again.

3. Virtual machine network

3.1 VMware network environment

1. Check the virtual machine network editor

  • Run VMware as administrator
  • Open the virtual network editor
  • Check the virtual subnet segment in NAT mode, such as 192.168.184.0
    Insert image description here

2. Check the virtual network card
Control Panel, "Network and Sharing Center | Change Adapter Settings"
. By default, there are two virtual network cards VMnet1andVMnet8

If VMnet1/VMnet8 is not found, pleaseUninstall VMware and reinstall

  • Note that it will be better to run the installer in administrator mode

3. Check the network configuration of the virtual machine: NAT mode
Insert image description here

3.2 Virtual machine networking

Virtual machine networking settings:

1. Check whether the virtual machine hardware is NAT方式
2. Log in to the Ubuntu system and set it in the upper right corner

  • On the left side of the settings panel, "Network"
  • Check IPv4/DHCP settings (The default settings are sufficient and do not need to be changed.)
    Insert image description here
    Insert image description here
    3. Check the IP address
    . My demo environment IP: 192.168.184.128
    4. Access the external network for testing
  • ping www,baidu.com
    according toCTRL+Cinterrupt

Note: The host itself must be connected to the external network

Key points:

You can set up the network in the graphical interface or use the command line in the terminal.

3.3 Interconnection with the host

Check the IP address
Virtual machine: 192.168.184.128
Host: 192.168.184.1
Insert image description here
Insert image description here
How to check the host IP?
Open the command line terminal: enter ipconfig
Insert image description here
the virtual machine and host interconnection:
192.168.184.1 → 192.168.184.128
ping 192.168.184.128
Insert image description here

192.168.184.128 → 192.168.184.1
ping 192.168.184.1
Insert image description here
Insert image description here
Insert image description here

3.4 Manually configure the network

In the terminal, configure the network using the command line

Commonly used commands: ifconfig, netstatetc.

By default, the ifconfig command is not available under Ubuntu.

Need to use apt package manager
apt install net-toolsto install a package
apt remove net-toolsremove a package
apt search xxxsearch
apt list | grep xxxlist
Insert image description here

1. Check the network configuration

ifconfig

Among them, if means interface network interface,
all network interfaces will be listed, and the status of each interface
2. Enable/disable the network

sudo ifconfig xxx up
sudo ifconfig xxx down

4. Server

4.1 FTP server

How to transfer files to Ubuntu host?

1. U disk copy
2. Network transmission

  • FTP
  • SFTP

4.2 SSH server

Using the SSH protocol, you can achieve:

  • 1. Remote terminal
  • 2. File transfer

5. Text processing

5.1 vim text editing

vi / vim, a console-based text editor
gedit, a GUI-based text editor.
Among them, vim is an upgraded version of vi. The demonstration uses the vim command.

How to open text editing

1. Open text editing.
vim abc.txt
If the target file exists, open editing; if it does not exist, a new file will be created.
If there is no vim on the system, install it:
sudo apt install vim
2. Switch mode
Editing mode Insert Mode: Press the i key
Command mode: Press ESC key
3. Exit editing
(1) Press ESC key to enter command mode
(2) Enter :wqSave and exit
Enter Exit Enter :qForce exit (abandon saving)
:q!

5.2 More uses of vim

vimThe complete usage is very complex
and there are many tricks. Almost every key on the keyboard is a shortcut key.
It is not recommended to study in depth. Vim itself is an inefficient tool.

Editing of Linux text files

  1. Desktop environment:gedit
  2. Terminal environment:
    – Minor modifications: vim
    – Heavy modifications: Edit on Windows, then upload to Linux

5.3 Upload of text files

Recommendation: Edit on Windows, then upload to Linux
using notepad++ or other professional editors

Line breaks for text files

Windows : \r\n
Linux : \n

This difference can be observed in Notepad++
View | Show Symbols | Show End of Line Characters

Conversion of newline characters:
Edit document | Format conversion | Convert to UNIX format

Notice:Conversion is only required when editing SHELL scripts

Files in other formats generally do not need to be converted, such as *.xml, *.java

Guess you like

Origin blog.csdn.net/YSA_SFPSDPGY/article/details/131840432