Bash Scripting Learn Notes

Posted 每天更强一点...

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash Scripting Learn Notes 相关的知识,希望对你有一定的参考价值。

References:

[1] http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html

1. Executing programs from a script

When the program being executed is a shell script, bash will create a new bash process using a fork. This subshell reads the lines from the shell script one line at a time. Commands on each line are read, interpreted and executed as if they would have come directly from the keyboard.

While the subshell processes each line of the script, the parent shell waits for its child process to finish. When there are no more lines in the shell script to read, the subshell terminates. The parent shell awakes and displays a new prompt.

If input is not commented, the shell reads it and divides it into words and operators, employing quoting rules to define the meaning of each character of input. Then these words and operators are translated into commands and other constructs, which return an exit status available for inspection or processing. The above fork-and-exec scheme is only applied after the shell has analyzed input in the following way:

  • The shell reads its input from a file, from a string or from the user‘s terminal.

  • Input is broken up into words and operators, obeying the quoting rules, see Chapter 3. These tokens are separated by metacharacters. Alias expansion is performed.

  • The shell parses (analyzes and substitutes) the tokens into simple and compound commands.

  • Bash performs various shell expansions, breaking the expanded tokens into lists of filenames and commands and arguments.

  • Redirection is performed if necessary, redirection operators and their operands are removed from the argument list.

  • Commands are executed.

  • Optionally the shell waits for the command to complete and collects its exit status.

It is preferred to execute the script like this in a subshell. The variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten. If you don‘t want to start a new shell but execute the script in the current shell, you source it: source script_name.sh

以上是关于Bash Scripting Learn Notes 的主要内容,如果未能解决你的问题,请参考以下文章

[Bash Scripting LOOP]for, while. until

[Bash Scripting LOOP]for, while. until

Linux Bash Scripting - Command Substitution

Linux Bash Scripting - Command Chaining & Command lists

编程实践Bash Scripting with ChatGPT: Highlight Syntax使用 ChatGPT 编写 Bash 脚本:突出显示语法

《Advanced Bash-scripting Guide》学习(十九):两个整数的最大公约数