BASH & KSH tips & tricks

Handle variables

indirection expansion

var=EDITOR

echo ${!var}

Handle conditions

continue / break / return

break a while or case loop

break

skip to the next iteration of for, while, until or select loop

continue

exit a function and not the whole script

return

commands within an error

false || { echo first command; echo second command; }

Text processing

for vs. while read

https://stackoverflow.com/questions/14040911/reading-a-file-line-by-line-in-ksh

https://www.cyberciti.biz/faq/ksh-read-file/

Other tips & tricks

unix2date

GNU

date -d @timestamp

BSD/OSX

date -r timestamp

press any key…

https://stackoverflow.com/questions/43030991/korn-shell-how-can-i-make-press-any-key-to-continue

echo

print tabs and new lines

echo -e foo\\tbar\\nnewline

write to standard error

echo 'There were errors' >&2

Deal with floats

#typeset -F 2 var

float var

(( var = ... ))

Resources

float

https://www.informit.com/articles/article.aspx?p=99035 –> THIS

https://unix.stackexchange.com/questions/369788/how-to-compare-two-float-values-in-ksh

https://stackoverflow.com/questions/11481648/arithmetic-syntax-error-in-shell-script

https://askubuntu.com/questions/1309320/ksh-syntax-error-invalid-arithmetic-operator-error-token-is-111111111-2-555

https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894802

[fw] https://docstore.mik.ua/orelly/unix3/korn/ch06_05.htm

https://unix.stackexchange.com/questions/40786/how-to-do-integer-float-calculations-in-bash-or-other-languages-frameworks

https://stackoverflow.com/questions/11084675/equivalent-for-typeset-l-and-typeset-u-in-linux-bash

date

https://stackoverflow.com/questions/61584427/how-to-convert-unix-timestamp-to-date-and-time-in-windows

moar

https://stackoverflow.com/questions/32774586/get-substring-of-a-string-in-korn-shell

substitutions

https://stackoverflow.com/questions/27501410/ksh-nested-substitution

https://softpanorama.org/Scripting/Shellorama/String_operations/ksh_substitutions.shtml


https://stackoverflow.com/questions/12000949/scope-of-variables-in-ksh

https://stackoverflow.com/questions/1027869/is-there-any-difference-between-using-typeset-in-ksh-to-simply-setting-a-variabl ==> local vars with typeset


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | HTML5 | CONTACT
Copyright © 2024 Pierre-Philipp Braun