Shell-基础(一):Shell解释器、Shell脚本

二、Shell解释器

1. Shell解析器种类

Linux提供的Shell解析器有以下几种(linux是开源的,不同的人写了不同风格的解释器):

  • /bin/sh(UNIX默认的shell,已经被/bin/bash取代)
  • /bin/bash(Linux默认的shell)
  • /sbin/nologin
  • /bin/dash
  • /bin/tcsh
  • /bin/csh
[root@localhost ~]$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

不同的shell解析器,功能不同,比如csh,符合c语言风格的shell解析器。Centos默认使用的是“/bin/bash”和“/bin/sh”作为shell解析器。“/bin/sh”是“/bin/bash”的软连接,两者等价。

2. 查看当前解释器

通过“echo $SHELL”命令查看当前解释器

[root@localhost ~]$ echo $SHELL
/bin/bash

在终端中输入:cat /etc/shells等价于:/bin/bash -c ‘cat /etc/shells’

默认/bin/bash必须接收一个脚本作为输入!如果是一条命令,需要加-c (command)
[root@localhost ~]$ /bin/bash -c 'cat /etc/shells'
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

3. Shell脚本的编写要求

  1. 声明解释器: #!/bin/bash
  2. 正文: 必须是shell解释器能解释的命令
[root@localhost /]$ cd home
[root@localhost home]$ ls
whx
[root@localhost home]$ cd whx
[root@localhost whx]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[root@localhost whx]$ mkdir myshells
[root@localhost whx]$ ll
total 0
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Desktop
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Documents
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Downloads
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Music
drwxr-xr-x. 2 root root 6 Oct 28 22:13 myshells
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Pictures
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Public
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Templates
drwxr-xr-x. 2 whx  whx  6 Aug 13 05:01 Videos
[root@localhost whx]$ cd myshells
[root@localhost myshells]$ ll
total 0
[root@localhost myshells]$ vim first.sh
[root@localhost myshells]$ ll
total 4
-rw-r--r--. 1 root root 51 Oct 28 22:15 first.sh
[root@localhost myshells]$ bash first.sh
这是shell脚本文件first.sh
[root@localhost myshells]$ sh first.sh
这是shell脚本文件first.sh

first.sh文件内容为:

#!/bin/bash
echo 'first.sh脚本文件执行完毕'

second.sh文件内容为:

#!/bin/bash
b='这是second.sh'
sleep 10s
echo $b
echo 'second.sh脚本文件执行完毕'

third.sh文件内容为:

#!/bin/bash
echo $b
echo 'third.sh脚本文件执行完毕'

虽然linux系统的文件不需要用后缀,但是一般用后缀.sh表明该文件是一个shell脚本文件,方便识别。

4. Shell脚本的执行方式

  1. bash 脚本sh 脚本
[root@localhost myshells]$ bash first.sh
这是shell脚本文件first.sh
[root@localhost myshells]$
特点: 
	①新开一个bash执行脚本;
	②一旦脚本执行完毕,bash自动关闭!

使用pstree查看进程

        ├─sshd─┬─sshd───bash───bash───sleep
        │      └─sshd───bash───pstree
  1. ./脚本

    特点:
    ①前提是当前用户对脚本有执行权限,使用当前默认的解释器执行脚本;
    ②新开一个bash执行脚本;
    ③一旦脚本执行完毕,bash自动关闭!
    添加权限:chmod u+x first.sh
    删除权限:chmod u-x first.sh

[root@localhost myshells]$ ll
total 4
-rw-r--r--. 1 root root 51 Oct 28 22:15 first.sh
[root@localhost myshells]$ ./first.sh
-bash: ./first.sh: Permission denied
[root@localhost myshells]$ chmod u+x first.sh
[root@localhost myshells]$ ll
total 4
-rwxr--r--. 1 root root 51 Oct 28 22:15 first.sh
[root@localhost myshells]$ ./first.sh
这是shell脚本文件first.sh
[root@localhost myshells]$ 

使用pstree查看进程

        ├─sshd─┬─sshd───bash───second.sh───sleep
        │      └─sshd───bash───pstree
  1. . 脚本
[root@localhost myshells]$ . first.sh
这是shell脚本文件first.sh
[root@localhost myshells]$ 
特点: 
	①使用当前默认的解释器执行脚本,并不要求当前用户对脚本有执行权限;
	②使用当前默认的解释器执行脚本

使用pstree查看进程

├─sshd─┬─sshd───bash───sleep
│      └─sshd───bash───pstree
  1. source 脚本
[root@localhost myshells]$ source first.sh
这是shell脚本文件first.sh
[root@localhost myshells]$ 
特点: 
	①使用当前默认的解释器执行脚本,并不要求当前用户对脚本有执行权限;	
	②使用当前默认的解释器执行脚本

使用pstree查看进程

├─sshd─┬─sshd───bash───sleep
│      └─sshd───bash───pstree
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值