shell学习笔记一
Tofloor
poster avatar
老陌
deepin
2018-09-30 20:56
Author
本帖最后由 myccloves 于 2019-7-31 22:31 编辑

这和我之前的学习有些重复,只挑选重点内容记录一下


一、查看操作系统默认的Shell
  1. linux@deepin:~$ echo $SHELL
  2. /bin/bash
Copy the Code

SHELL是一个环境变量,打印它的值就可以知道用的是什么shell

  1. linux@deepin:~$ cat /etc/passwd | grep root
  2. root : x : 0 : 0 : root : /root : /bin/bash
Copy the Code

查看管理员账号后面的登录shell,就可以看出用的是什么shell

二、脚本的命令解释器
  1. #!/bin/bash
Copy the Code

指定了命令的解释器,它要放到第一行,否则会被当成注释信息。 linux中一般默认是bash解释器,如果不指定用默认的。对于不同的shell可以指定不同的解释器,如:/bin/sh

三、脚本的注释
在shell中如果遇到了#,后面的内容就是注释。 注释是对脚本的说明,不会被当成程序来执行,用于给开发者看的。要养成写注释的习惯。

  1. #!/bin/bash
  2. # author: laomo

  3. echo "Hello linux!"
Copy the Code

其中author: laomo就是注释信息, 当然如果#放到字符串中,则是属于字符串的普通字符,而不是注释标记。

四、脚本的执行
第一种:
  1. linux@deepin:~$ /bin/bash hello.sh
  2. Hello linux!
  3. linux@deepin:~$ /bin/sh hello.sh
  4. Hello linux!
Copy the Code

这种方式可以执行没有x权限的脚本。当脚本第一行没有指定用什么shell解释这个程序时,可以使用这种方法,相当于直接用指定的解释器去执行,推荐这种执行方式。

第二种:
  1. linux@deepin:~$ /home/linux/hello.sh
  2. Hello linux!
  3. linux@deepin:~$ ./hello.sh
  4. Hello linux!
Copy the Code

这种方式需要给脚本添加执行权限(x),可以指定绝对路径或相对路径。在定时任务等功能执行脚本时,为了避免出现问题,建议用第一种。

第三种:
  1. linux@deepin:~$ source hello.sh
  2. Hello linux!
  3. linux@deepin:~$ . hello.sh
  4. Hello linux!
Copy the Code

用source或 . 来载入并执行脚本。这种方式执行的脚本会在当前shell中执行,不会产生一个子shell来执行。 而上两种方式会启动新的进程执行脚本。

. 和 source是一样的,他们都是载入并执行脚本,脚本文件不需要有x权限。

需要注意用source和 . 执行的脚本是在当前shell中执行的,所以脚本中定义的变量,包括函数在当前shell中依然存在。而用bash,sh执行的脚本会启动新的子shell执行,执行完返回父shell,因此脚本中定义的变量无法保留。

可以写多个shell,之后用source进行组合。

Example:

  1. #!/bin/bash
  2. #filename: lib.sh

  3. myname="laomo"
Copy the Code

lib.sh中定义了一个变量。

  1. #!/bin/bash
  2. #filename: hello.sh
  3. # author: laomo

  4. . lib.sh
  5. echo $myname
  6. echo "Hello linux!"
Copy the Code

hello.sh中先把lib.sh载入执行一次,由于 . 会在当前shell中执行,也就是hello.sh这个子shell中执行,所以里面定义的变量myname,相当于定义在了hello.sh中,也就是可访问的。

  1. linux@deepin:~$ /bin/bash hello.sh
  2. laomo
  3. Hello linux!
Copy the Code

通过打印可以看出,hello.sh中打印了lib.sh中定义的变量,这就是source 和 . 的功能。

  1. linux@deepin:~$ echo $myname
Copy the Code

此时发现什么也没有输出,因为 . 在hello.sh子shell中执行的, 所以在当前shell中并不存在myname的变量。

  1. linux@deepin:~$ source lib.sh
  2. linux@deepin:~$ echo $myname
  3. laomo
Copy the Code

在当前shell中执行了source,所以再次打印myname变量,成功访问。

source或 . 可以交将执行的脚本中的变量传递到当前SHELL中

第四种:
  1. linux@deepin:~$ bash < hello.sh
  2. laomo
  3. Hello linux!
  4. linux@deepin:~$ cat hello.sh | bash
  5. laomo
  6. Hello linux!
Copy the Code

这两种方法是通过重定向实现的,用的比较少。

总结:

用source或 . 加载执行的脚本,由于是在当前shell中执行,因此执行结束后,脚本中的变量(包括函数)值在当前shell中依然存在,而sh和bash执行脚本都启动新的子shell执行,执行完后退回到父shell,因此脚本中的变量(包括函数)值无法保留。

别外还要注意:

  • 子shell会继承父shell中的变量,函数。
  • 父shell不可以访问子shell中的变量,函数。 但可以用source 或 . 加载执行子shell,这样就可以了。

五、基本规范
  • 第一行是指定脚本解释器:


  1. #!/bin/bash
Copy the Code

  • 添加版本,版权信息


  1. #date: 2018-9-29
Copy the Code

  • 在shell脚本中尽量不用中文
  • 脚本的扩展名应以.sh命名
  • shell脚本应该放在固定的路径下


Reply Favorite View the author
All Replies
avatar
aida
deepin
2018-10-18 19:13
#1
支持一波!
Reply View the author