[Shell] Basic grammar (1)


1. Introduction to the shell

The role of the shell is to explain and execute the user's command. When the user enters a command, the shell will explain and execute one. This method is called interactive (Interactive). There is also a way to execute the command in the shell. The user writes a shell script in advance 批处理(Batch)( Script), there are many commands in it, let the shell execute these commands at one time, instead of typing commands one by one. Shell scripts are very similar to programming languages. They also have variables and flow control statements, but shell scripts are interpreted and executed without compiling. The shell program reads and executes these commands line by line from the script, which is equivalent to a user putting the commands in the script Knock line by line to the Shell prompt to execute.

For historical reasons, there are many shells on UNIX systems:

  1. sh(Bourne Shell): Developed by Steve Bourne, various UNIX systems come with sh.
  2. csh(C Shell): Developed by Bill Joy and released with BSD UNIX, its flow control statement is very similar to C language, and supports many functions that Bourne Shell does not support: job control, command history, command line editing.
  3. ksh(Korn Shell): Developed by David Korn, it is backward compatible with the functions of sh, and adds new functions introduced by csh. It is the standard shell of many UNIX systems. On these systems, /bin/sh is often a symbolic link pointing to /bin/ksh .
  4. tcsh(TENEX C Shell): It is an enhanced version of csh, which introduces functions such as command completion, and replaces csh on systems such as FreeBSD and MacOS X.
  5. bash(Bourne Again Shell): The shell developed by GNU, the main goal is to be consistent with the POSIX standard, and at the same time take into account the compatibility with sh. bash borrows many functions from csh and ksh, and is the standard shell of various Linux distributions. On the Linux system /bin /sh is often a symbolic link pointing to /bin/bash. Even so, there are still many differences between bash and sh. On the one hand, bash has expanded some commands and parameters. On the other hand, bash is not completely compatible with sh, and some behaviors are inconsistent, so bash needs to simulate the behavior of sh: when we When bash is started through the program name sh, bash can pretend to be sh, does not recognize extended commands, and behaves in the same way as sh.

View the Shell type corresponding to the user:

  • man /etc/passwdCommand
    insert image description here
    The last column shows the shell type corresponding to the user

  • View environment variablesecho $SHELL
    insert image description here

Built-in commands:

After the user enters a command on the command line, the shell will generally fork and exec the command, but the shell is 内建命令an exception. Executing a built-in command is equivalent to calling a function in the shell process, and does not create a new process . The commands you have learned before cd、alias、umask、exitare built-in commands.

Check if the command is a built-in command:type 命令

insert image description here


2. Execute the script

Write a simple script test.sh:

#!/bin/sh
echo HelloWorld
cd ..
ls
  • Directly ./execute the comment
    in the shell script , which is equivalent to the comment in C language. But if # is at the beginning of the first line, and it is #! (called Shebang), it is an exception, which means that the script is interpreted and executed using the interpreter /bin/sh specified later. Add executable permission to this script file and execute it:#//
    insert image description here
  • /bin/shtest.sh Execution
    This method does not require the script to have executable permission, but only requires the script to have readable permission. This method can be used in some cases where there is no way to adjust the script permissions,But this method needs to pay attention to what interpreter the current script uses
    insert image description here
  • Commands entered on the command line are ()enclosed in
    insert image description here
  • sourceScript Address
    Executing the script in this way will not generate a child process, but will load all the script commands into it for execution. Commonly used to load configuration files such as:source /etc /profileinsert image description here

The process of Shell execution: Shell will fork a child process and call exec to execute ./test.shthis program, exec system callReplace the code segment of the child process with the code segment of the ./test.sh program, and execute it from its _start


3. The basic syntax of the shell

1. Use of variables

Shell variables usually start with a letter plus an underscore, and consist of letters, numbers, and underscores of any length

Variable definition:声明即赋值

varname=value, pay attention that no spaces can be left on both sides of the equal sign, if a space is left, it becomes a command + two parameters

insert image description here

The use of variables: use $symbols to follow the variable name to indicate the value of a variable, and the variable name can be added {}to indicate the scope of the variable name:

insert image description here


2. Classification of variables

Variables in the shell: Variables that exist in the shell parsing environment.

  • 全局变量: Variables that are not modified with any modifiers in the shell are global variables. Whether it is inside or outside the function, it is the same from the beginning of the declaration statement to the end of the script is its life cycle
  • 环境变量: It comes with the operating system, and every process will have it. When a child process is created, the child process inherits the environment variables of the parent process.
#!/bin/sh 
globalVar1="hello" // 全局变量

function test()
{
    
    
  globalVar2="world" // 全局变量
  local localVar="itcast" // 局部变量
}

#调用函数
test 
echo $globalVar1 $globalVar2

insert image description here

Variables in the shell can only be used in the current shell process, and cannot be passed across processes. Unless used source, loads a program from another process into the current process.

subScript.sh

#!/bin/sh 
echo "this is in subScript"
echo $globalVar1
echo "subScript end"

Execute subScript in test.sh:

insert image description here

环境变量It is a one-way transmission, which can only be passed from the parent process to the child process and not from the child process to the parent process.

Define an environment variable: export varname=valueorvarname=value; export varname

delete variable

insert image description here

Both ordinary variables and environment variables can be unsetdeleted using commands.


Guess you like

Origin blog.csdn.net/m0_67595314/article/details/132113843