shell脚本(一)
前言
shell的为用户提供了操作操作系统的接口,包括man,chmod,vi,fdisk,mkfs命令,这些都是独立的应用程序。
一、脚本程序设计
1.变量
定义:变量就是一组文字或符号等,来替换一些设置或者是一串保留数据。
1.1. 查看变量
利用echo能够读出,只需要在变量名前面加上$variable
或(${variable}
)
root@ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/heat/heat_test/host/bin
root@ubuntu:~#
1.2. 定义变量
举例一:
root@ubuntu:~# myname=john.jia
root@ubuntu:~# echo $myname
john.jia
root@ubuntu:~#
举例二:
root@ubuntu:~# version=$(uname -a)
root@ubuntu:~# echo $version
Linux ubuntu 5.0.0-32-generic #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
举例三:
root@ubuntu:~# var1=/lib/modules/$(uname -r)/kernel
root@ubuntu:~# echo $var1
/lib/modules/5.0.0-32-generic/kernel
1.3. 环境变量
若使该变量为环境变量,则export
声明。
二、shell基本命令使用
2.1. 获取当前shell的PID
root@ubuntu:~# echo $$
7567
root@ubuntu:~#
2.2. 单引号、双引号
单引号会丢变量内容,输出变成普通字符。如:
root@ubuntu:~# name=Vbird
root@ubuntu:~# echo $name
Vbird
root@ubuntu:~# myname='$name its me'
root@ubuntu:~# echo $myname
$name its me
root@ubuntu:~# myname="$name its me"
root@ubuntu:~# echo $myname
Vbird its me
root@ubuntu:~#
2.3. 回传码
回传码?,关于上一个命令执行的回传码。
2.4. 命令别名
alias、unalias。
举例一:
root@ubuntu:~# alias lm='ls -l | more'
root@ubuntu:~# lm
total 4
drwxr-xr-x 7 root root 4096 Apr 14 2021 snap
root@ubuntu:~#
举例二:
root@ubuntu:~# alias cls='clear'
root@ubuntu:~# cls
2.5. 读入环境配置文件
root@ubuntu:~# source ~/.bashrc
root@ubuntu:~#
2.6. bash默认组合键
快捷键 | 描述 |
---|---|
Ctrl+C | 终止命令 |
Ctrl+D | 输入结束 |
Ctrl+M | 相当于Enter |
Ctrl+S | 暂停屏幕输出 |
Ctrl+Q | 恢复屏幕输出 |
Ctrl+U | 整行命令删除 |
Ctrl+Z | 暂停目前命令 |
2.7. cut
cut
分解提取命令,-d是按照分隔符号选项,-f是选择-d分割之后的第几块。
root@ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/heat/heat_test/host/bin:/home/heat/heat_test/host/bin
root@ubuntu:~# echo $PATH | cut -d ':' -f 1
/usr/local/sbin
root@ubuntu:~# echo $PATH | cut -d ':' -f 2
/usr/local/bin
root@ubuntu:~# echo $PATH | cut -d ':' -f 3
/usr/sbin
root@ubuntu:~# echo $PATH | cut -d ':' -f 1,3
/usr/local/sbin:/usr/sbin
root@ubuntu:~#
2.8. grep
cut是在一行中提取数据,grep是分析一行需信息,若当中有我们需要的信息,则拿出该行。
举例一摘出eth0字符相关内容的行:dmesg | grep ‘eth0’
root@ubuntu:~# dmesg | grep 'eth0'
[ 2.592972] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:b0:4a:af
[ 2.592975] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection
[ 2.594484] e1000 0000:02:01.0 ens33: renamed from eth0
root@ubuntu:~#
举例二摘出eth0字符内容的行:dmesg| grep -n ‘eth0’
root@ubuntu:~# dmesg| grep -n 'eth0'
1442:[ 2.592972] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:b0:4a:af
1443:[ 2.592975] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection
1444:[ 2.594484] e1000 0000:02:01.0 ens33: renamed from eth0
root@ubuntu:~#
举例三摘出数字0-9内容的行
root@ubuntu:~# dmesg| grep -n '0-9'
43:[ 0.003415] 00000-9FFFF write-back
root@ubuntu:~# dmesg| grep -n [0-9]
1:[ 0.000000] Linux version 5.0.0-32-generic (buildd@lgw01-amd64-015) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #34~18.04.2-Ubuntu SMP Thu Oct 10 10:36:02 UTC 2019 (Ubuntu 5.0.0-32.34~18.04.2-generic 5.0.21)
2:[ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.0.0-32-generic root=UUID=22a9c086-39e5-447d-94bb-7ad94e3a466b ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet
3:[ 0.000000] KERNEL supported cpus:
4:[ 0.000000] Intel GenuineIntel
5:[ 0.000000] AMD AuthenticAMD
6:[ 0.000000] Hygon HygonGenuine
7:[ 0.000000] Centaur CentaurHauls
8:[ 0.000000] Disabled fast string operations
9:[ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
举例四摘出单引号‘内容的行
dmesg | grep \'
2.9. sort
sort排序。
2.10. find
find: 搜索文件,如:文件名搜索find . -name filename -print
root@ubuntu:~# ls
mkemmcboot.sh snap
root@ubuntu:~# find . -name mkemmcboot.sh -print
./mkemmcboot.sh
root@ubuntu:~#
2.11. tail
tail 打印文本文件末尾几行。
root@ubuntu:~# tail -n 20 mkemmcboot.sh
;;
--device | -d)
if [[ $# < 2 ]]; then
usage $0
exit 0
fi
DEVICE=$2
;;
--version | -v)
version $0
exit 0
;;
*)
usage $0
exit 0
;;
esac
main
#####################################
root@ubuntu:~#
2.12. head
head file 打印文本文件开头几行。
root@ubuntu:~# head -n 20 mkemmcboot.sh
#!/bin/bash
# Script function:Create eMMC system boot.
#
# Copyright 2018 Tronlong Elec. Tech. Co. Ltd. All Rights Reserved.
readonly VERSION="1.0"
DEVICE=""
# Mount points of boot, rootfs partition.
BOOT_MP="/tmp/$$-boot"
ROOTFS_MP="/tmp/$$-rootfs"
# Print error message
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $@" >&2
}
# Command execution function
execute() {
root@ubuntu:~#
3.11. man
如man查看echo帮助
root@ubuntu:~# man echo
ECHO(1) User Commands ECHO(1)
NAME
echo - display a line of text
SYNOPSIS
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version
output version information and exit
If -e is in effect, the following sequences are recognized:
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\e escape
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\0NNN byte with octal value NNN (1 to 3 digits)
\xHH byte with hexadecimal value HH (1 to 2 digits)
NOTE: your shell may have its own version of echo, which usually
supersedes the version described here. Please refer to your shell's
documentation for details about the options it supports.
三、shell正则表达式使用
3.1. sed
sed对数据进行替换、删除、新增、选取。
3.2. awk
awk把一行分成数段处理。
3.3. diff
diff文件比较。
root@ubuntu:~# diff /etc/rc2.d/ ./
Only in ./: .bash_history
Only in ./: .bashrc
Only in ./: .cache
Only in ./: .gnupg
Only in ./: .local
Only in ./: mkemmcboot.sh
3.4. cmp
cmp利用字节比较。
四、shell script
4.1. shell script良好习惯
每个脚本头处记录内容:
script的功能;
script的版本;
script的作者;
script的版权声明;
script内特殊命令;
script执行时需要的环境变量预先声明和设置。
4.2. hello world模板
#!/bin/sh
# Script Function:输出hello world 字符串.
# Copyright 2021 xx Elec. Tech. Co. Ltd. All Rights Reserved.
# Author: john.jia
# Version: 1.0.0.1
# Date: 2021-11-1
str="hello world"
# 现在打印变量a的内容:
echo "A is:"
echo $str
4.3. 交互式脚本
使用read命令
#!/bin/sh
# Script Function:获取字符串.
# Copyright 2021 xx Elec. Tech. Co. Ltd. All Rights Reserved.
# Author: john.jia
# Version: 1.0.0.1
# Date: 2021-11-1
str="hello world"
read -p "please input a string:" str #提示输入字符串
echo “ the str is : $str ”
4.4. 判断
test 命令是Shell 脚本中用来进行条件判断的。
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# test "$a" != "$b" && echo yes || echo no
no
[root@localhost mnt]# [ "$a" != "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=0
[root@localhost mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=2
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=3
[root@localhost mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=1
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
no
[root@localhost mnt]# a=2
[root@localhost mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# c=3
[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# b=1
[root@localhost mnt]# [ "$a" -lt "$b" -a "$b" -lt "$c" ] && echo yes || echo no
no
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# c=3
[root@localhost mnt]# [ "$a" -lt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "$a" -gt "$b" -o "$b" -gt "$c" ] && echo yes || echo no
no
[root@localhost mnt]# c=''
[root@localhost mnt]# [ -z "$c" ] && echo yes || echo no
yes
[root@localhost mnt]# c=2
[root@localhost mnt]# [ -z "$c" ] && echo yes || echo no
no
举例:
输入参数个数小于等于1:
if [$# -lt '1']; then
echo "$0"
exit 0
fi
4.5. shell script 默认变量$0$1…
举例:
/home/root/scriptname opt1 opt2 opt3
$0
= /home/root/scriptname
$1
= opt1
$2
= opt2
$3
= opt3
…
还有一些常用的默认参数,
$#
代表后接的参数个数;
$@
代表双引号括起来的变量;
$*
代表“$1c$2c$3c$4”,其中c代表分隔符。
4.6. function函数
function fname(){
程序段
}
4.7. 循环
while [ 条件]
do
程序段落
done
for var in con1 con2 con3 ...
do
程序段落
done
感谢阅读,祝君成功!
-by aiziyou