1.shell脚本的基本结构
脚本幻数,即脚本解释器,脚本运行的最优先指令,负责对脚本中其他命令进行解释
程序主题,程序主体通常由命令,执行逻辑控制器和数据组成
注释,脚本中的说明文字,不参与脚本执行,只是对脚本中的代码进行说明
2.脚本中的注释方法
root@localhost shellsrcpit]# sh fanll.sh
^Z
[1]+ Stopped sh fanll.sh
[root@localhost shellsrcpit]# ps f
PID TTY STAT TIME COMMAND
2444 pts/0 Ss 0:00 -bash
2520 pts/0 T 0:00 \_ sh fanll.sh
2521 pts/0 T 0:00 | \_ cat
2522 pts/0 R+ 0:00 \_ ps f
[root@localhost shellsrcpit]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 4096 0 4096 0% /dev
tmpfs 886580 0 886580 0% /dev/shm
tmpfs 354636 7216 347420 3% /run
/dev/mapper/rhel-root 17141760 4889644 12252116 29% /
/dev/nvme0n1p2 983040 298268 684772 31% /boot
/dev/nvme0n1p1 613184 7140 606044 2% /boot/efi
/dev/sr0 10281784 10281784 0 100% /rhel9
tmpfs 177316 52 177264 1% /run/user/42
tmpfs 177316 36 177280 1% /run/user/0
[root@localhost shellsrcpit]#
[root@localhost shellsrcpit]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 Nov 24 2022 /bin/sh -> bash
[root@localhost shellsrcpit]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 1388904 Nov 24 2022 /bin/bash
[root@localhost shellsrcpit]#
1).脚本书写
[root@localhost shellsrcpit]# vim fanll.sh
[root@localhost shellsrcpit]# vim fanll.sh
[root@localhost shellsrcpit]# chmod +x fanll.sh
[root@localhost shellsrcpit]# ./fanll.sh
^Z
[2]+ Stopped ./fanll.sh
[root@localhost shellsrcpit]# ps f
PID TTY STAT TIME COMMAND
2444 pts/0 Ss 0:00 -bash
2520 pts/0 T 0:00 \_ sh fanll.sh
2521 pts/0 T 0:00 | \_ cat
2545 pts/0 T 0:00 \_ /bin/bash ./fanll.sh
2546 pts/0 T 0:00 | \_ cat
2547 pts/0 R+ 0:00 \_ ps f
[root@localhost shellsrcpit]#
脚本中通常用#注释单行内容
[root@localhost shellsrcpit]# vim fanll.sh
[root@localhost shellsrcpit]# sh fanll.sh
hello fanll
[root@localhost shellsrcpit]#
多行注释
[root@localhost shellsrcpit]# vim fanll.sh
[root@localhost shellsrcpit]# sh fanll.sh
hello fanll
[root@localhost shellsrcpit]#
书写规范和注意事项
脚本文件名应见名知意,例如
backup_mysql.sh
文件开头指定脚本解释器
#!/bin/sh
或
#!/bin/bash
脚本中尽量不要用中文注释,防止本机或切换系统环境后中文乱码的困扰
多使用内部命令,如:
echo
、
eval
、
exec
、
export
、
read
、
shift
、
exit
尽量用少的命令完成动作
[root@localhost shellsrcpit]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost shellsrcpit]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost shellsrcpit]#
开头加版本特权等信息
# Date:
创建日期
# Author:
作者
# Mail:
联系方式
# Function:
功能
# Version:
版本

