meta data for this page
Szkriptelés
Gyors uniq awk-val
awk '!x[$0]++'
Fájlba beszúrás minta után, ha még nincs
sed -i '/^EZENSTRINGUTAN /{N; /\nBEILLESZTENDO$/b; s/\n/\nBEILLESZTENDO\n/}' /etc/resolv.conf
Futási logok archiválása havi lebontású .zip-be, és az 1 hónapnál régebbiek törlése
zip -9rv ${LOG}/$(date +%Y%m)_archived_backup_logs.zip ${LOG}/${LOGFILE##*/} find ${LOG}/*.log -type f -mtime +60 -exec rm {} \;
Háttérben futunk-e?
case $(ps -o stat= -p $$) in *+*) echo "Running in foreground" ;; *) echo "Running in background" ;; esac
crontab-ból futunk-e?
CRON=$(pstree -s $$ | grep -q cron && echo true || echo false)
nohup-pal futunk-e?
if egrep -q "SigIgn:\s.{15}[13579bdf]" /proc/$$/status ; then
echo "Ignores SIGHUP (runing via nohup)"
else
echo "Doesn't ignore SIGHUP (NOT running via nohup)"
fi
Fájl rendezése több soros csoportosításban
Pl: 4 oszlopos bemeneti fájl 3 soros csoportokban. A 3 sort egymás után rakjuk, lerendezzük az 5. oszlopra, majd újra eltördeljük
paste - - - < input.file | sort -k5 | xargs -n4
Szkript kimeneteinek automatikus logolása fájlba
A_CURR_SHELL=$(ps -o args= -p "$$" | sed 's/ .*$//' | sed 's#^.*\/##g') if [[ "$A_CURR_SHELL" == "bash" ]]; then exec > >(tee -ia $A_TMP_FILE) exec 2> >(tee -ia $A_TMP_FILE >&2) elif [[ "$A_CURR_SHELL" == "ksh" ]]; then mkfifo /tmp/$$-out-fifo ( exec tee -ia $A_TMP_FILE < /tmp/$$-out-fifo >&2 ) & exec 1>/tmp/$$-out-fifo exec 2>/tmp/$$-out-fifo rm -f /tmp/$$-out-fifo fi
Ismétlődő sorok számolása
cat file | sed 's/([^(]*)$/,&/' | awk -F'[,]' '{a[$2]++}END{for(k in a) print $1,k,$3,"("a[k]"x)"}'
User input default értékkel
DEFAULT="Semmit" read -e -p "Mit akarsz?" -i "$DEFAULT" VALTOZO VALTOZO="${VALTOZO:-$DEFAULT}"
Terminálról olvasás ciklusban
read input </dev/tty
/dev/stdin üres-e? (Van-e adat a pipe-ban?)
[ ! -t 0 ]
/dev/stdout -ja van-e a processznek? (Terminálban fut-e?)
Bekezdésre greppelés Linux alatt (AIX: grep -p)
sed -n '/Storage Slots/{:a;N;/\n$/!ba;p}' awk 'BEGIN{IGNORECASE=1;RS=ORS="\n\n";FS=OFS="\n"}/Storage Slots/' perl -00 -ne 'print if /Storage Slots/'
Bekezdésre exclude-olása Linux alatt
awk -v RS= -v ORS='\n\n' 'NR==FNR{a[$0]++;next}!($0 in a)' file2 file1
Ha az SSH kilép aciklusból az első futás után
ssh -n ...
Sztringek hexa értelmezésének kiküszöbölése
Pl:
$((10#`date +%H`))
A grep "-o" kapcsolójának kiváltása perl-lel
perl -lne '/reguláris kifejezés/ && print $&'
Processz környezeti változóinak listázása
xargs -0 -L1 -a /proc/3242/environ
BASH változó manipulációk
Syntax | Meaning |
---|---|
${#var} | String length (number of characters in $var). |
${var,} | Lowercase first alphanumeric character in $var. |
${var^} | Uppercase first alphanumeric character in $var. |
${var^^} | Uppercase alphanumeric characters in $var. |
${var^^[p,g]} | Uppercase alphanumeric characters in $var matching p or g. |
${parameter?err_msg},${parameter:?err_msg} | If parameter set, use it, else print err_msg and abort the script with an exit status of 1. Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null. |
${parameter-default},${parameter:-default} | If parameter not set, use default. ${parameter-default} and ${parameter:-default} are almost equivalent. The extra : makes a difference only when parameter has been declared, but is null. |
${parameter=default},${parameter:=default} | If parameter not set, set it to default. Both forms nearly equivalent. The : makes a difference only when $parameter has been declared and is null. |
${parameter+alt_value},${parameter:+alt_value} | If parameter set, use alt_value, else use null string. Both forms nearly equivalent. The : makes a difference only when parameter has been declared and is null. |
${var#Pattern} | Remove from $var the shortest part of $Pattern that matches the front end of $var. |
${var##Pattern} | Remove from $var the longest part of $Pattern that matches the front end of $var. |
${var%Pattern} | Remove from $var the shortest part of $Pattern that matches the back end of $var. |
${varPattern} | Remove from $var the longest part of $Pattern that matches the back end of $var. |
${var:pos} | Variable var expanded, starting from offset pos. |
${var:pos:len} | Expansion to a max of len characters of variable var, from offset pos. |
${var/Pattern/Replacement} | First match of Pattern, within var replaced with Replacement. If Replacement is omitted, then the first match of Pattern is replaced by nothing, that is, deleted. |
${var//Pattern/Replacement} | Global replacement. All matches of Pattern, within var replaced with Replacement. If Replacement is omitted, then all occurrences of Pattern are replaced by nothing, that is, deleted. |
${var/#Pattern/Replacement} | If prefix of var matches Pattern, then substitute Replacement for Pattern. |
${var/%Pattern/Replacement} | If suffix of var matches Pattern, then substitute Replacement for Pattern. |
${!varprefix*},${!varprefix@} | Matches names of all previously declared variables beginning with varprefix. |
forrás: http://tldp.org/LDP/abs/html/parameter-substitution.html
Gyakori BASH RC kódok
Exit Code Number | Meaning | Example | Comments |
---|---|---|---|
1 | Catchall for general errors | let „var1 = 1/0” | Miscellaneous errors, such as „divide by zero” and other impermissible operations |
2 | Misuse of shell builtins (according to Bash documentation) | empty_function() {} | Missing keyword or command |
126 | Command invoked cannot execute | /dev/null | Permission problem or command is not an executable |
127 | „command not found” | illegal_command | Possible problem with $PATH or a typo |
128 | Invalid argument to exit | exit 3.14159 | exit takes only integer args in the range 0 - 255 (see first footnote) |
128+n | Fatal error signal „n” | kill -9 $PPID of script | $? returns 137 (128 + 9) |
130 | Script terminated by Control-C | Ctl-C | Control-C is fatal error signal 2, (130 = 128 + 2, see above) |
255* | Exit status out of range | exit -1 | exit takes only integer args in the range 0 - 255 |
Színezés
ANSI2HTML https://github.com/pixelb/scripts/blob/master/scripts/ansi2html.sh Színkódok https://misc.flogisoft.com/bash/tip_colors_and_formatting
Hiba csatorna színezése:
$ parancs 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done)
Bash színkódok törlése szövegből:
sed -r "s/\x1B(\[[0-9;]*[JKmsu]|\(B)//g"
Színkódok és sorvég karakterek kivágása szövegből: A ^M-et így: CTRL+V, CTRL+M !!
sed -i -e 's/\x1b\[[0-9;]*m//g' -e 's/^M//g' file
Váltott soroz színezés:
| awk '{if (NR%2 != 1) {print "\033[48;5;235m" $0 "\033[0m"; next;} {print "\033[38;5;239m" $0 "\033[0m";}}'
Dátumos prompt képernyőmentéses dokumentáláshoz
export PS1='[\u@\h \W] \D{%F %T}\n\$ '
Terminál doboz rajzolás
env printf '\u2502'
Karakterek: https://en.wikipedia.org/wiki/Box-drawing_character
Terminál kurzor pozícionálás
- Position the Cursor:
\033[<L>;<C>H Or \033[<L>;<C>f puts the cursor at line L and column C.
- Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
- Clear the screen, move to (0,0):
\033[2J
- Erase to end of line:
\033[K
- Save cursor position:
\033[s
- Restore cursor position:
\033[u
"Homokóra" - progress ikon
v1:
a=1 sp="/-\|" echo -n ' ' while true do printf "\b${sp:a++%${#sp}:1}" done
v2:
spinner() { local pid=$1 local delay=0.75 local spinstr='|/-\' while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do local temp=${spinstr#?} printf " [%c] " "$spinstr" local spinstr=$temp${spinstr%"$temp"} sleep $delay printf "\b\b\b\b\b\b" done printf " \b\b\b\b" }