0%

Linux-bash

Bash

shell是一种弱类型的编程语言

配置文件

profile:定义环境变量,运行程序或者脚本,只在用户登录时才会运行该配置文件

bashrc:设置本地变量,定义命令别名

登录式shell,配置文件及次序

/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc

非登录式shell,配置文件及次序

~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh


linux变量

变量赋值:变量名=变量值

撤销变量:unset 变量名

引用变量:$变量名

bash变量类型

  1. 环境变量:作用域为当前shell进程及其子进程
    1. 查看环境变量
      1. printenv
      2. env
      3. export
  2. 本地变量:作用域为整个bash进程
    1. 查看本地变量
      1. set
  3. 特殊变量
    1. $?
      1. 返回上一个命令执行状态返回值,值为0代表程序正确执行,非零都为错误执行
    2. $#
      1. 返回传入参数个数
    3. $@
      1. 返回参数内容,多个参数返回列表
    4. $*
      1. 返回所有参数
    5. $RANDOM
      1. 生成随机数
    6. $$
      1. 返回当前进程id
    7. $!
      1. 后台运行的最后一个进程的id号
  4. 位置变量
    1. $0
      1. 脚本名,带上所有引用路径
    2. $1-9
      1. 参数
    3. 注意,$10 不能获取第十个参数,获取第十个参数需要​${10}。当n>=10时,需要使用${n}来获取参数

shift命令,踢出参数


数组

  1. 声明数组
    1. declare -a 数组名
  2. 数组赋值
    1. 数组名={元素1 元素2 ……}
      1. human={jack tom divint3}
    2. 数组名={[元素位置1]=元素值1 [元素位置]2=元素2 ……}
      1. human={[0]=jack [1]=tom [6]=Divint3}

管道与重定向

输入输出重定向

  1. 标准输入:代码为 0 ;或称为 stdin ;使用的方式为 <
  2. 标准输出:代码为 1 ;或称为 stdout;使用的方式为 1>
  3. 错误输出:代码为 2 ;或称为 stderr;使用的方式为 2>

输入重定向,一般输入到一个程序中:< 输出通定向:> 追加输出重定向:>> 在此处生成文档 Here Document:<<

例如: 不指定输出位置,则输出到屏幕:

1
2
3
4
5
6
root@Divint3:~# cat << EOF
> The First Line.
> The Second Line.
> EOF
The First Line.
The Second Line.

指定输出位置,则输出到指定位置:

1
2
3
4
5
6
7
root@Divint3:~# cat << EOF >> doc.txt
> The First Line.
> The Second Line.
> EOF
root@Divint3:~# cat doc.txt
The First Line.
The Second Line.

bash中使用><|时 -用作占位符

set -C:阻止覆盖重定向 强制覆盖输出 >| set +C:取消阻止覆盖重定向重定向

tee: 既能保证输出可以输出到标准输出(屏幕),也能保证输出到文件中.

1
2
3
4
root@Divint3:~# echo "echo" | tee /tmp/a
echo
root@Divint3:~# cat /tmp/a
echo

重定向所有输出(标准输出以及标准错误输出)

ls > /dev/null 2>&1


bash编程

流程控制

分支结构

多行if结构
1
2
3
4
5
6
7
if condition
then
command1
command2
...
commandN
fi
单行if结构
1
if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
if else
1
2
3
4
5
6
7
8
9
if condition
then
command1
command2
...
commandN
else
command
fi
if elif else
1
2
3
4
5
6
7
8
9
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
casein
模式1)
command1
command2
...
commandN
;;
模式2)
command1
command2
...
commandN
;;
esac

循环结构

for循环
1
2
3
4
5
6
for var in item1 item2 ...;
do
command1
command2
...
done
while循环
1
2
3
4
while condition;
do
command
done
until循环
1
2
3
4
until condition
do
command
done

跳出循环

continue:跳出本次循环进行下一次循环

break:跳出循环


基本运算符

布尔运算符

运算符说明
非运算
-o或运算
-a与运算

关系运算符

运算符说明
-eq相等返回true
-ne不相等返回true
-gt大于返回true
-lt小于返回true
-ge大于等于返回true
-le小于等于返回true

字符串运算符

运算符说明
=字符串相等返回true
!=字符串不相等返回true
-z字符串长度为0返回true
-n字符串长度不为0返回true
$字符串不为空返回true

文件测试运算符

运算符说明
-b是块设备文件返回true
-c是字符设备文件返回true
-d是文件夹返回true
-f是普通文件返回true
-g设置了sgid位返回true
-k设置了粘滞位返回true
-p是有名管道文件返回true
-u设置了suid位返回true
-r是可读文件返回true
-w是可写文件返回true
-x是可执行文件返回true
-s是空文件返回true
-e文件/文件夹存在返回true
-L文件存在链接返回true

函数

1
2
3
4
5
<function> funname(){
command1
command2
...
}

bash检查语法以及查看执行过程

1
2
bash -n   test.bash   #查看bash是否存在语法错误
bash -x test.bash #查看bash详细的执行过程

信号

1
2
3
4
5
6
7
8
9
10
11
12
13
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

Ctrl+c:sigint=kill -2

trap信号捕捉

trap是不能捕捉sigkill信号的,kill -9 发送sigkill信号,程序必定停止运行

read读变量


脚本使用选项

getopts

内置变量$OPTARG,用于接收选项参数

选项索引$OPTIND

获得最后的参数shift ${$OPTIND-1}

恰饭,恰饭