Skip to content

Instantly share code, notes, and snippets.

@tuannvm
Last active August 16, 2023 20:15
Show Gist options
  • Save tuannvm/04487aec37d2056d8a4b4c4b9d53dd16 to your computer and use it in GitHub Desktop.
Save tuannvm/04487aec37d2056d8a4b4c4b9d53dd16 to your computer and use it in GitHub Desktop.
#unix #bash #terminal #cheatsheet

Bash cheatsheet

Helpful links

  • tmux cheatsheet link

  • bash cheatsheet link

  • Makefile cheatsheet link

  • GPG cheatsheet link

  • Exit code link

Kafka

Continuously produce kafka messages

for x in {1..100}; do echo $x; sleep 2;done | kafka-console-producer.sh --bootstrap-server <endpoint> --topic <topic-name>

Tricks & tips

Terminal Movement

See: alacritty/alacritty#93 https://www.tecmint.com/linux-command-line-bash-shortcut-keys/

  • Ctrl + a: move to beginning
  • Ctrl + r: move to end
  • Alt(Esc) + b: move backward 1 word
  • Alt(Esc) + f: move forward 1 word
  • Alt(Esc) + delete: delete backward 1 word
  • Alt(Esc) + d: delete forward 1 word
  • Ctrl + u: delete from cursor to beginning
  • Ctrl + k: delete from cursor to end

Print Custom Format

Reference: https://stackoverflow.com/a/22625150

curl -w "@curl-format.txt" -o /dev/null -s "http://wordpress.com/"

## curl-format.txt
time_namelookup:  %{time_namelookup}s\n
time_connect:  %{time_connect}s\n
time_appconnect:  %{time_appconnect}s\n
time_pretransfer:  %{time_pretransfer}s\n
time_redirect:  %{time_redirect}s\n
time_starttransfer:  %{time_starttransfer}s\n
----------\n
time_total:  %{time_total}s\n

Validate SSL certificate

curl --insecure -v https://google.com 2>&1 | awk 'BEGIN { cert=0 } /^\* Server certificate:/ { cert=1 } /^\*/ { if (cert) print }'

Wait until something works

until ping -c 1 mongo; do
  echo "waiting for DNS mongo..."
  sleep 2
done

tr

tr -s '[[:space:]]' '\n' : replace space with newline td -d '[:space:]' or tr -d " " : remove all spaces

nc

  • Test connection
nc -v -z -w 3 <ip> <port> &> /dev/null && echo "Online" || echo "Offline"

* Check UDP port
```bash
nc -zuv 192.168.0.10 123

Check for argument

  if [ -z "$1" ];then
    echo "Usage: ./create-users.sh <user-name>"
    exit 0
  fi

Cat content to file

cat > outfile.txt <<EOF
some text
to save
EOF

Using journalctl & sytemctl

# display last line
journalctl -b -1 -e

# follow log stream
journalctl -u <service> -f

# restart service
systemctl restart <service>

Compress folder while excluding certain files

tar -zcvf file.tar.gz --exclude=".git/*" --exclude="*.zip" .

Datadog troubleshoot

To display information about the Agent's state with this command.

debian:

docker exec dd-agent service datadog-agent info

alpine:

docker exec dd-agent /opt/datadog-agent/bin/agent info

Generate markdown table from @so0k link

import yaml

print_format="| {parameter:<40}| | {default:<50}|"
def walk_dict(d,keys=[],depth=0):
    for k,v in sorted(d.items(),key=lambda x: x[0]):
        keys.append(k)
        if isinstance(v,dict):
            walk_dict(v,keys,depth+1)
        else:
            print(print_format.format(parameter='`{0}`'.format(".".join(keys)),default='`{0}`'.format(v)))
        keys.pop()

s = open("./values.yaml")
d = yaml.load(s)

walk_dict(d)

Ssh through jumphost

ssh -o ProxyCommand='ssh <jump_host> nc %h %p' user@host

Set -e

set -e stops the execution of a script if a command or pipeline has an error - which is the opposite of the default shell behaviour, which is to ignore errors in scripts. Type help set in a terminal to see the documentation for this built-in command.

Ssh issue

ln -s ../Cellar/openssl@1.1/1.1.0f /usr/local/opt/openssl@1.1

List all processes from pods list

export NODE_NAME=<NODE_NAME> && export IFS=$'\n' && \
for i in $(kubectl get pod --all-namespaces -o wide | grep $NODE_NAME | awk '{print "kubectl exec -it -n "$1" "$2" -- sh -c \"hostname && ps aux\""}'); \
do bash -c "$i";done
#/bin/bash
IFS=$'\n' # make newlines the only separator
if [ -z "$1" ]; then echo "Usage: ./deploy_finder.sh <deployment-name>" && exit;fi
# Get all cluster from kubeconfig except minikube
clusters=`kubectl config get-contexts | tr -s ' ' | cut -d" " -f2 | grep -viE 'NAME|minikube'`
for cluster in $clusters; do
deployments=`kubectl --context $cluster get deployment --all-namespaces -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name | awk "NR==1 || /$1/"`
if [ "$deployments" ];then
echo "Cluster: $cluster"
for deployment in $deployments;do
echo $deployment
done
fi
done
# Get resources
# kget <pod|deployment|svc> <resource-name> <option>
kget() {
kubectl get $@
}
# Change context
# kctx <context-name>
kctx() {
kubectl config use-context $1
}
# Change namespace
# kns <namespace-name>
kns() {
kubectl config set-context $(kubectl config current-context) --namespace=$1
}
# Get current context
kcurrent() {
kubectl config current-context
}
# Delete resources
# kdel <pod|deployment|svc> <resource-name> <option>
kdel() {
kubectl delete $@
}
# Describe resources
# kdes <pod|deployment|svc> <resource-name> <option>
kdes() {
kubectl describe $@
}
# install package
pip install Pygments
# highlight with better color
echo 'alias pygmentize="pygmentize -O style=native"' >> ~/.zshrc
source ~/.zshrc
# sample usage
echo "<html>abc</html>" | pygmentize
# wrap filename with quote
ls | grep -E "[0-9]+.[0-9]" | awk '{print "\""$0"\""}'
@tuannvm
Copy link
Author

tuannvm commented Mar 12, 2021

for i in $(seq 30 10 100);do kubectl scale deployment <DEPLOYMENT-NAME> --replicas=$i && sleep 300;done

@tuannvm
Copy link
Author

tuannvm commented Aug 4, 2021

<C-c> to exit nested vim's insert mode

@tuannvm
Copy link
Author

tuannvm commented Aug 6, 2021

  • c-w-| to have window take over (if using vsplits)
  • c-w-_ to have window take over (if using splits)
  • c-w-= to restore

@tuannvm
Copy link
Author

tuannvm commented Aug 6, 2021

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