- Jan 2021
-
www.morethantechnical.com www.morethantechnical.com
-
nice recipe for quickly turning a scanned PDF into a searchable one
-
- Dec 2020
-
jesrui.sdf-eu.org jesrui.sdf-eu.org
- Nov 2020
-
stackoverflow.com stackoverflow.com
-
yell() { echo "$0: $*" >&2; } die() { yell "$*"; exit 111; } try() { "$@" || die "cannot $*"; }
-
If you were to check the return status of every single command, your script would look like this:
Illustrates how much boilerplate set -e saves you from.
Update: Oops, if you read a comment further below, you learn that:
Actually the idiomatic code without
set -e
would be justmake || exit $?
True that.
-
-
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
-
-
www.digitalocean.com www.digitalocean.com
- Aug 2020
-
stackoverflow.com stackoverflow.com
-
Note that the double quotes around "${arr[@]}" are really important. Without them, the for loop will break up the array by substrings separated by any spaces within the strings instead of by whole string elements within the array. ie: if you had declare -a arr=("element 1" "element 2" "element 3"), then for i in ${arr[@]} would mistakenly iterate 6 times since each string becomes 2 substrings separated by the space in the string, whereas for i in "${arr[@]}" would iterate 3 times, correctly, as desired, maintaining each string as a single unit despite having a space in it.
-
-
stackoverflow.com stackoverflow.com
-
Sadly, bash can't handle long options which would be more readable, i.e.:
-
- 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.
-
-
scriptingosx.com scriptingosx.com
-
Changing a user’s default Shell to bash v5
The only way that setting bash v5 works after installing with homebrew
Tags
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
sdiff
Works on mac
-
-
thomask.sdf.org thomask.sdf.org
-
So be careful running editing a bash script that may be currently executing. It could execute an invalid command, or do something very surprising.
Never modify a running bash command as it can execute something surprising
-
-
linux.die.net linux.die.net
-
readonly TUX=penguinpower
Declare a contant in bash
Tags
Annotators
URL
-
-
stackoverflow.com stackoverflow.com
-
function foo { local -n data_ref=$1 echo ${data_ref[a]} ${data_ref[b]} } declare -A data data[a]="Fred Flintstone" data[b]="Barney Rubble" foo data
best way to pass associative arrays as function argument in bash
-
-
www.digitalocean.com www.digitalocean.com
-
How To Create a Sudo User on Ubuntu
Creates a user with 'bash' as shell and creates a home directory
-
-
www.tecmint.com www.tecmint.com
-
list of directories from newsest to oldest
-
-
www.shell-tips.com www.shell-tips.com
-
echo "scale=2; 2/3" | bc .
the right way to do math in bash
Tags
Annotators
URL
-
-
www.gnu.org www.gnu.org
-
break : . continue eval exec exit export readonly return set shift trap unset
special built-ins in bash. reserved words.
Tags
Annotators
URL
-
-
askubuntu.com askubuntu.com
-
(set -f; IFS=:; printf "%s\n" $PATH)
best way to split a string into lines in basg
-
- Apr 2020
-
dev.to dev.to
-
From Bash to Zsh
-
-
apple.stackexchange.com apple.stackexchange.com
-
zsh will be the default login shell for new accounts, and even then, you can select bash instead
-
-
stackabuse.com stackabuse.com
-
-
www.cyberciti.biz www.cyberciti.biz
-
Apple replaced Bourne Again SHell with Z shell for licensing reasons
-
-
stackoverflow.com stackoverflow.com
-
-
One thing to consider is that getting used to this being enabled in your profile may result in some confusion if you run into a situation where your personalized profile configuration isn't applied (rebuilt machine, shell scripts which may run on other machines, etc). There's some benefit to sticking close to defaults. This is definitely a conservative viewpoint, however.
-
-
linuxize.com linuxize.com
-
- 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.freecodecamp.org www.freecodecamp.org
-
It’s worth noting that first line of the script starts with #!. It is a special directive which Unix treats differently.
Term hash tag at top of bash scripts are NOT comments... they are important
-
-
www.linuxjournal.com www.linuxjournal.com
-
rvm.io rvm.io
-
When you execute commands in non login shell like ssh server command or scp file server:~ or sudo(without -i) or su (without -l) it will execute ~/.bashrc
-
open a login shell which sources ~/.bash_profile
-
- Dec 2019
-
askubuntu.com askubuntu.com
-
The point of the .bashrc file is that it sets the shell up to be more convenient for interactive users. Helpful alias, pretty colors, useful prompts, common environment variables, that sort of thing. And some of these conveniences could break non-interactive scripts.
-
-
unix.stackexchange.com unix.stackexchange.com
-
The main benefit I can see to having .bashrc sourced when running a (non-interactive) remote command is that shell functions can be run. However, most of the commands in a typical .bashrc are only relevant in an interactive shell
-
I discovered that remote shells are treated differently. While non-interactive Bash shells don’t normally run ~/.bashrc commands at start-up, a special case is made when the shell is Invoked by remote shell daemon:
-
This has the consequence that if the .bashrc contains any commands that print to standard output, file transfers will fail, e.g, scp fails without error.
-
-
git.savannah.gnu.org git.savannah.gnu.org
-
COMMAND EXECUTE BASHRC -------------------------------- bash -c foo NO bash foo NO foo NO rsh machine ls YES (for rsh, which calls `bash -c') rsh machine foo YES (for shell started by rsh) NO (for foo!) echo ls | bash NO login NO bash YES
-
-
unix.stackexchange.com unix.stackexchange.com
-
If you want happy cow messages when you login change your bash_profile.
-
AFAIK, the right way to enable un-hindered scp is less about which conditional for stdout in your ~/.bashrc script, and more about simply restricting screen output to the ~/.bash_profile script. At least that is how it works for my distro (CentOS.) Edit for clarity: Put only lines in your ~/.bashrc file as required by "all" remote conections
-
-
bugzilla.redhat.com bugzilla.redhat.com
-
So, though normally bash would not run ~/.bashrc for a non-interactive shell, with ssh it does so anyway.
Tags
Annotators
URL
-
-
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
-
- Feb 2018
-
stackoverflow.com stackoverflow.com
-
You do that using backticks: echo World > file.txt
run command from file in bash command and define after > the filename where the rest of the command is.
-
-
userweb.eng.gla.ac.uk userweb.eng.gla.ac.uk
-
Shell Utilities for QC
-
-
www.tldp.org www.tldp.org
-
Process Substitution
Tags
Annotators
URL
-
-
blog.csdn.net blog.csdn.net
-
${FUNCNAME[@]}
常量FUNCNAME,但是有一点区别是,它是一个数组而非字符串,其中数组的第一个元素为当前函数的名称
Tags
Annotators
URL
-
- Nov 2017
-
stackoverflow.com stackoverflow.com
-
while IFS='=' read -r col1 col2 do echo "$col1" echo "$col2" done <testprop.properties
-
-
timmurphy.org timmurphy.org
-
In Bash you quite often need to check to see if a variable has been set or has a value other than an empty string. This can be done using the -n or -z string comparison operators.
Two most useful commands in bash
-
- Oct 2017
-
www.cyberciti.biz www.cyberciti.biz
-
;
This
semicolon
character is key for the whole thing to work.
-
- Sep 2017
-
stackoverflow.com stackoverflow.com
-
Perl script in bash's HereDoc
-
-
clubmate.fi clubmate.fi
-
Associative arrays in bash
Tags
Annotators
URL
-
- Jul 2017
-
bobah.net bobah.net
-
.
This character should be escaped by a backslash. The complete command would then be:
strings $PWD/bin/myapp | egrep '\.gcda$'
-
- 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!
-
- Oct 2016
-
www.unixmantra.com www.unixmantra.com
-
Counting number of lines
可以用来统计代码行
-
-print0
find -print0 经常和 xargs -0 配合使用,处理文件名中换行符这种特殊情况
-
- Feb 2014
-
unix.stackexchange.com unix.stackexchange.com
-
What is missing is a space between the $( and the following (, to avoid the arithmetic expression syntax. The section on command substitution in the shell command language specification actually warns for that:
This is a very good example of why shell scripting does not scale from simple scripts to large projects. This is not the only place where changes in whitespace can lead to scripts that are very difficult to debug. A well-meaning and experienced programmer from another language, but new to bash scripting, might decide to clean up formatting to make it more consistent-- a laudable goal, but one which can lead to unintentional semantic changes to the program.
Flat, short bash scripts are extremely useful tools that I still employ regularly, but once they begin creeping in size and complexity it's time to switch to another language to handle that-- I think that is what (rightly) has driven things likes Python, Puppet, Ansible, Chef, etc.
Despite the syntactic horrors lurking in shell scripts there is still a beautiful simplicity that drives their use which is a testament to the core unix philosophy.
-