{"id":74,"date":"2019-10-19T23:47:08","date_gmt":"2019-10-19T21:47:08","guid":{"rendered":"http:\/\/linuxboxen2.dk\/?page_id=74"},"modified":"2019-10-19T23:47:08","modified_gmt":"2019-10-19T21:47:08","slug":"bash-scripting","status":"publish","type":"page","link":"https:\/\/www.linuxboxen.dk\/?page_id=74","title":{"rendered":"Bash scripting"},"content":{"rendered":"<p><img decoding=\"async\" width=\"471\" height=\"390\" src=\"https:\/\/www.linuxboxen.dk\/wp-content\/uploads\/2021\/08\/png-transparent-bash-shell-script-gnu-bourne-shell-shell-1.png\" alt=\"\" loading=\"lazy\" srcset=\"https:\/\/www.linuxboxen.dk\/wp-content\/uploads\/2021\/08\/png-transparent-bash-shell-script-gnu-bourne-shell-shell-1.png 471w, https:\/\/www.linuxboxen.dk\/wp-content\/uploads\/2021\/08\/png-transparent-bash-shell-script-gnu-bourne-shell-shell-1-300x248.png 300w, https:\/\/www.linuxboxen.dk\/wp-content\/uploads\/2021\/08\/png-transparent-bash-shell-script-gnu-bourne-shell-shell-1-326x270.png 326w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><br \/>\n\t\tHer er nogle eksempler p\u00e5 hvordan man bruger bash mange st\u00e6rke sider. Liste er meget lang og har taget mig en del tid at lave.<\/p>\n<h1>Bash scripting oversigt<\/h1>\n<aside data-js-no-preview=\"\">Eksempler.<\/aside>\n<pre><code>#!\/usr\/bin\/env bash\nNAME=\"John\"\necho \"Hello $NAME!\"\n<\/code><\/pre>\n<h3 id=\"variables\">Variabler<\/h3>\n<pre><code>NAME=\"John\"\necho $NAME\necho \"$NAME\"\necho \"${NAME}!\"\n<\/code><\/pre>\n<h3 id=\"string-quotes\">Strenge i bash<\/h3>\n<pre><code>NAME=\"John\"\necho \"Hej $NAME\"  #=&gt; Hej John\necho 'Hej $NAME'  #=&gt; Hej $NAME\n<\/code><\/pre>\n<h3 id=\"shell-execution\">Shell udf\u00f8relse af kommando<\/h3>\n<pre><code>echo \"I'm in $(pwd)\"\necho \"I'm in `pwd`\"\n# Her betyder $(kommando) og `kommando` det samme.\n<\/code><\/pre>\n<h3 id=\"functions-example\">Betinget udf\u00f8relse<\/h3>\n<pre><code>git commit &amp;&amp; git push\ngit commit || echo \"Commit failed\"\n<\/code><\/pre>\n<h3 id=\"functions-example\">Funktioner<\/h3>\n<pre><code>get_name() {\n  echo \"John\"\n}\necho \"You are $(get_name)\"\n<\/code><\/pre>\n<h3>Betingelser<\/h3>\n<pre><code>if [[ -z \"$string\" ]]; then\n  echo \"String is empty\"\nelif [[ -n \"$string\" ]]; then\n  echo \"String is not empty\"\nfi<\/code><code>\n<\/code><\/pre>\n<h3 id=\"tw-target-text\" dir=\"ltr\" data-placeholder=\"Overs\u00e6ttelse\">Brace ekspansion<\/h3>\n<pre><code>echo {A,B}.js\n<\/code><\/pre>\n<table>\n<tbody>\n<tr>\n<td><code>{A,B}<\/code><\/td>\n<td>Samme som <code>A B<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{A,B}.js<\/code><\/td>\n<td>Samme som <code>A.js B.js<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>{1..5}<\/code><\/td>\n<td>Samme som <code>1 2 3 4 5<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"parameter-expansions\">Parametre expansions<\/h2>\n<h3 id=\"basics\">Basics<\/h3>\n<pre><code>name=\"John\"\necho ${name}\necho ${name\/J\/j}    #=&gt; \"john\" (Erstat tegn)\necho ${name:0:2}    #=&gt; \"Jo\" (Udskriver 2 tegn fra position 0)\necho ${name::2}     #=&gt; \"hn\" (Udskriver de f\u00f8rste 2 tegn)\necho ${name::-1}    #=&gt; \"Joh\"\necho ${name:(-1)}   #=&gt; \"n\" (fra h\u00f8jre)\necho ${name:(-2):1} #=&gt; \"h\" \necho ${food:-Cake}  #=&gt; \"Cake\"\n<\/code><\/pre>\n<pre><code>length=2\necho ${name:0:length}  #=&gt; \"Jo\"\n<\/code><\/pre>\n<p>Se: <a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/pe\">Parameter expansion<\/a><\/p>\n<pre><code>STR=\"\/path\/to\/foo.cpp\"\necho ${STR%.cpp}    # \/path\/to\/foo\necho ${STR%.cpp}.o  # \/path\/to\/foo.o\necho ${STR##*.}     # cpp (extension)\necho ${STR##*\/}     # foo.cpp (basepath)\necho ${STR#*\/}      # path\/to\/foo.cpp\necho ${STR##*\/}     # foo.cpp\necho ${STR\/foo\/bar} # \/path\/to\/bar.cpp\n<\/code><\/pre>\n<pre><code>STR=\"Hello world\"\necho ${STR:6:5}   # \"world\"\necho ${STR:-5:5}  # \"world\"\n<\/code><\/pre>\n<pre><code>SRC=\"\/path\/to\/foo.cpp\"\nBASE=${SRC##*\/}   #=&gt; \"foo.cpp\" (basepath)\nDIR=${SRC%$BASE}  #=&gt; \"\/path\/to\/\" (dirpath)\n<\/code><\/pre>\n<h3 id=\"substitution\">Substitution<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>${FOO%suffix}<\/code><\/td>\n<td>Fjern suffix<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO#prefix}<\/code><\/td>\n<td>Fjern prefix<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>${FOO%%suffix}<\/code><\/td>\n<td>Fjern langt suffix<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO##prefix}<\/code><\/td>\n<td>Fjern langt prefix<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>${FOO\/from\/to}<\/code><\/td>\n<td>Erstat f\u00f8rste match<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO\/\/from\/to}<\/code><\/td>\n<td>Erstat alt<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>${FOO\/%from\/to}<\/code><\/td>\n<td>Erstat suffix<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO\/#from\/to}<\/code><\/td>\n<td>Erstat prefix<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"comments\">Kommentarer<\/h3>\n<pre><code># Enkelt linje kommentar\n<\/code><\/pre>\n<pre><code>: '\nFlere linje\nkommentar\n'\n<\/code><\/pre>\n<h3 id=\"substrings\">Substrings<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>${FOO:0:3}<\/code><\/td>\n<td>Substring <em>(position, l\u00e6ngde)<\/em><\/td>\n<\/tr>\n<tr>\n<td><code>${FOO:-3:3}<\/code><\/td>\n<td>Substring fra h\u00f8jre<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"length\">L\u00e6ngde<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>${#FOO}<\/code><\/td>\n<td>L\u00e6ngte af <code>$FOO<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"default-values\">Default v\u00e6rdi<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>${FOO:-val}<\/code><\/td>\n<td><code>$FOO<\/code>, or <code>val<\/code> Hvis ikke sat<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO:=val}<\/code><\/td>\n<td>Set <code>$FOO<\/code> to <code>val<\/code> Hvis ikke sat<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO:+val}<\/code><\/td>\n<td><code>val<\/code> if <code>$FOO<\/code> Hvis sat<\/td>\n<\/tr>\n<tr>\n<td><code>${FOO:?message}<\/code><\/td>\n<td>Vis fejl medelse og exit hvis <code>$FOO<\/code> ikke er sat<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Tegnet\u00a0 <code>:<\/code> er valgfri (Eks, <code>${FOO=word}<\/code> virker ogs\u00e5)<\/p>\n<h2 id=\"loops\">L\u00f8kker<\/h2>\n<h3 id=\"basic-for-loop\">Basis for l\u00f8kker<\/h3>\n<pre><code>for i in \/etc\/rc.*; do\n  echo $i\ndone\n<\/code><\/pre>\n<h3 id=\"ranges\">Omr\u00e5der<\/h3>\n<pre><code>for i in {1..5}; do\n    echo \"Welcome $i\"\ndone\n<\/code><\/pre>\n<h4 id=\"with-step-size\">L\u00f8kker med step st\u00f8rrelser<\/h4>\n<pre><code>for i in {5..50..5}; do\n    echo \"Welcome $i\"\ndone\n<\/code><\/pre>\n<h3 id=\"reading-lines\">L\u00e6s linjer<\/h3>\n<pre><code>&lt; file.txt | while read line; do\n  echo $line\ndone\n<\/code><\/pre>\n<h3 id=\"forever\">For evigt<\/h3>\n<pre><code>while true; do\n  \u00b7\u00b7\u00b7\ndone\n<\/code><\/pre>\n<h2 id=\"functions\">Funktioner<\/h2>\n<h3 id=\"defining-functions\">Definere en funktion.<\/h3>\n<pre><code>myfunc() {\n    echo \"hello $1\"\n}\n<\/code><\/pre>\n<pre><code># Samme som ovenfor (med alternativ syntax)\nfunction myfunc() {\n    echo \"hello $1\"\n}\n<\/code><\/pre>\n<pre><code>myfunc \n\"John\"\n<\/code><\/pre>\n<h3 id=\"returning-values\">Retur v\u00e6rdier<\/h3>\n<pre><code>myfunc() {\n    local myresult='some value'\n    echo $myresult\n}\n<\/code><\/pre>\n<p>Her bliver result lig med resultatet som returneres fra kaldet af myfunc.<\/p>\n<pre><code>result=\"$(myfunc)\"\n<\/code><\/pre>\n<h3 id=\"raising-errors\">Returnere fejl.<\/h3>\n<pre><code>myfunc() {\n  return 1\n}\n<\/code><\/pre>\n<pre><code>if myfunc; then\n  echo \"success\"\nelse\n  echo \"failure\"\nfi\n<\/code><\/pre>\n<h3 id=\"arguments\">Argumenter<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>$#<\/code><\/td>\n<td>Number af argumenter<\/td>\n<\/tr>\n<tr>\n<td><code>$*<\/code><\/td>\n<td>Alle argumenter<\/td>\n<\/tr>\n<tr>\n<td><code>$@<\/code><\/td>\n<td>Alle argumenter, start fra start<\/td>\n<\/tr>\n<tr>\n<td><code>$1<\/code><\/td>\n<td>F\u00f8rste argument<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>See <a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/shellvars#special_parameters_and_shell_variables\">Special parameters<\/a>.<\/p>\n<h2 id=\"conditionals\">Betingede<\/h2>\n<p>Noter dig venlist af\u00a0 <code>[[<\/code> faktisk et program som returnere enten\u00a0 <code>0<\/code> (true) eller <code>1<\/code> (false) set fra shell fejl kode. Alle programmer har samme logik\u00a0 (s\u00e5 som alle base kommandoer, som eks. <code>grep(1)<\/code> eller <code>ping(1)<\/code>) kan blive brugt, se eksempler.<\/p>\n<table>\n<tbody>\n<tr>\n<td><code>[[ -z STRING ]]<\/code><\/td>\n<td>Tom streng<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -n STRING ]]<\/code><\/td>\n<td>Ikke tom streng<\/td>\n<\/tr>\n<tr>\n<td><code>[[ STRING == STRING ]]<\/code><\/td>\n<td>Ligmed<\/td>\n<\/tr>\n<tr>\n<td><code>[[ STRING != STRING ]]<\/code><\/td>\n<td>Ikke ligmed<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>[[ NUM -eq NUM ]]<\/code><\/td>\n<td>Ligmed<\/td>\n<\/tr>\n<tr>\n<td><code>[[ NUM -ne NUM ]]<\/code><\/td>\n<td>Ikke ligmed<\/td>\n<\/tr>\n<tr>\n<td><code>[[ NUM -lt NUM ]]<\/code><\/td>\n<td>Mindre<\/td>\n<\/tr>\n<tr>\n<td><code>[[ NUM -le NUM ]]<\/code><\/td>\n<td>Mindre eller samme<\/td>\n<\/tr>\n<tr>\n<td><code>[[ NUM -gt NUM ]]<\/code><\/td>\n<td>St\u00f8re en<\/td>\n<\/tr>\n<tr>\n<td><code>[[ NUM -ge NUM ]]<\/code><\/td>\n<td>St\u00f8re eller samme<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>[[ STRING =~ STRING ]]<\/code><\/td>\n<td>Regexp<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>(( NUM &lt; NUM ))<\/code><\/td>\n<td>Numerisk test<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<td><code>[[ -o noclobber ]]<\/code><\/td>\n<td>If OPTIONNAME er aktiv<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>[[ ! EXPR ]]<\/code><\/td>\n<td>Not<\/td>\n<\/tr>\n<tr>\n<td><code>[[ X ]] &amp;&amp; [[ Y ]]<\/code><\/td>\n<td>And<\/td>\n<\/tr>\n<tr>\n<td><code>[[ X ]] || [[ Y ]]<\/code><\/td>\n<td>Or<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"file-conditions\">Fil betingelser<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>[[ -e FILE ]]<\/code><\/td>\n<td>Findes<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -r FILE ]]<\/code><\/td>\n<td>Kan l\u00e6ses<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -h FILE ]]<\/code><\/td>\n<td>Symlink fil<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -d FILE ]]<\/code><\/td>\n<td>Bibliotek p\u00e5 disk<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -w FILE ]]<\/code><\/td>\n<td>Skrivebar<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -s FILE ]]<\/code><\/td>\n<td>St\u00f8rrelse er &gt; 0 bytes<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -f FILE ]]<\/code><\/td>\n<td>En Fil<\/td>\n<\/tr>\n<tr>\n<td><code>[[ -x FILE ]]<\/code><\/td>\n<td>Kan startes<\/td>\n<\/tr>\n<\/tbody>\n<tbody>\n<tr>\n<td><code>[[ FILE1 -nt FILE2 ]]<\/code><\/td>\n<td>1 er nyere en fil 2<\/td>\n<\/tr>\n<tr>\n<td><code>[[ FILE1 -ot FILE2 ]]<\/code><\/td>\n<td>2 er nyere en fil\u00a0 1<\/td>\n<\/tr>\n<tr>\n<td><code>[[ FILE1 -ef FILE2 ]]<\/code><\/td>\n<td>Samme filer<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"example-1\">Eksempler<\/h3>\n<pre><code>if ping -c 1 google.com; then\n  echo \"Din internet virker ping google ok.\"\nfi\n<\/code><\/pre>\n<pre><code>if grep -q 'foo' ~\/.bash_history; then\n  echo \"Du har skrevet 'foo' tidligere\"\nfi\n<\/code><\/pre>\n<pre><code># String\nif [[ -z \"$string\" ]]; then\n  echo \"Streng er tom\"\nelif [[ -n \"$string\" ]]; then\n  echo \"Streng er ikke tom\"\nfi\n<\/code><\/pre>\n<pre><code># Combinations\nif [[ X ]] &amp;&amp; [[ Y ]]; then\n  ...\nfi\n<\/code><\/pre>\n<pre><code># Equal\nif [[ \"$A\" == \"$B\" ]]\n<\/code><\/pre>\n<pre><code># Regex\nif [[ \"A\" =~ \".\" ]]\n<\/code><\/pre>\n<pre><code>if (( $a &lt; $b )); then\n   echo \"$a er mindre en $b\"\nfi\n<\/code><\/pre>\n<pre><code>if [[ -e \"file.txt\" ]]; then\n  echo \"fil findes\"\nfi\n<\/code><\/pre>\n<h2 id=\"arrays\">Arrays<\/h2>\n<h3 id=\"defining-arrays\">Definere arrays<\/h3>\n<pre><code>Fruits=('Apple' 'Banana' 'Orange')\n<\/code><\/pre>\n<pre><code>Fruits[0]=\"Apple\"\nFruits[1]=\"Banana\"\nFruits[2]=\"Orange\"\n<\/code><\/pre>\n<h3 id=\"working-with-arrays\">Arbejde med arrays<\/h3>\n<pre><code>echo ${Fruits[0]}           # Element #0\necho ${Fruits[@]}           # Alle elementer, space-separated\necho ${#Fruits[@]}          # Antal af elementer\necho ${#Fruits}             # L\u00e6nge p\u00e5 tekst p\u00e5 f\u00f8rste element\necho ${#Fruits[2]}          # L\u00e6nge p\u00e5 tekst p\u00e5 N element \necho ${Fruits[@]:1:2}       # (fra position 1, elementer 2)\n<\/code><\/pre>\n<h3 id=\"operations\">Operations<\/h3>\n<pre><code>Fruits=(\"${Fruits[@]}\" \"Watermelon\")    # Push\nFruits+=('Watermelon')                  # Igen Push\nFruits=( ${Fruits[@]\/Ap*\/} )            # Fjern med regex mash\nunset Fruits[2]                         # Fjern et element\nFruits=(\"${Fruits[@]}\")                 # Duplikere\nlines=(`cat \"logfile\"`)                 # L\u00e6s fra program output\n<\/code><\/pre>\n<h3 id=\"iteration\">Iteration<\/h3>\n<pre><code>for i in \"${arrayName[@]}\"; do\n  echo $i\ndone\n<\/code><\/pre>\n<h2 id=\"history\">Kommander<\/h2>\n<table>\n<tbody>\n<tr>\n<td><code>history<\/code><\/td>\n<td>Viser historien over de sidst kommandoer udf\u00f8rt<\/td>\n<\/tr>\n<tr>\n<td><code>shopt -s histverify<\/code><\/td>\n<td>Udf\u00f8r ikke udvidet resultat\u00a0straks<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"expansions\">Expansions<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>!$<\/code><\/td>\n<td>Expand last parameter of most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!*<\/code><\/td>\n<td>Expand all parameters of most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!-n<\/code><\/td>\n<td>Expand <code>n<\/code>th most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!n<\/code><\/td>\n<td>Expand <code>n<\/code>th command in history<\/td>\n<\/tr>\n<tr>\n<td><code>!<command><\/command><\/code><\/td>\n<td>Expand most recent invocation of command <code><command><\/command><\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 id=\"operations-1\">Operations<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>!!<\/code><\/td>\n<td>Viser kommandoen og udf\u00f8re sidste kommando igen.<\/td>\n<\/tr>\n<tr>\n<td><code>!!:s\/\/\/<\/code><\/td>\n<td>Replace first occurrence of <code><\/code> to <code><\/code> in most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!!:gs\/\/\/<\/code><\/td>\n<td>Replace all occurrences of <code><\/code> to <code><\/code> in most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!$:t<\/code><\/td>\n<td>Expand only basename from last parameter of most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!$:h<\/code><\/td>\n<td>Expand only directory from last parameter of most recent command<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>!!<\/code> and <code>!$<\/code> can be replaced with any valid expansion.<\/p>\n<h3 id=\"slices\">Slices<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>!!:n<\/code><\/td>\n<td>Expand only <code>n<\/code>th token from most recent command (command is <code>0<\/code>; first argument is <code>1<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><code>!^<\/code><\/td>\n<td>Expand first argument from most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!$<\/code><\/td>\n<td>Expand last token from most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!!:n-m<\/code><\/td>\n<td>Expand range of tokens from most recent command<\/td>\n<\/tr>\n<tr>\n<td><code>!!:n-$<\/code><\/td>\n<td>Expand <code>n<\/code>th token to last from most recent command<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>!!<\/code> can be replaced with any valid expansion i.e. <code>!cat<\/code>, <code>!-2<\/code>, <code>!42<\/code>, etc.<\/p>\n<h2 id=\"miscellaneous\">Andre diverse muligheder.<\/h2>\n<h3 id=\"numeric-calculations\">Numeriske beregninger<\/h3>\n<pre><code>$((a + 200))      # Addere 200 til $a\n<\/code><\/pre>\n<pre><code>$((RANDOM%=200))  # Random nummer mellem 0..200\n<\/code><\/pre>\n<h3 id=\"subshells\">Shell i shells<\/h3>\n<p>Her udf\u00f8res et cd til <em>dirsted<\/em> men st\u00e5r stadig i det bibliotek som du startede med at st\u00e5 i.<\/p>\n<pre><code>(cd dirsted; echo \"Vi er nu i $PWD biblioteket\")\npwd # still in first directory\n<\/code><\/pre>\n<h3 id=\"redirection\">Omdirigering af output.<\/h3>\n<pre><code>python hello.py &gt; output.txt   # stdout til (file)\npython hello.py &gt;&gt; output.txt  # stdout til (file), tilf\u00f8j til filen.\npython hello.py 2&gt; error.log   # stderr til (file)\npython hello.py 2&gt;&amp;1           # stderr til stdout\npython hello.py 2&gt;\/dev\/null    # stderr til (null)\npython hello.py &amp;&gt;\/dev\/null    # stdout og stderr til (null)\n<\/code><\/pre>\n<pre><code>python hello.py &lt; foo.txt      # feed foo.txt til stdin for python\n<\/code><\/pre>\n<h3 id=\"inspecting-commands\">Inspektion af kommandoer.<\/h3>\n<pre><code>command -V cd\n#=&gt; \"cd is a function\/alias\/whatever\"\n<\/code><\/pre>\n<h3 id=\"trap-errors\">Fang fejl.<\/h3>\n<pre><code>trap 'echo Error at about $LINENO' ERR\n<\/code><\/pre>\n<p>eller<\/p>\n<pre><code>traperr() {\n  echo \"ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}\"\n}\nset -o errtrace\ntrap traperr ERR\n<\/code><\/pre>\n<h3 id=\"caseswitch\">Case\/switch<\/h3>\n<pre><code>case \"$1\" in\n  start | up)\n    vagrant up\n    ;;\n  *)\n    echo \"Usage: $0 {start|stop|ssh}\"\n    ;;\nesac\n<\/code><\/pre>\n<h3 id=\"source-relative\">Kilde relativ<\/h3>\n<pre><code>source \"${0%\/*}\/..\/share\/foo.sh\"\n<\/code><\/pre>\n<h3 id=\"printf\">printf<\/h3>\n<pre><code>printf \"Hello %s,\" Jeg er Olga\n#=&gt; \"Hello Sven, Jeg er Olga\n<\/code><\/pre>\n<h3 id=\"directory-of-script\">Directory i script<\/h3>\n<pre><code>DIR=\"${0%\/*}\"\n<\/code><\/pre>\n<h3 id=\"getting-options\">Getting options<\/h3>\n<pre><code>while [[ \"$1\" =~ ^- &amp;&amp; ! \"$1\" == \"--\" ]]; do case $1 in\n  -V | --version )\n    echo $version\n    exit\n    ;;\n  -s | --string )\n    shift; string=$1\n    ;;\n  -f | --flag )\n    flag=1\n    ;;\nesac; shift; done\nif [[ \"$1\" == '--' ]]; then shift; fi\n<\/code><\/pre>\n<h3 id=\"heredoc\">Heredoc<\/h3>\n<pre><code>cat &lt;<\/code><\/pre>\n<h3 id=\"reading-input\">Reading input<\/h3>\n<pre><code>echo -n \"Proceed? [y\/n]: \"\nread ans\necho $ans\n<\/code><\/pre>\n<pre><code>read -n 1 ans    # Just one character\n<\/code><\/pre>\n<h3 id=\"special-variables\">Special variables<\/h3>\n<table>\n<tbody>\n<tr>\n<td><code>$?<\/code><\/td>\n<td>Exit status fra sidste task<\/td>\n<\/tr>\n<tr>\n<td><code>$!<\/code><\/td>\n<td>PID fra sidste backgrounds task<\/td>\n<\/tr>\n<tr>\n<td><code>$<\/code><\/td>\n<td>PID nr p\u00e5 shell<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Se ogs\u00e5 <a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/shellvars#special_parameters_and_shell_variables\">Special parameters<\/a>.<\/p>\n<p>G\u00e5 til sidste bibliotek<\/p>\n<pre><code>pwd # \/home\/user\/foo\ncd bar\/\npwd # \/home\/user\/foo\/bar\ncd -\npwd # \/home\/user\/foo\n<\/code><\/pre>\n<p>Her er flere links til guider om bash. Men de er p\u00e5 engelsk.<\/p>\n<ul>\n<li><a href=\"http:\/\/wiki.bash-hackers.org\/\">Bash-hackers wiki<\/a> <em>(bash-hackers.org)<\/em><\/li>\n<li><a href=\"http:\/\/wiki.bash-hackers.org\/syntax\/shellvars\">Shell vars<\/a> <em>(bash-hackers.org)<\/em><\/li>\n<li><a href=\"https:\/\/learnxinyminutes.com\/docs\/bash\/\">Learn bash in y minutes<\/a> <em>(learnxinyminutes.com)<\/em><\/li>\n<li><a href=\"http:\/\/mywiki.wooledge.org\/BashGuide\">Bash Guide<\/a> <em>(mywiki.wooledge.org)<\/em><\/li>\n<li><a href=\"https:\/\/www.shellcheck.net\/\">ShellCheck<\/a> <em>(shellcheck.net)<\/em><\/li>\n<\/ul>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_74\" class=\"pvc_stats all  \" data-element-id=\"74\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.linuxboxen.dk\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Her er nogle eksempler p\u00e5 hvordan man bruger bash mange st\u00e6rke sider. Liste er meget lang og har taget mig en del tid at lave. Bash scripting oversigt Eksempler. #!\/usr\/bin\/env bash NAME=&#8221;John&#8221; echo &#8220;Hello $NAME!&#8221; Variabler NAME=&#8221;John&#8221; echo $NAME echo &#8220;$NAME&#8221; echo &#8220;${NAME}!&#8221; Strenge i bash NAME=&#8221;John&#8221; echo &#8220;Hej $NAME&#8221; #=&gt; Hej John echo &#8216;Hej [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_74\" class=\"pvc_stats all  \" data-element-id=\"74\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.linuxboxen.dk\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":"","footnotes":""},"class_list":["post-74","page","type-page","status-publish","hentry"],"a3_pvc":{"activated":true,"total_views":102,"today_views":0},"_links":{"self":[{"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=\/wp\/v2\/pages\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=74"}],"version-history":[{"count":0,"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=\/wp\/v2\/pages\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.linuxboxen.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}