#!/bin/bash
set -euo pipefail
log() { echo "$*" >&2; }
fatal() { log "error: $*"; exit 1; }
run() { local c; c="$(printf ' %q' "$@")"; log "+$c"; "$@" || fatal "command failed ($?):$c"; }
main() {
local here
here="$(cd "$(dirname "$0")"; pwd)"
}
main "$@"
#!/bin/bash
is probably more portable than#!/usr/bin/bash
set -u
: error on use of unset variableset -e
: error on uncaught command errorset -o pipefail
: error on uncaught command error in a pipeline chain
tmpdir="$(mktemp -d -t myscript.XXXXXX)"
# shellcheck disable=SC2064
trap "$(printf 'rm -rf -- %q ||:' "$tmpdir")" EXIT
command | ts '[%y-%m-%d %H:%M:%.S]'
date +%y%m%d-%H%M%S
iso 8601:
date -u +%Y%m%dT%H%M%SZ # UTC, short
date -u +%Y-%m-%dT%H:%M:%SZ # UTC, long
date +%Y%m%dT%H%M%S%z # NON-UTC, short
date +%Y-%m-%dT%H:%M:%S%:z # NON-UTC, long
set -euo pipefail
xdg_dir() {
local value
value="$(eval echo "\${XDG_${1}_DIR:-}")"
[[ -z "$value" ]] || { echo "$value"; return 0; }
value="$(
# See /usr/bin/xdg-user-dir
# shellcheck source=/dev/null
test -f "${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs" && . "${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs"
eval echo "\${XDG_${1}_DIR:-}"
)"
[[ -z "$value" ]] || { echo "$value"; return 0; }
echo "$2"
}
XDG_DOWNLOAD_DIR="$(xdg_dir DOWNLOAD "$HOME/Downloads")"
XDG_RUNTIME_DIR="$(xdg_dir RUNTIME "/run/user/$UID")"
XDG_CONFIG_DIR="$(xdg_dir CONFIG "$HOME/.config")"
declare | grep '^XDG_'
# Hard-format, zero first 40MiB
dd bs=4M count=10 conv=sync,fsync status=progress if=/dev/zero of=/dev/disk/by-id/usb-...
partprobe
# Create partiion
fdisk /dev/disk/by-id/usb-...
# type 0c "0c W95 FAT32 (LBA)" for classic USB FAT32
partprobe
# Create FAT32 FS
mkfs.vfat -F 32 -n PARTLABEL /dev/disk/by-id/usb-...-part1
# Burn iso on usb
dd bs=4M conv=sync,fsync status=progress if=...iso of=/dev/disk/by-id/usb-...
man perlrun
# Perl substitute but with perl code eval
# Substitute all matches in text and print all new text
perl -pe 's/[0-9]+/ sprintf("%04d", $& + 42) /eg'
# Print only matches, substituted
perl -ne 's/^ foo (.*?) bar (.*?)$/ print $1."\/".$2."\n"; /e'
# Readable regex code and commants: /x ignores non-backslashed whitespaces, and allow '#' comments.
perl -pe 's/
# This is a comment, not part of regex.
# The following is part of the regex match:
[a-z0-9]+
# Whitespaces and newlines match nothing, and must be matched explicitly
\ + \s+ [ \t]+
/repl/x'
# Matching over multi-line input
# * -0777 : slurps the whole file (777 is a convention)
# * //m : /^/ and /$/ match begin and end of each lines in input, instead of begin and end of whole input
# * //s : /./ also match newlines (note: /\s/ always match newlines, /\s+$/ match trailing spaces)
perl -0777 -pe 's///gms'
tar --owner=0 --group=0 --no-same-owner --no-same-permissions -caf tar.tar.xz -C dir file ...
# QoS
sacctmgr show qos | less
# Reservations
sacctmgr show reservation
lsof /path
lsof -p pid,pid
# List open file for all "zsh" processes
lsof -c zsh
# List network connections
lsof -i
# -*- coding: utf-8 -*-
gcc -dM -E -x c /dev/null
g++ -dM -E -x c++ /dev/null
diff <(gcc -Q --help=target) <(gcc -Q -march=native --help=target)
diff <(gcc -Q -O0 --help=optimizers) <(gcc -Q -O3 --help=optimizers)
checkboxes: