Bash Scripting Learn Notes (1)

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

时间: 2024-12-29 01:29:58

Bash Scripting Learn Notes (1)的相关文章

Stanford iOS Learn Notes - 3

题目是iOS Learn Notes,不过这篇文章更多的是梳理了一下过去半个月的弯路:没有跟iOS视频,去看国内的教材去了…… 掐指一算,已经半个月没有更新这个博客了,恍恍惚惚了半个月呀. 其实这半个月也没有闲着,把<Swift语言实战入门>的前8章翻了一下.第8章没有看完,实在看不下去了.怎么评价呢?这本书可以看出来写的时候应该还是用心写了的,最起码不像有些书,直接粘贴复制Swfit的官方文档翻译. 看了也有收获,比如 首先又熟悉了一遍Swift语法.这本书对语法进行了精简提炼,所以通读一遍

Linux Bash Scripting - Command Chaining & Command lists

# this is to show you how to execute a series of commands in one strike. $ clear; cd /; ls -l; echo "You are in $PWD"; echo "Time to go back home"; cd ~; # use semi colon between each command. # There is also command lists, which is fo

Java 8 Learn Notes - Streams

Main reference [1] http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples 1. How Streams Work A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements: List<String>

Stanford iOS Learn Notes - 5

今天总结一下 View Controller Lifecycle和Autolayout,即课程的第8节. 1. View Controller Lifecycle 其实听了两遍课程之后,我还是不能准确的总结出每一个controller的生命周期是多长.controller即MVC中的C,在MVC被创建后,controller的生命周期开始,这个很容易理解.但是我困惑的是MVC生命周期的结束. 首先怎么定义MVC生命周期的结果?controller的deinit函数被调用(内存释放)?还是View

Stanford iOS Learn Notes - 8

好久没有写笔记了.这二十天好忙啊,项目要发布,没有太多的时间看iOS了. 现在已经把Stanford的教程学完了,但是感觉很尴尬.其实看代码的时候,感觉没一个Demo都没有什么东西,但是看Demo看时似是而非的看不懂.视频里面,教授用每个API都是信手拈来,但是自己去写,课后看代码的时候,就不是那么回事了.不同的地方改用什么对象,UILable还是UIButton?这个对象都有什么API?这些其实都不知道.而要成为熟练的iOS开发者,这些其实是非常非常重要的. 翻了之前的几遍笔记主要是写了API

Stanford iOS Learn Notes - 1

这段时间在学习Stanford的iOS 8 的教学视频,学而不思则怠,所以准备总结一下看视频学习的一些笔记,便于自己加深理解. 现在已经学了6节课,从这六节课看,前三节课主要是讲了一个Calculator的Demo,并穿插了很少一些iOS的介绍,以及MVC的介绍.第四节主要是介绍了一些Swift的语法,第五节和第六节主要讲述了iOS的触控操作,并且穿插了一些Swift的语法.因此,这篇我把篇笔记分为三部分: 总结一下教授对iOS的简介 总结一下教授在这六节课里面讲的语法 总结一下Happines

Stanford iOS Learn Notes - 2

上一篇文章总结说,Stanford视频前六节需要总结的内容主要分为三部分: 总结一下教授对iOS的简介 总结一下教授在这六节课里面讲的语法 MVC,Cocoa Touch, Happiness Demo 前两部分在上一篇文章里面大概梳理了一下,当时有些知识点的细节没有详解总结,毕竟如果真要总结的话,每个知识点都可展开成一个文章.今天这篇文章主要集中梳理一下第三部分:MVC,Cocoa Touch,Happiness Demo. 1. MVC Divide objects in your prog

Bash脚本15分钟进阶教程

这里的技术技巧最初是来自谷歌的“Testing on the Toilet” (TOTT).这里是一个修订和扩增版本. 脚本安全 我的所有bash脚本都以下面几句为开场白: #!/bin/bashset-o nounsetset-o errexit 这样做会避免两种常见的问题: 引用未定义的变量(缺省值为“”) 执行失败的命令被忽略 需要注意的是,有些Linux命令的某些参数可以强制忽略发生的错误,例如“mkdir -p” 和 “rm -f”. 还要注意的是,在“errexit”模式下,虽然能有

bash条件测试之空字符串的困惑

看一个字符串是否为空,可以通过 -n 或 -z来判断.按照操作符的意义,变量VAR而言,如果VAR为空或未定义,那么-n测试结果应该为假,而-z应该为真.反之,如果不空,则-n为真,-z为假. 采用条件测试的列表形式,即"[ -n $VAR ] && echo TRUE",如果输出TRUE,则表明条件测试结果为真. 经过测试,显然VAR不空时,测试结果都正常,而VAR为空,测试结果也是真,但很多手册指南什么的(包括ABS<Advance Bash Scriptin