en:programming:start
Table of Contents
Programming
Bash
root-check
if [ $UID -eq 0 ];then echo "root";fi
online-check
ping -c 1 ${HOST} -W 1 >/dev/null if [ $? == 0 ]; then fi
colors
clr_red=$'\e[1;31m' clr_green=$'\e[1;32m' clr_yellow=$'\e[1;33m' clr_blue=$'\e[1;34m' clr_reset=$'\e[0m'
echo ${clr_red}test${clr_reset}
debug
#!/bin/bash [[ -n "$DEBUG" ]] && set -x echo "test ($DEBUG)" [[ -n "$DEBUG" ]] && set +x
calling this script with DEBUG-variable set switches to xtrace mode (print commands before they are executed)
DEBUG=1 ./test.sh
logging
log single command (only stderr) and view simultaneously (stdout+stderr)
#via Process Substitution (https://www.gnu.org/software/bash/manual/bash.html#Process-Substitution) make 2> >( tee "$logfile" ) ; echo $?
log multiple lines/commands:
exec 3> >(tee logfile) make alpha 2>&3 && make bravo 2>&3 && make charlie 2>&3 && success=1 exec 3>&-
Case
case "$string" in *foo*) #anything ;; [1-6]*) #number-ranges ;; *) #all other ;; esac
stringmanipulation
path / filename
$ VAR=/home/me/mydir/file.c $ DIR=$(dirname "${VAR}") $ echo "${DIR}" /home/me/mydir $ basename "${VAR}" file.c
substrings
a=string b=${a:p:l} #p=Position (0-basiert),l=Länge
replace
orig="AxxBCyyyDEFzzLMN" mod=${orig//[xyz]/_}
Regex-Check
string='My string'; if [[ $string =~ .*My.* ]] then echo "It's there!" fi
calculation
GPIO_NO=$((232+25))
Number-Ranges
ls IMG_20170923_{17..18}*
terminal language
temporary change language (e.g. to get errors in english for forums):
LANG=C
Batch
C/C++
CSS
JavaScript
HTML
PHP
en/programming/start.txt · Last modified: 2024/06/17 09:13 by frank