Рубрики
Bash Linux

Bash

Shortcuts:
Ctrl+A: to the beginning of the line (like Home)
Ctrl+C: cancel the program
Ctrl+E: to the end of the line (like End)
Ctrl+K: Delete a string from a where cursor located now to the end of the string.
Ctrl+Y: paste just deleted text
Ctrl+Z: stop a program (stop PID). can be restored with fg PID or bg PID

Alt+B: move the cursor one word backward
Alt+F: move the cursor one word forward
Alt+D: delete the word, the cursor must be before the word
Alt+T: switch two previous words
Crtl+T: switch two previous strings

####################################################

Vi:
i — insert mode
i i — replace mode
Esc — exit from edit mode

in view mode:
H — move the cursor to the right
L — move the cursor to the left
J — move the cursor one string down
K — move the cursor one string up
X — Delete a symbol
D D — delete a string
P — paste deleted string
U — undo all
: — switch to command mode

####################################################

list file in the directory:
ls
-t : by time
-S : by size
-X : by extension
-r : reverse
-l : more info, like owner etc.
-h : human-readable size
-R : recursively with sub-catalogs
-F : show file type
/ — catalog
* — executable
@ — symlink
— — symbol device
+ — block device
= — Pipe, FIFO

combine ls and less to list though long output:
ls -lR | less

####################################################

While true loops:

This script makes log rotate to execute every 20 minutes and writes status to file.
while true;
do sleep 1200;
logrotate -s /var/log/logstatus /etc/logrotate.conf;
done

jq — makes pretty print for json. («.» after is mandatory for filtering, without «.« the file will not be written)
while true;
do sleep 1;
curl -v -u login:passwd http://ip:port/api/nodes | jq . >> /home/user/curl.20200512.log;
done

To delete all commented lines from file use next script:
./stripcomments.sh file.name
#!/bin/sh
grep -v ^[[:space:]]*\# $1 | grep -v ^[[:space:]]*\; | grep -v ^$

Copy all files in catalog with «.bak» extensions to «.bak~»
for i in *.bak;
do cp $i $i~;
done

Save logs for all pods without sidecars in k8s default namespace to separate files:
now=$(date "+%Y%d%m-%H%M"); for item in $(kubectl get pods | awk '{print $1}'); do kubectl logs ${item} > ${item}.$now.logs; done;

More advanced script for logs from k8s:

#!/bin/bash now=$(date "+%Y%d%m-%H%M");
mkdir "${now}_logs";
for item in $(kubectl get pods | awk '{print $1}')
do if [[ ${item} = NAME ]] || [[ ${item} = *"cpro"* ]] || [[ ${item} = *"snmp-webhook"* ]] || [[ ${item} = *"reloader"* ]] || [[ ${item} = *"coap-adapter"* ]]
then
echo "Pod ${item} has multiply containers or there is some error or it may be filtered!" else
kubectl logs "${item}" > "${now}_logs/${item}.${now}.logs" fi
done
tar -zcvf "logs-${now}.tar.gz" "${now}_logs" echo echo echo "Done!"

Copy SSH identity without password prompt and fingerprint confirmation:
sshpass -f password.txt ssh-copy-id 10.241.45.73 -o StrictHostKeyChecking=no;

Change user password in one string:
echo -e "userpassword\nuserpassword" | passwd username

Create a file in each directory and all subdirectories recursively:
find . -type d -exec touch .gitkeep {} \;

Execute command in each subdirectory:
find . -type d -print0 | xargs -0 -L1 sh -c 'cd "$0" && pwd && ./packer.sh unpack'

xargs flags:
-0 — input is terminated (split) by end of the line and not with white space.
-L1 — max 1 not blank line per argument

Generate ssh key in one string to the default file:
ssh-keygen -q -t rsa -N '' -f ~/.ssh/id_rsa <<<y 2>&1 >/dev/null

Turn off swap and comment it in fstab:
swapoff -a
sed -i 's/^\/swap/#\/swap/' /etc/fstab

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

Этот сайт использует Akismet для борьбы со спамом. Узнайте, как обрабатываются ваши данные комментариев.