shell学习笔记

221 阅读4分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第10天,点击查看活动详情

一.Shell是什么?

Shell 既是一种脚本编程语言,也是一个连接内核和用户的软件(命令解释器)。

Shell作为命令解释器,它在操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出屏幕返回给用户。

Shell作为脚本编程语言,可以通过命令、变量、流程控制语句等形式完成一条或一组脚本语言,提供给解释器进行解释运行。

Shell脚本命令的工作方式有两种,交互式和批处理:

  • 交互式(Interactive):用户每输入一条命令就立即执行。
  • 批处理(Batch):由用户事先编写好一个完整的Shell脚本,Shell会一次性执行脚本中诸多的命令。

二、几种常见的Shell

常见的 Shell 有 sh、bash、csh、tcsh、ash 等。

  • sh(Bourne shell),由 AT&T 公司的 Steve Bourne开发,为了纪念他,就用他的名字命名了。sh 是 UNIX 上的标准 shell,很多 UNIX 版本都配有 sh。sh 是第一个流行的 Shell。
  • bash(bash shell) 由 GNU 组织开发,保持了对 sh shell 的兼容性,是各种 Linux 发行版默认配置的 shell。
  • csh是sh 之后另一个广为流传的 shell ,它是由加州大学伯克利分校的 Bill Joy 设计的,因其语法有点类似C语言,所以才得名为 C shell ,简称为 csh。
  • tcsh 是 csh 的增强版,加入了命令补全功能,提供了更加强大的语法支持。
  • ash一个简单的轻量级的 Shell,占用资源少,适合运行于低内存环境,但是与 bash shell 完全兼容。

查看系统中的Shell

[root@hollowman ~]# cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash

查看系统默认的Shell

[root@hollowman ~]# echo $SHELL
/bin/bash

三、bash 的几个功能

既然bash是大多数linux发行版默认的shell,那就学学bash的几个有趣的功能吧。

1.历史命令

bash会将使用过的命令保存在 ~/.bash_history文件中, 并可通过history命令查看,一般可以保存最近的1000条命令记录

$ history
$ cat ~/.bash_history 

2.Tab补齐功能

安装bash-completion包后(一般的系统都默认安装了,没有安装的可以手动安装),终端中的bash命令在输入过程中,即可通过连续按Tab键来进行命令的补齐。

3.命令别名设置

可通过alias来进行命令别名的查看和设置

$ alias ll='ls -alF' 	#设置或修改命令别名 ll 为 ls -alF 命令
$ alias 				#查看已有别名

4.通配符

$ grep "l*" a.txt     # *表示任意多个字符,其他统配符的了解可学习正则表达式

三、Shell脚本编程规范

1.建立一个专门存放脚本的目录

[root@hollowman ~]# mkdir  hollowman_scripts

2.脚本文件以.sh为扩展名(扩展名为.sh不是必须,但养成良好的习惯会让编程更轻松)

3. 脚本开头必须指定脚本解释器

如以#! /bin/bash指定脚本解释器为bash,(#!)又称为范数,用来告诉系统使用哪种Shell解释器来执行该脚本。

4.养成注释的好习惯 ,注释都以#开头

5.可以通过.vimrc文件来快速生成开头的注释信息

第一步:创建一个.vimrc文件,并将格式化内容

[root@hollowman ~]# cat  >> ~/.vimrc << EOF
> autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
> 
> func SetTitle()
>     if expand("%:e") == 'sh'
>         call setline(1,"#!/bin/bash")
>         call setline(2, "##############################################################")
>         call setline(3, "# File Name: ".expand("%"))
>         call setline(4, "# Version: V1.0")
>         call setline(5, "# Author: hollowman")
>         call setline(6, "# Organization: https://www.hollowman.cn")
>         call setline(7, "# Created Time : ".strftime("%F %T"))
>         call setline(8, "# Description:")
>         call setline(9, "##############################################################")
>         call setline(10, "")
>     endif
> endfunc
> EOF

第二步:查看该文件是否创建并输入了格式化内容

[root@hollowman ~]# cat .vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"

