linux:bash

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
linux:bash [2022/08/10 03:13] – [Linux.Bash] adminlinux:bash [2024/02/14 06:45] (current) – [Linux.Bash] admin
Line 8: Line 8:
 setopt histreduceblanks setopt histreduceblanks
  
 +</code>
 +
 +<code BASH>
 +# check xterm color
 +msgcat 
 +</code>
 +++++ Пример|
 +{{:linux:linux:bash:pasted:20240214-064440.png}}
 +++++
 +<code BASH>
 +# grep for configuration without comment line
 +egrep -v "^$|^[[:space:]]*#" /etc/postfix/main.cf
 </code> </code>
 bash bash
Line 48: Line 60:
 <code BASH> <code BASH>
 http -hdo ./body httpbin.org/get 2>&1 http -hdo ./body httpbin.org/get 2>&1
 +
  
 # выделение и формирование объекта # выделение и формирование объекта
Line 54: Line 67:
 jq -r '.' | grep 1 jq -r '.' | grep 1
  
-# рекурсивно выделить в json объект в имени которого есть "Mounts" +# 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(. != {})' 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}'
 +</code>
 +
 +image magic
 +<code BASH>
 +create collage from files 
 +"C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\magick.exe" montage *.jpg c:\tmp\montage.jpg 
 </code> </code>
  
Line 119: Line 147:
 - BBCP - 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
 +
 +<code BASH>
 +
 +#!/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
 +</code>
  • linux/bash.1660101226.txt.gz
  • Last modified: 2022/08/10 03:13
  • by admin