“Shell基础”的版本间的差异
跳到导航
跳到搜索
(未显示同一用户的22个中间版本) | |||
第1行: | 第1行: | ||
− | == | + | |
+ | [[Shell札记]] | ||
+ | ==GNU Bash manual == | ||
+ | https://www.gnu.org/software/bash/manual/ | ||
+ | |||
+ | ==IF条件判断,判断条件== | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | 1 字符串判断 | ||
+ | |||
+ | str1 = str2 当两个串有相同内容、长度时为真 | ||
+ | str1 != str2 当串str1和str2不等时为真 | ||
+ | -n str1 当串的长度大于0时为真(串非空) | ||
+ | -z str1 当串的长度为0时为真(空串) | ||
+ | str1 当串str1为非空时为真 | ||
+ | |||
+ | 2 数字的判断 | ||
+ | |||
+ | int1 -eq int2 两数相等为真 | ||
+ | int1 -ne int2 两数不等为真 | ||
+ | int1 -gt int2 int1大于int2为真 | ||
+ | int1 -ge int2 int1大于等于int2为真 | ||
+ | int1 -lt int2 int1小于int2为真 | ||
+ | int1 -le int2 int1小于等于int2为真 | ||
+ | |||
+ | 3 文件的判断 | ||
+ | |||
+ | -r file 用户可读为真 | ||
+ | -w file 用户可写为真 | ||
+ | -x file 用户可执行为真 | ||
+ | -f file 文件为正规文件为真 | ||
+ | -d file 文件为目录为真 | ||
+ | -c file 文件为字符特殊文件为真 | ||
+ | -b file 文件为块特殊文件为真 | ||
+ | -s file 文件大小非0时为真 | ||
+ | -t file 当文件描述符(默认为1)指定的设备为终端时为真 | ||
+ | |||
+ | 3 复杂逻辑判断 | ||
+ | |||
+ | -a 与 | ||
+ | -o 或 | ||
+ | ! 非 | ||
+ | |||
+ | </pre> | ||
+ | [https://blog.csdn.net/ithomer/article/details/5904632 SHELL学习笔记----IF条件判断,判断条件] | ||
+ | |||
+ | =避免shell脚本被同时运行多次= | ||
+ | |||
+ | == 利用$0 排除自身== | ||
+ | <pre> | ||
+ | #!/bin/bash | ||
+ | root@evan-ThinkPad-Edge:~# cat /home/evan/tunnel | ||
+ | CPID=$$ #$$ 是脚本运行的当前进程ID号 | ||
+ | RUN="`ps -ef | grep "$0" | grep -v "grep" | grep -v "$CPID" | wc -l`" #$0 是脚本本身的名字 | ||
+ | [ "$RUN" -gt 1 ] && exit | ||
+ | ssh -i /home/evan/.ssh/id_rsa -p 22 -qtfnN -D 7070 [email protected] | ||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | ==lockfile == | ||
+ | <pre> | ||
+ | LOCK_FILE="/tmp/my.lock" | ||
+ | if [[ -e $LOCK_FILE ]] ; then | ||
+ | echo "re-entry, exiting" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | ### Placing lock file | ||
+ | touch $LOCK_FILE | ||
+ | |||
+ | 你要写的代码 | ||
+ | |||
+ | ### Removing lock | ||
+ | rm -f $LOCK_FILE | ||
+ | </pre> | ||
+ | |||
+ | [https://blog.csdn.net/timchen525/article/details/76407605?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_title~default-0.no_search_link&spm=1001.2101.3001.4242.1 Linux中如何防止一个Shell脚本被重复执行?] | ||
+ | |||
+ | [https://blog.csdn.net/sg656720274/article/details/73604551 如何避免shell脚本被同时运行多次] | ||
+ | |||
+ | [https://www.geek-share.com/detail/2632983934.html Shell脚本避免重复执行的方法 ] | ||
+ | |||
+ | [https://blog.51cto.com/lavasoft/243360 防止同一个Java应用重复启动的shell脚本] | ||
+ | |||
+ | =shell脚本特殊符号= | ||
+ | [https://www.cnblogs.com/xuxm2007/archive/2011/10/20/2218846.html shell脚本中一些特殊符号] | ||
+ | |||
+ | = MySQL backup shell script = | ||
+ | https://fedingo.com/shell-script-to-backup-mysql-database/ | ||
+ | |||
+ | https://www.fosstechnix.com/shell-script-for-mysql-database-backup/ | ||
+ | |||
+ | https://gist.github.com/lon-io/9067f746cd8cce10af4c52179c7e9b7e | ||
+ | |||
+ | https://orahow.com/linux-shell-script-for-mysql-database-backup/ | ||
+ | |||
+ | = shell规范= | ||
+ | |||
+ | https://www.shellcheck.net/ | ||
+ | |||
+ | [https://chegva.com/3977.html Shell编程规范实践指南] | ||
+ | |||
+ | [https://www.cnblogs.com/thatsit/p/shell-jiao-ben-shu-xie-gui-fan.html Shell脚本书写规范] | ||
+ | |||
+ | [https://www.iterdaily.com/post/shell-script-flavor/ shell书写规范建议] | ||
+ | |||
+ | [https://blog.csdn.net/lihonghai2392/article/details/77915439 shell脚本编写规范(超实用)] | ||
+ | =Shell 基本运算符= | ||
+ | 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。 | ||
+ | |||
+ | expr 是一款表达式计算工具,使用它能完成表达式的求值操作。 | ||
+ | |||
+ | 例如,两个数相加(注意使用的是反引号 ` 而不是单引号 ') | ||
+ | [https://www.runoob.com/linux/linux-shell-basic-operators.html Shell 基本运算符] | ||
+ | |||
+ | =Shell 流程控制= | ||
+ | <pre> | ||
+ | if else 的 [...] 判断语句中大于使用 -gt,小于使用 -lt。 | ||
+ | |||
+ | if [ "$a" -gt "$b" ]; then | ||
+ | ... | ||
+ | fi | ||
+ | |||
+ | 如果使用 ((...)) 作为判断语句,大于和小于可以直接使用 > 和 <。 | ||
+ | |||
+ | if (( a > b )); then | ||
+ | ... | ||
+ | fi | ||
+ | </pre> | ||
+ | =Shell 函数= | ||
+ | <pre> | ||
+ | 函数参数 | ||
+ | 几个特殊字符用来处理参数: | ||
+ | |||
+ | 参数处理 说明 | ||
+ | $# 传递到脚本或函数的参数个数 | ||
+ | $* 以一个单字符串显示所有向脚本传递的参数 | ||
+ | $$ 脚本运行的当前进程ID号 | ||
+ | $! 后台运行的最后一个进程的ID号 | ||
+ | $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。 | ||
+ | $- 显示Shell使用的当前选项,与set命令功能相同。 | ||
+ | $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。 | ||
+ | </pre> | ||
+ | |||
+ | =Shell 输入/输出重定向= | ||
+ | command >> file 2>&1 | ||
+ | command > /dev/null 2>&1 | ||
+ | |||
+ | https://www.runoob.com/linux/linux-shell-io-redirections.html | ||
=see also= | =see also= | ||
第5行: | 第155行: | ||
[https://blog.csdn.net/qq_45019159/article/details/104258887 Linux运维处理及监控脚本] | [https://blog.csdn.net/qq_45019159/article/details/104258887 Linux运维处理及监控脚本] | ||
+ | |||
+ | [https://www.centoschina.cn/course/introduction/9527.html CentOS下查看cpu,内存,硬盘等硬件信息的方法 ] | ||
+ | |||
+ | [https://blog.csdn.net/cd15109139570/article/details/80316911 linux系统中的shell脚本||脚本常用的命令] | ||
[https://blog.csdn.net/weixin_39887925/article/details/116693709 linux运维脚本开发工具,送给运维人员的8个编写脚本的建议] | [https://blog.csdn.net/weixin_39887925/article/details/116693709 linux运维脚本开发工具,送给运维人员的8个编写脚本的建议] |
2024年4月30日 (二) 14:26的最新版本
目录
GNU Bash manual
https://www.gnu.org/software/bash/manual/
IF条件判断,判断条件
1 字符串判断 str1 = str2 当两个串有相同内容、长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空) -z str1 当串的长度为0时为真(空串) str1 当串str1为非空时为真 2 数字的判断 int1 -eq int2 两数相等为真 int1 -ne int2 两数不等为真 int1 -gt int2 int1大于int2为真 int1 -ge int2 int1大于等于int2为真 int1 -lt int2 int1小于int2为真 int1 -le int2 int1小于等于int2为真 3 文件的判断 -r file 用户可读为真 -w file 用户可写为真 -x file 用户可执行为真 -f file 文件为正规文件为真 -d file 文件为目录为真 -c file 文件为字符特殊文件为真 -b file 文件为块特殊文件为真 -s file 文件大小非0时为真 -t file 当文件描述符(默认为1)指定的设备为终端时为真 3 复杂逻辑判断 -a 与 -o 或 ! 非
避免shell脚本被同时运行多次
利用$0 排除自身
#!/bin/bash root@evan-ThinkPad-Edge:~# cat /home/evan/tunnel CPID=$$ #$$ 是脚本运行的当前进程ID号 RUN="`ps -ef | grep "$0" | grep -v "grep" | grep -v "$CPID" | wc -l`" #$0 是脚本本身的名字 [ "$RUN" -gt 1 ] && exit ssh -i /home/evan/.ssh/id_rsa -p 22 -qtfnN -D 7070 [email protected]
lockfile
LOCK_FILE="/tmp/my.lock" if [[ -e $LOCK_FILE ]] ; then echo "re-entry, exiting" exit 1 fi ### Placing lock file touch $LOCK_FILE 你要写的代码 ### Removing lock rm -f $LOCK_FILE
shell脚本特殊符号
MySQL backup shell script
https://fedingo.com/shell-script-to-backup-mysql-database/
https://www.fosstechnix.com/shell-script-for-mysql-database-backup/
https://gist.github.com/lon-io/9067f746cd8cce10af4c52179c7e9b7e
https://orahow.com/linux-shell-script-for-mysql-database-backup/
shell规范
Shell 基本运算符
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。
例如,两个数相加(注意使用的是反引号 ` 而不是单引号 ') Shell 基本运算符
Shell 流程控制
if else 的 [...] 判断语句中大于使用 -gt,小于使用 -lt。 if [ "$a" -gt "$b" ]; then ... fi 如果使用 ((...)) 作为判断语句,大于和小于可以直接使用 > 和 <。 if (( a > b )); then ... fi
Shell 函数
函数参数 几个特殊字符用来处理参数: 参数处理 说明 $# 传递到脚本或函数的参数个数 $* 以一个单字符串显示所有向脚本传递的参数 $$ 脚本运行的当前进程ID号 $! 后台运行的最后一个进程的ID号 $@ 与$*相同,但是使用时加引号,并在引号中返回每个参数。 $- 显示Shell使用的当前选项,与set命令功能相同。 $? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误。
Shell 输入/输出重定向
command >> file 2>&1 command > /dev/null 2>&1
https://www.runoob.com/linux/linux-shell-io-redirections.html