User Tools

Site Tools



en:programming:start

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:programming:start [2017/12/13 13:42] – [Bash] franken:programming:start [2020/08/29 17:14] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Programming ======
 +http://rogerdudler.github.io/git-guide/
 +
 +===== Bash =====
 +FIXME
 +
 +==== root-check ====
 +<code bash>
 +if [ $UID -eq 0 ];then echo "root";fi
 +</code>
 +==== online-check ====
 +<code bash>
 +  ping -c 1 ${HOST} -W 1 >/dev/null
 +  if [ $? == 0 ];
 +  then
 +  
 +  fi
 +</code>
 +==== colors ====
 +<code bash>
 +clr_red=$'\e[1;31m'
 +clr_green=$'\e[1;32m'
 +clr_yellow=$'\e[1;33m'
 +clr_blue=$'\e[1;34m'
 +clr_reset=$'\e[0m'
 +</code>
 +<code bash>
 + echo ${clr_red}test${clr_reset}
 +</code>
 +
 +==== logging ====
 +log single command (only stderr) and view simultaneously (stdout+stderr)
 +<code bash>
 +#via Process Substitution (https://www.gnu.org/software/bash/manual/bash.html#Process-Substitution)
 +make 2> >( tee "$logfile" ) ; echo $?
 +</code>
 +log multiple lines/commands:
 +<code bash>
 +exec 3> >(tee logfile)
 +
 +make alpha 2>&3 &&
 +make bravo 2>&3 &&
 +make charlie 2>&3 && success=1
 +
 +exec 3>&-
 +</code>
 +
 +==== Case ====
 +
 +  case "$string" in 
 +    *foo*)
 +      #anything
 +    ;;
 +    [1-6]*)
 +      #number-ranges
 +    ;;
 +    *)
 +      #all other
 +    ;;
 +  esac
 +==== stringmanipulation ====
 +In Bash 4:
 +all lowercase
 +
 +  $ echo "${string,,}"
 +
 +all uppercase:
 +
 +  $ echo "${string^^}"
 +
 +[[https://stackoverflow.com/questions/2264428/converting-string-to-lower-case-in-bash|Quelle]]
 +
 +=== path / filename ===
 +
 +<code bash>
 +  $ VAR=/home/me/mydir/file.c
 +  $ DIR=$(dirname "${VAR}")
 +  $ echo "${DIR}"
 +  /home/me/mydir
 +  $ basename "${VAR}"
 +  file.c
 +</code>
 +
 +=== substrings ===
 +
 +  a=string
 +  b=${a:p:l}
 +  #p=Position (0-basiert),l=Länge
 +
 +=== replace ===
 +
 +  orig="AxxBCyyyDEFzzLMN"
 +  mod=${orig//[xyz]/_}
 +
 +=== Regex-Check ===
 +
 +<code bash>
 +string='My string';
 +  
 +if [[ $string =~ .*My.* ]]
 +then
 +  echo "It's there!"
 +fi
 +</code>
 +==== 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 =====