shell脚本需求
[root@localhost shellsrcpit]# sh host_message.sh \
> ipaddress: 192.168.79.136 192.168.79.140 \
> username: root \
> work dir: /mnt
第一个脚本
[root@localhost shellsrcpit]# vim host_message.sh
[root@localhost shellsrcpit]# sh host_message.sh
ipaddress: 192.168.79.136 192.168.79.140
username: root
work dir: /root/shellsrcpit
[root@localhost shellsrcpit]#
vim
[root@localhost shellsrcpit]# vim ~/.vimrc
[root@localhost shellsrcpit]# vim file
[root@localhost shellsrcpit]#
脚本执行方法
在当前环境下运行
方法一
[root@localhost shellsrcpit]# vim fan.sh
root@localhost shellsrcpit]# . fan.sh &
[4] 2658
[root@localhost shellsrcpit]# ps f
PID TTY STAT TIME COMMAND
2444 pts/0 Ss 0:00 -bash
2520 pts/0 T 0:00 \_ sh fanll.sh
2521 pts/0 T 0:00 | \_ cat
2545 pts/0 T 0:00 \_ /bin/bash ./fanll.sh
2546 pts/0 T 0:00 | \_ cat
2641 pts/0 T 0:00 \_ -bash
2642 pts/0 T 0:00 | \_ cat
2658 pts/0 T 0:00 \_ -bash
2659 pts/0 T 0:00 | \_ cat
2660 pts/0 R+ 0:00 \_ ps f
[4]+ Stopped . fan.sh
[root@localhost shellsrcpit]#
方法二
[root@localhost shellsrcpit]# source fan.sh &
[5] 2661
[root@localhost shellsrcpit]# ps f
PID TTY STAT TIME COMMAND
2444 pts/0 Ss 0:00 -bash
2520 pts/0 T 0:00 \_ sh fanll.sh
2521 pts/0 T 0:00 | \_ cat
2545 pts/0 T 0:00 \_ /bin/bash ./fanll.sh
2546 pts/0 T 0:00 | \_ cat
2641 pts/0 T 0:00 \_ -bash
2642 pts/0 T 0:00 | \_ cat
2658 pts/0 T 0:00 \_ -bash
2659 pts/0 T 0:00 | \_ cat
2661 pts/0 T 0:00 \_ -bash
2662 pts/0 T 0:00 | \_ cat
2663 pts/0 R+ 0:00 \_ ps f
[5]+ Stopped source fan.sh
[root@localhost shellsrcpit]#
在指定环境中运行
[root@localhost shellsrcpit]# sh fan.sh &
[6] 2665
[root@localhost shellsrcpit]# ps f
PID TTY STAT TIME COMMAND
2444 pts/0 Ss 0:00 -bash
2520 pts/0 T 0:00 \_ sh fanll.sh
2521 pts/0 T 0:00 | \_ cat
2545 pts/0 T 0:00 \_ /bin/bash ./fanll.sh
2546 pts/0 T 0:00 | \_ cat
2641 pts/0 T 0:00 \_ -bash
2642 pts/0 T 0:00 | \_ cat
2658 pts/0 T 0:00 \_ -bash
2659 pts/0 T 0:00 | \_ cat
2661 pts/0 T 0:00 \_ -bash
2662 pts/0 T 0:00 | \_ cat
2665 pts/0 T 0:00 \_ sh fan.sh
2666 pts/0 T 0:00 | \_ cat
2667 pts/0 R+ 0:00 \_ ps f
[6]+ Stopped sh fan.sh
[root@localhost shellsrcpit]#
命令退出值
1.什么是退出值
- 在UNIX或者Linux中,每个命令都会返回一个退出状态码
- 退出状态码是一个整数,其有效范围为0~255
- 通常情况下,成功的命令返回0,而不成功的命令返回非0值。非0值通常都被解释成一个错误码
-
行为良好的UNIX命令,程序和工具都会返回0作为退出码来表示成功。
查看和更改退出值
查看退出值
[root@localhost shellsrcpit]# ls /mnt/fanll
ls: cannot access '/mnt/fanll': No such file or directory
[root@localhost shellsrcpit]# ls /mnt/fan
ls: cannot access '/mnt/fan': No such file or directory
[root@localhost shellsrcpit]# echo $?
2
[root@localhost shellsrcpit]#
[root@localhost shellsrcpit]# touch /mnt/fanll
[root@localhost shellsrcpit]# ls /mnt/fanll
/mnt/fanll
[root@localhost shellsrcpit]# echo $?
0
[root@localhost shellsrcpit]#
修改脚本退出值
未指定退出值
[root@localhost shellsrcpit]# vim fanll.sh
[root@localhost shellsrcpit]# vim fan.sh
[root@localhost shellsrcpit]# sh fan.sh
Tue Dec 3 12:21:30 AM CST 2024
[root@localhost shellsrcpit]# echo $?
0
[root@localhost shellsrcpit]#
指定退出值
[root@localhost shellsrcpit]# vim fan.sh
[root@localhost shellsrcpit]# sh fan.sh
Tue Dec 3 12:23:03 AM CST 2024
[root@localhost shellsrcpit]# echo $?
20
[root@localhost shellsrcpit]#