===== Linux.Bash =====
PS bash/zsh ${SSH_CLIENT+ssh:}${(%):-%m}
# see to set options https://github.com/ricbra/zsh-config/blob/master/zshrc
setopt no_share_history
setopt histignorespace
setopt histignorealldups
setopt histreduceblanks
# check xterm color
msgcat
++++ Пример|
{{:linux:linux:bash:pasted:20240214-064440.png}}
++++
# grep for configuration without comment line
egrep -v "^$|^[[:space:]]*#" /etc/postfix/main.cf
bash
* [[https://linuxize.com/post/bash-functions/|bash-functions]]
# to save some commmand
savecmd () {
echo "$1" >> /tmp/$(date +%F).txt
}
# using
savecmd "ansible proxmox -m apt -a "name=qemu-guest-agent state=present" --become"
Jshon - bash json parser - http://kmkeen.com/jshon/
# Example пример создания - ниже конструирования объектов
jshon -n {}
-n {}
-n {}
-n {}
-s "ssh" -i ansible_connection
-s "ubuntu" -i ansible_user
-i "10.59.4.11"
-i "hostvars"
-i "_meta"
-n {}
-n []
-s "10.59.4.11" -i 0
-i "hosts"
-i dockers
# результат в виде запускаемой строки
jshon -n {} -n {} -n {} -n {} -s "ssh" -i ansible_connection -s "ubuntu" -i ansible_user -i "10.59.4.11" -i "hostvars" -i "_meta" -n {} -n [] -s "10.59.4.11" -i 0 -i "hosts" -i dockers
http -hdo ./body httpbin.org/get 2>&1
# выделение и формирование объекта
http https://github.com/ | jq '.[]| select(.number=="059-07-07" or .number=="059-03-08")| {requestId: .requestId, number: .number}'
# jq grep
jq -r '.' | grep 1
# JQ https://gist.github.com/pedroxs/f0ee8c515eea0dbce2e23eea7c048e10
# рекурсивно выделить в json объект в имени которого есть "Mounts" - find key on name and get object
jq '.. | objects | with_entries(select(.key | contains("Mounts"))) | select(. != {})'
# same, but output propper array
jq '[ .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) ]'
# or
jq 'map( .. | objects | with_entries(select(.key | contains("ftp"))) | select(. != {}) )'
# transform input from {type: a, amount: 1} to {a: 1} and sum all values by type
jq '[ .[] | {(.type): .amount} ] | map(to_entries) | add | group_by(.key) | map({key: .[0].key, value: map(.value) | add}) | from_entries'
# invert selection of contains key name
journalctl -f -o json | jq -cr 'select(._SYSTEMD_UNIT=="monitorinflux.service"| not ) | {unit:._SYSTEMD_UNIT, mess:.MESSAGE}'
image magic
create collage from files
"C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe" montage *.jpg c:\tmp\montage.jpg
===== Linux.Admin =====
# users
$ps auxww
#all full command line
$ps ewwf
# Get process using swap
$ (echo "COMM:PID:SWAP"; for file in /proc/**/status ; do awk '/^Pid|VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r ) | column -t | head -50
# Asing heredoc to variable Use $() to assign the output of cat to your variable like this:
VAR=$(cat <<'END_HEREDOC'
test 'test"
$(dont-execute-this)
test"test"''
END_HEREDOC
)
# this will echo variable with new lines intact
echo "$VAR"
# this will echo variable without new lines (changed to space character)
echo $VAR
Making sure to delimit starting END_HEREDOC with single-quotes.
# monitor directory
watch -n 5 'echo $(date) $(du -d 1 /media/synology-esb-smb/uismv-db-backups/full-backups/temp/)'
# SWAP INFO
alias swapi=$(cat <<'END_HEREDOC'
( echo "COMM:PID:SWAP"; for file in /proc/**/status ; do awk '/^Pid|VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep kB | grep -wv "0 kB" | sort -k 3 -n -r ) | column -t | head -50
END_HEREDOC
)
# find KVM and name of kvm names
ps axf | grep -oE "(64353|930818|182162|239596|804199|692833|64644|309148|126991).+debug" | grep -Eo " guest=\S+"
# Grep exclude comments and blank lines
grep -v '^\s*$\|^\s*\#' temp
===== Linux move files =====
https://princetonuniversity.github.io/PUbootcamp/sessions/data-transfer-basics/PUBootCamp_20181031_DataTransfer.pdf
- scp
- ftp
- rsync
- GridFTP
- BBCP
===== Scripting =====
* scripting pitfails https://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file
===== Scripting-code =====
* read line by line - https://phoenixnap.com/kb/bash-read-file-line-by-line
#!/bin/bash
# Declare variables
vm_name=""
vm_path=""
skipped_lines=""
current_line_number=1
# Read the file line by line
while IFS=';' read -r vm_name vm_path; do
# Check if the line has the correct format
if [[ -z "$vm_name" ]] || [[ -z "$vm_path" ]]; then
skipped_lines="${skipped_lines}Line ${current_line_number}: ${line}\n"
fi
# Process the variables here if the line has the correct format
if [[ -n "$vm_name" ]] && [[ -n "$vm_path" ]]; then
echo "VM name: $vm_name"
echo "VM path: $vm_path"
fi
# Increment the line number
current_line_number=$((current_line_number+1))
done < file.txt
# Print the skipped lines
if [[ -n "$skipped_lines" ]]; then
echo "The following lines were skipped because they did not have the correct format:"
echo "$skipped_lines"
fi