shell学习笔记

shell是基于shell解释器的脚本编程语言,也是使用Linux的常用工具,连接着用户和系统内核。

  • 查看操作系统支持shell解释器
$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh
  • 查看当前使用shell解释器
$ echo $SHELL
/bin/bash

一、一些基础

  1. #!/bin/bash:每个shell开头都有这句说明使用的解释器
  2. 执行脚本
  • 作为可执行程序
# #是shell的注释符
$ chmod +x ./test.sh#执行前先给可执行权限
$ ./test.sh
  • 作为解释器参数
$ /bin/bash test.sh
  1. 读入标准输入
#!/bin/bash
echo 'what is your name?'
read personname #通过read标准输入
echo "hello,$personname"

二、变量

  1. 变量的定义和重新赋值:不需要指定类型,都看作是字符串或者数组
variable=value #需要注意=号左右不能有空格

variable='value' #单引号原样输出

variable="value" #先解析里面的变量和命令再输出

variable=$(command) #等同于variable=`command`(`反引号),将命令执行结果赋值给变量

variable=(value0 value1 value2 value3) #定义了一个数组
  1. 变量的使用
  • 方式一:
$NAME
  • 方式二:
${NAME}
#${#NAME}获取字符串长度
#${NAME:1:3}提取子串
  1. 常见shell变量的组成
  • 局部变量:当前脚本或命令中定义,只在当前shell实例中有效
  • 环境变量
  • 特殊变量:见下表
变量 含义
$$ 当前Shell进程ID
$# 传递给脚本或函数的参数个数
$* 传递给脚本或函数的所有参数
$@ 传递给脚本或函数的所有参数;
和$的区别:当被双引号包含时,$作为整体输出,而$@依然是分别输出$1 $2...
$n 传递给脚本或函数的第n个参数;
$0:执行的命令名称、$1:第一个参数、$2:第二个参数依次类推
$? 上个命令的退出状态,或函数的返回值

三、运算符

  1. 算术运算符:需要expr支持才能实现算术运算
运算符 说明 举例
+ 加法 expr $a + $b 结果为 30
- 减法 expr $a - $b 结果为 10
* 乘法 expr $a \* $b 结果为 200
/ 除法 expr $b / $a 结果为 2
% 取余 expr $b % $a 结果为 0
  1. 关系运算符:仅支持数字比较
运算符 说明 举例
-eq 检测两个数是否相等,相等返回 true [ $a -eq $b ] 返回 true
-ne 检测两个数是否相等,不相等返回 true [ $a -ne $b ] 返回 true
-gt 检测左边的数是否大于右边的,如果是,则返回 true [ $a -gt $b ] 返回 false
-lt 检测左边的数是否小于右边的,如果是,则返回 true [ $a -lt $b ] 返回 true
-ge 检测左边的数是否大等于右边的,如果是,则返回 true [ $a -ge $b ] 返回 false
-le 检测左边的数是否小于等于右边的,如果是,则返回 true [ $a -le $b ] 返回 true
  1. 布尔运算符
运算符 说明 举例
! 非运算,表达式为 true 则返回 false,否则返回 true [ ! false ] 返回 true
-o 或运算,有一个表达式为 true 则返回 true [ $a -lt 20 -o $b -gt 100 ] 返回 true
-a 与运算,两个表达式都为 true 才返回 true [ $a -lt 20 -a $b -gt 100 ] 返回 false
  1. 字符串运算符
运算符 说明 举例
= 检测两个字符串是否相等,相等返回 true [ $a = $b ] 返回 false
!= 检测两个字符串是否相等,不相等返回 true [ $a != $b ] 返回 true
-z 检测字符串长度是否为0,为0返回 true [ -z $a ] 返回 false
-n 检测字符串长度是否为0,不为0返回 true [ -z $a ] 返回 true
str 检测字符串是否为空,不为空返回 true [ $a ] 返回 true
  1. 文件测试运算符(仅列举常用的)
运算符 说明 举例
-d file 检测文件是否是目录,如果是,则返回 true [ -d $file ] 返回 false
-f file 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true [ -f $file ] 返回 true
-r file 检测文件是否可读,如果是,则返回 true [ -r $file ] 返回 true
-w file 检测文件是否可写,如果是,则返回 true [ -w $file ] 返回 true
-x file 检测文件是否可执行,如果是,则返回 true [ -x $file ] 返回 true
-s file 检测文件是否为空(文件大小是否大于0),不为空返回 true [ -s $file ] 返回 true
-e file 检测文件(包括目录)是否存在,如果是,则返回 true [ -e $file ] 返回 true

四、流程控制语句

  1. if语句
if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi
  1. for语句
for 变量 in 列表
do
    command1
    command2
    ...
    commandN
done
  1. while语句
while command
do
   Statement(s) to be executed if command is true
done
  1. 跳出循环:break 和 continue

五、函数

  1. 结构如下:
 [function] function_name () {
    list of commands
    [ return value ] 
    #return只能是整数
    #如果没有return,将最后一条命令运行结果作为返回值
}
#函数调用,只要使用函数名即可,后面跟的是入口参数
function_name para1 para2
  1. 一个例子
#!/bin/bash
funWithParam(){
    echo "The command is $0 !" 
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The amount of the parameters is $# !"  # 参数个数
    echo "The string of the parameters is $* !"  # 传递给函数的所有参数
    echo "This process id is $$"

    if [ $1 -eq $2 ]
    then
       echo '$1==$2'
    else
       echo '$1!=$2'
    fi

    #val=`expr $1 + $2 + $3`
    val=$(expr $1 + $2 + $3)
    return $val
}
funWithParam 1 2 3
echo "The sum of the three parameters is $?" #$?上个命令的退出状态,或函数的返回值

运行结果如下:

The command is ./test.sh !
The value of the first parameter is 1 !
The value of the second parameter is 2 !
The amount of the parameters is 3 !
The string of the parameters is 1 2 3 !
This process id is 5189
$1!=$2
The sum of the three parameters is 6

参考网址:http://c.biancheng.net/cpp/view/6994.html

你可能感兴趣的:(shell学习笔记)