sh #unix #bash #cheatsheet
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh #unix #bash #cheatsheet相关的知识,希望对你有一定的参考价值。
# Bash cheatsheet
<!-- TOC -->
- [Bash cheatsheet](#bash-cheatsheet)
- [Helpful links](#helpful-links)
- [Tricks & tips](#tricks--tips)
- [Wait until something works](#wait-until-something-works)
- [tr](#tr)
- [Check udp port](#check-udp-port)
- [Check for argument](#check-for-argument)
- [Cat content to file](#cat-content-to-file)
- [Using journalctl & sytemctl](#using-journalctl--sytemctl)
- [Compress folder while excluding certain files](#compress-folder-while-excluding-certain-files)
- [Datadog troubleshoot](#datadog-troubleshoot)
- [Generate markdown table from @so0k [link](https://gist.github.com/so0k/256cde434cbb0b58702249b31203a357)](#generate-markdown-table-from-so0k-linkhttpsgistgithubcomso0k256cde434cbb0b58702249b31203a357)
- [Ssh through jumphost](#ssh-through-jumphost)
- [Set -e](#set--e)
- [Ssh issue](#ssh-issue)
- [List all processes from pods list](#list-all-processes-from-pods-list)
<!-- /TOC -->
## Helpful links
* tmux cheatsheet [link](https://gist.github.com/MohamedAlaa/2961058)
* bash cheatsheet [link](https://gist.github.com/LeCoupa/122b12050f5fb267e75f)
* Makefile cheatsheet [link](https://gist.github.com/isaacs/62a2d1825d04437c6f08)
* GPG cheatsheet [link](http://irtfweb.ifa.hawaii.edu/~lockhart/gpg/)
* Exit code [link](http://tldp.org/LDP/abs/html/exitcodes.html)
## Tricks & tips
### Wait until something works
```bash
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:]'` : remove all spaces
### Check udp port
```bash
nc -zuv 192.168.0.10 123
```
### Check for argument
```bash
if [ -z "$1" ];then
echo "Usage: ./create-users.sh <user-name>"
exit 0
fi
```
### Cat content to file
```bash
cat > outfile.txt <<EOF
some text
to save
EOF
```
### Using journalctl & sytemctl
```bash
# 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
```bash
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](https://gist.github.com/so0k/256cde434cbb0b58702249b31203a357)
```python
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
```bash
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
```bash
ln -s ../Cellar/openssl@1.1/1.1.0f /usr/local/opt/openssl@1.1
```
### List all processes from pods list
```bash
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
```
# 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 $@
}
#!/bin/bash
if [ -z "$1" ];then
echo "Get your token at: https://honestbee.loggly.com/live_tail/tokens"
echo "Usage: ./loggly-setup.sh <token>"
echo "###"
echo 'Then run this: livetail -m "include query" -i "exclude query"'
exit 0
fi
curl -O 'https://www.loggly.com/install/tailclient-1.0.3-install.zip'
unzip tailclient-1.0.3-install.zip
sed -i.orig "s/#tail.client.authtoken=.*/tail.client.authtoken=$1/" tailclient-1.0.3/conf/livetail.properties
cd tailclient-1.0.3/bin/
mkdir log/
current_dir=`pwd`
ln -s $current_dir/livetail /usr/local/bin/livetail
---
#!/bin/bash
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ] || [ -z "$COMMIT_SHA" ] || [ -z "$DB_IDENTIFIER" ];then
echo "AWS access, private key, commit hash and db identifier are required to continue. Exit..."
exit 1
fi
if [ -z "$AWS_DEFAULT_REGION" ];then
export AWS_DEFAULT_REGION=ap-southeast-1
fi
echo "Creating snapshot $DB_IDENTIFIER-$COMMIT_SHA for RDS $DB_IDENTIFIER ..."
aws rds create-db-snapshot --db-instance-identifier $DB_IDENTIFIER --db-snapshot-identifier $DB_IDENTIFIER-$COMMIT_SHA
以上是关于sh #unix #bash #cheatsheet的主要内容,如果未能解决你的问题,请参考以下文章