func SetTitle()
    if expand("%:e") == 'sh'
        call setline(1,"#!/bin/bash")
        call setline(2, "##############################################################")
        call setline(3, "# File Name: ".expand("%"))
        call setline(4, "# Version: V1.0")
        call setline(5, "# Author: hollowman")
        call setline(6, "# Organization: https://www.hollowman.cn")
        call setline(7, "# Created Time : ".strftime("%F %T"))
        call setline(8, "# Description:")
        call setline(9, "##############################################################")
        call setline(10, "")
    endif
endfunc

第三步:用vim创建.sh文件,查看是否有相应信息

[root@hollowman ~]# vim ./hollowman_scripts/newscript.sh
#!/bin/bash
##############################################################
# File Name: newscripts.sh
# Version: V1.0
# Author: hollowman
# Organization: https://hollowman.cn
# Created Time : 2020-12-21 23:31:24
# Description:
##############################################################

第四步:编写简单的脚本

[root@hollowman ~]# vim ./hollowman_scripts/newscript.sh
#!/bin/bash
##############################################################
# File Name: newscripts.sh
# Version: V1.0
# Author: hollowman
# Organization: https://hollowman.cn
# Created Time : 2020-12-21 23:31:24
# Description:
##############################################################
echo "the script file name is $0"
echo "here are $# parameters"

第五步:运行脚本

第一种方法:使用bash命令

[root@hollowman ~]# bash ./hollowman_scripts/newscript.sh  a b c d
the script file name is ./hollowman_scripts/newscripts.sh
here are 4 parameters

第二种方法:直接用文件路径/执行文件名 来执行

[root@hollowman ~]# ./hollowman_scripts/newscript.sh a b c d
the script file name is ./hollowman_scripts/newscripts.sh
here are 4 parameters

四、shell编程中的常用语法

1.几个命令行参数变量

$0:  当前Shell脚本程序的名称
$#:  命令行参数的个数
$*:  分别输出每个命令行参数的值
$?:  打印上一次命令的执行返回值
$N:  N为1,2,3,...{10},{11},...分别对应第N个参数值

2.条件测试[ ]

[ ]表示条件测试,其结果返回0或1。常用的条件测试有:

-d 		测试文件是否为目录类型
-e 		测试文件是否存在
-f 		判断是否为一般文件
-r 		测试当前用户是否有权限读取
-w 		测试当前用户是否有权限写入
-x 		测试当前用户是否有权限执行

注意:[ ]内所有元素前后都必须有空格,否则会报错。

3.整数比较运算符

-eq 	是否等于(equal)
-ne 	是否不等于(not equal)
-gt 	是否大于(greater than)
-lt 	是否小于(less than)
-le 	是否等于或小于
-ge 	是否大于或等于

4.字符比较运算符

= 	比较字符串内容是否相同
!= 	比较字符串内容是否不同
-z 	判断字符串内容是否为空

实例:

root@hollowman-F117:/home/hollowman/hollowman_scripts# cat test.sh 
root@hollowman-F117:/home/hollowman/hollowman_scripts# cat test.sh 
a=$1
b=$1
c=$3
d=$4
if [ $a -eq $b ];then 
        echo "$a与$b值相等"
else 
        echo "$a与$b值不相等"
fi

if [ $c = $d ];then
        echo "$c与$d内容相同"
else
        echo "$c与$d内容不相同"
fi

root@hollowman-F117:/home/hollowman/hollowman_scripts# ./test.sh 1 1 abc abc
11值相等
abc与abc内容相同

5.条件语句

if expression ;then
    command
    [break|exit]
elif expression ;then
    command
else
    command
fi

case语句:

case var in
pattern 1 )
    command 
    ;;
pattern 2 )
    command 
    ;;
*)
    command 
    ;;
esac

6.循环语句

#===while循环语句===
while [ cond1 ] && { || } [ cond2 ] …; do
    command
done

#===until循环语句===
until [ cond1 ] && { || } [ cond2 ] …; dodone

#===for循环语句表现形式一===
for var in …; do
    command
done

#===for循环语句表现形式二===
for (( cond1; cond2; cond3 )) do
    command
done