22 Matching Annotations
- May 2024
- Dec 2022
-
pythonspeed.com pythonspeed.com
-
For sufficiently simple cases, just running a few commands sequentially, with no subshells, conditional logic, or loops, set -euo pipefail is sufficient (and make sure you use shellcheck -o all).
Advice for when you can use shell scripts
Tags
Annotators
URL
-
- Mar 2021
-
askubuntu.com askubuntu.com
-
you can use "${@:1}" instead of shift, but that requires bash instead of sh in your #! shebang. IMHO your original shift approach is simpler and better
-
- Nov 2020
-
stackoverflow.com stackoverflow.com
-
yell() { echo "$0: $*" >&2; } die() { yell "$*"; exit 111; } try() { "$@" || die "cannot $*"; }
-
-
mywiki.wooledge.org mywiki.wooledge.org
-
However, this construct is not completely equivalent to if ... fi in the general case.
The caveat/mistake here is if you treat it / think that it is equivalent to if a then b else c. That is not the case if b has any chance of failing.
-
-
-
stackoverflow.com stackoverflow.com
-
[[ -z "$a" || -z "$b" ]] && usage
-
- May 2020
-
stackoverflow.com stackoverflow.com
-
I have used this bash one-liner before set -- "${@:1:$(($#-1))}" It sets the argument list to the current argument list, less the last argument.
Analogue of
shift
built-in. Too bad there isn't just apop
built-in.
-
- Apr 2020
-
stackabuse.com stackabuse.com
-
-
www.cyberciti.biz www.cyberciti.biz
-
Apple replaced Bourne Again SHell with Z shell for licensing reasons
-
- Jan 2020
-
wilsonmar.github.io wilsonmar.github.io
-
ps f
this doesn't run on my system. However
ps -f
seems to list processes started in the terminal andps -ef
lists all (?) processes
Tags
Annotators
URL
-
-
www.linuxjournal.com www.linuxjournal.com
- Dec 2019
-
stackoverflow.com stackoverflow.com
-
For those (like me) wondering why is the space needed, man bash has this to say about it: > Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the :- expansion.
-
- Nov 2019
-
devhints.io devhints.io
- Jul 2019
-
mp.weixin.qq.com mp.weixin.qq.com良许Linux4
-
将错误IP放到数组里面判断是否ping失败三次
#!/bin/bash IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2" for IP in $IP_LIST; do NUM=1 while [ $NUM -le 3 ]; do if ping -c 1 $IP > /dev/null; then echo "$IP Ping is successful." break else # echo "$IP Ping is failure $NUM" FAIL_COUNT[$NUM]=$IP let NUM++ fi done if [ ${#FAIL_COUNT[*]} -eq 3 ];then echo "${FAIL_COUNT[1]} Ping is failure!" unset FAIL_COUNT[*] fi done
-
获取随机8位字符串:
方法1: # echo $RANDOM |md5sum |cut -c 1-8 471b94f2 方法2: # openssl rand -base64 4 vg3BEg== 方法3: # cat /proc/sys/kernel/random/uuid |cut -c 1-8 ed9e032c
-
获取随机8位数字:
方法1:
# echo $RANDOM |cksum |cut -c 1-8 23648321 方法2: # openssl rand -base64 4 |cksum |cut -c 1-8 38571131 方法3: # date +%N |cut -c 1-8 69024815
-
注意事项
1)开头加解释器:#!/bin/bash
2)语法缩进,使用四个空格;多加注释说明。
3)命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用。
4)默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作用域。
5)有两个命令能帮助我调试脚本:set -e 遇到执行非0时退出脚本,set-x 打印执行过程。
6)写脚本一定先测试再到生产上。
Tags
Annotators
URL
-
- Oct 2017
-
www.cyberciti.biz www.cyberciti.biz
-
;
This
semicolon
character is key for the whole thing to work.
-
- Feb 2017
-
content.netdevgroup.com content.netdevgroup.com
-
A shell script is a file of executable commands that has been stored in a text file. When the file is run, each command is executed.
The power of BASH!
-