Skip to content

Instantly share code, notes, and snippets.

@zh4n7wm
Last active March 10, 2020 03:51
Show Gist options
  • Select an option

  • Save zh4n7wm/cb5156f8465c0b4f5bdeb9947c4903fd to your computer and use it in GitHub Desktop.

Select an option

Save zh4n7wm/cb5156f8465c0b4f5bdeb9947c4903fd to your computer and use it in GitHub Desktop.
Bash script tips

脚本的当前位置

类似 Python 的 __file__

from: https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"

Using ${BASH_SOURCE[0]} instead of $0 produces the same behavior regardless of whether the script is invoked as <name> or source <name>.

ABSOLUTE_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"

Bash 多进程

xargs

cat ids.txt| sed 's#\(\w\+\)#\1.png#'| xargs -P 40 -n 1 -I {} wget -q -c http://example.com/{} -U 'Mozilla/5.0 (Linux; U; Android 2.1)'

parallel

cat ids.txt| sed 's#\(\w\+\)#\1.png#'| parallel -j4 "wget -q -c http://example.com/{} -U 'Mozilla/5.0 (Linux; U; Android 2.1)'"

还可以通过 FIFO 文件控制进程数量

删除重复的行并保持之前的顺序

awk '!visited[$0]++' your_file > deduplicated_file

ref: How to remove duplicate lines from files preserving their order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment