Differences

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

Link to this comparison view

bash [2018/07/23 14:01]
bash [2023/08/03 20:29] (current)
rzr
Line 1: Line 1:
 +===== LEARN =====
 +
 +  * https://devhints.io/bash
 +  * https://github.com/dylanaraps/pure-bash-bible
 +  * https://news.ycombinator.com/item?id=25428621# MinimaL
 +  * https://betterprogramming.pub/24-bashism-to-avoid-for-posix-compliant-shell-scripts-8e7c09e0f49a#
 +  * https://bobcopeland.com/blog/2012/10/goto-in-bash/
 +
 +==== OneLiner ====
 +
 +  * https://github.com/onceupon/Bash-Oneliner
 +
 +  for i in {0..65535} ; do printf "%x\n" $i ; done \
 +  | while read -r hex ; do printf "$hex(%d): \U$hex\n" 0x$hex ; done
 +
 +==== arrays ===
 +
 +<code>
 +list=(
 + "item"
 + "item with space"
 +)
 +
 +for item in "${list[@]}" ; do
 +  echo "$item"
 +done
 +</code>
 +
 +  echo 1 2 3 | { read -a words ; for value in "${words[@]}" ; do echo $value ; done } 
 +  
 +  end_date=($(date --date "$(date +%Y-%m-01) -1 day" "+%d %m %y"))
 +  start_date=(${end_date[*]})
 +  start_date[0]="01"
 +  
 +  echo ${start_date} # 01
 +  echo "${#start_date}" # 2
 +
 +  echo ${start_date[*]} # 01 04 20
 +  echo "${#start_date[*]}" # 3
 +  
 +  echo ${end_date[*]} # 30 04 20
 +  echo "${start_date[@]:0:2}" # 01 04
 +
 +
 +
 +  * http://wiki.bash-hackers.org/syntax/arrays
 +  * https://www.gnu.org/software/bash/manual/html_node/Arrays.html
 +
 +@TaG: ArraY
 +
 +
 +==== bug ? ====
 +
 +<code>
 +$SHELL --version && export LANG=C ; for i in $(seq 1 20) ; do  printf "[%d]\n" 0$i ; done 2>&1 | grep ": invalid"
 +GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
 +Copyright (C) 2007 Free Software Foundation, Inc.
 +bash: printf: 08: invalid number
 +bash: printf: 09: invalid number
 +bash: printf: 018: invalid number
 +bash: printf: 019: invalid number
 +
 +$SHELL --version && export LANG=C ; for i in $(seq 1 20) ; do  printf "[%d]\n" 0$i ; done 2>&1 | grep ": invalid"
 +GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
 +Copyright (C) 2013 Free Software Foundation, Inc.
 +License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
 +
 +This is free software; you are free to change and redistribute it.
 +There is NO WARRANTY, to the extent permitted by law.
 +bash: printf: 08: invalid octal number
 +bash: printf: 09: invalid octal number
 +bash: printf: 018: invalid octal number
 +bash: printf: 019: invalid octal number
 +
 +
 +</code>
 +
 +Fixed in :
 +  GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
 +
 +
 +GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
 +
 +  bash: printf: 08: invalid octal number
 +  bash: printf: 09: invalid octal number
 +
 +  printf "[%04d]\n" 9              
 +  [0009]
 +
 +@TaG: PrintF
 +
 +==== fork bomb ====
 +
 +  :(){:|:&};: # fork bomb 
 +
 +  url=http://view.atdmt.com/action/dlXP ; while [ true ] ;do wget -O /dev/null $url ; sleep 1 ; done
 +
 +
 +
 +==== ${#foo} ====
 +
 +<code>
 +display_string_by_packet()
 +{
 +    res_string=""
 +    packet_len=1
 +    [ "_" != "_${2}" ] && packet_len=$2
 +    if [ 0 -lt ${packet_len} ] ; then
 +        src_string=${1}
 +        pos=${#src_string}
 +        len=${packet_len}
 +        while [ 0 -lt ${pos} ] ; do
 +            pos=$((pos-${packet_len}))
 +            if [ 0 -ge ${pos} ] ; then
 +                len=$((${packet_len}+${pos}))
 +                pos=0
 +            fi
 +            res_string=${src_string:${pos}:${len}}" "${res_string}
 +        done
 +    fi
 +    echo -n ${res_string}
 +}
 +
 +
 +display_string_by_packet "this is a test" 2
 +#| th is i s a te st
 +
 +</code>
 +
 +
 +==== variables ====
 +
 +  echo ${foo:-bar}
 +  bar
 +  
 +=== $@ ===
 +
 +  echo $@ | sort | while read t ; do cat "$t" ; done > "${file}"
 +
 +https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#index-_0040
 +==== getopts ====
 +
 +  * https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html
 +  * https://wiki-dev.bash-hackers.org/howto/getopts_tutorial#
 +
 +
 +==== ops ====
 +
 +  * https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Here-Documents <<<
 +===== env =====
 +
 +<code>
 +# rzr@lap:tmp/ # [126] # mkdir  /tmp/bin/; touch /tmp/bin/script.sh
 +# rzr@lap:tmp/ # [0] #  PATH="/tmp/bin:$PATH" which script.sh
 +# rzr@lap:tmp/ # [1] #  PATH="/tmp/bin:$PATH" script.sh
 +bash: /tmp/bin/script.sh: Permission denied
 +</code>
 +
 +
 +===== MISC =====
 +
 +  * http://wiki.bash-hackers.org/snipplets/start
 +  * https://github.com/vlisivka/bash-modules
 +
 +Interactive Shell
 +
 +        if [ "${-#*i}" != "$-" ]; then 
 +
 +  * http://www.tldp.org/LDP/abs/html/intandnonint.html#IITEST
 +  * http://mywiki.wooledge.org/BashFAQ/002?action=diff&rev2=35&rev1=33
 +  * https://github.com/tdenniston/bish# TooL CompilE
 +  * https://n0where.net/bash-open-tcpudp-sockets# SockeT
 +  * https://github.com/aristocratos/bashtop# CurseS
 +  * https://github.com/petobens/trueline#
 +  * https://betterdev.blog/minimal-safe-bash-script-template/#
 +
 +
 +
 +===== MORE =====
 +
 +@TaG: [[Shell]] [[sh]] [[printf]] DeclarE
 +
 +{{http://www.monsterbashnews.com/bash/Bash2006lobbyzach.jpg}}
  
bash.txt ยท Last modified: 2023/08/03 20:29 by rzr
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki