<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bash Trénink]]></title><description><![CDATA[<h3>Bash Trénink</h3>
<p dir="auto">Regulární výrazy pomocí []</p>
<pre><code class="language-bash">$ string=whatever
$ [[ $string =~ h[aeiou] ]]
$ echo $?
0
</code></pre>
<p dir="auto">Evaluace aritmetického výrazu pomocí ((...))<br />
Možno potom zkombinovat s and příkazem</p>
<pre><code>((verbose)) &amp;&amp; command ## execute command if verbose != 0
</code></pre>
<pre><code class="language-bash">Tests
-f test regular file
-e file exists
-d directory exists
-h symbolic link
-x execute file
-s is file and is not empty
mozno i negovat pomocí ! -f 
-z arguments are empty
-n arguments are nonempty
</code></pre>
<p dir="auto">conditional operators</p>
<pre><code class="language-bash">if
if ... then elif then fi

if [ -d "$dir" ] &amp;&amp; cd "$dir"
then
  echo "$PWD"
fi
</code></pre>
<p dir="auto">Case</p>
<pre><code class="language-bash">case WORD in
    PATTERN) COMMANDS ;;
    PATTERN) COMMANDS ;; ## optional
esac

Listing 3-4. Does One String Contain Another?
case $1 in
    *"$2"*) true ;;
    *) false ;;
esac

Listing 3-5. Is This a Valid Positive Integer?
case $1 in
   *[!0-9]*) false;;
   *) true ;;
esac

Check appropriate count of input arguments
case $# in
    3) ;; ## We need 3 args, so do nothing
    *) printf "%s\n" "Please provide three names" &gt;&amp;2
        exit 1
        ;;
esac
</code></pre>
<p dir="auto">Looping</p>
<pre><code class="language-bash">while
while &lt;list&gt;
do
    &lt;list&gt;
done

until
n=1
until [ $n -gt 10 ]
do
    echo "$n"
    n=$(( $n + 1 ))
done

for
for var in Canada USA Mexico
do
    printf "%s\n" "$var"
done
</code></pre>
<p dir="auto">Command Substitution</p>
<pre><code>wc -l $( date +%Y-%m-%d ).log
</code></pre>
<p dir="auto">Vypise pocet radek v danem souboru 2017-03-09.log</p>
<p dir="auto">Process Substitution<br />
Vytvari docasny soubor pro prikaz nebo list prikazu.<br />
Lze jej uzit potom kdekoliv se pouziva soubor</p>
<pre><code class="language-bash">while read perms links owner group size month day time file
do
    printf "%10d %s\n" "$size" "$file"
    totalsize=$(( ${totalsize:=0} + ${size:-0} ))
done &lt; &lt;(ls -l *)
echo ${totalsize-unset}
12879
</code></pre>
<p dir="auto">Check if variable x is set</p>
<pre><code class="language-bash">if [[ ${x+X} = X ]] ## If $x is set
unset x ## unset variable x
export x ## exportuje promennou x do environment variables
</code></pre>
<p dir="auto">Opakování pomocí čárek</p>
<pre><code class="language-bash">printf "%s\n" ${RANDOM}{,,,,,}
printf "%s\n" {1..5}{,}  ## dvojice 1122334455 
</code></pre>
<p dir="auto">Poslední pipe bude provedena v current shellu (bash 4.2)</p>
<pre><code>shopt -s lastpipe
</code></pre>
<p dir="auto">Ověření verze bash skriptu</p>
<pre><code class="language-bash">case $BASH_VERSION in
    [12].*) echo "You need at least bash3.0 to run this script" &gt;&amp;2; exit 2;;
esac
</code></pre>
<pre><code class="language-bash">${var:-default} and ${var-default}: Use Default Values
The most commonly used expansion, ${var:-default}, checks to see whether a variable is unset or empty
and expands to a default string if it is:
  $ var= 
  $ ba "${var:-default}"  ## The sa script was introduced in Chapter 4
  :default:
  If the colon is omitted, the expansion checks only whether the variable is unset:
  $ var=
  $ ba "${var-default}" ## var is set, so expands to nothing
  ::
  $ unset var
  $ ba "${var-default}" ## var is unset, so expands to "default"
  :default:
  defaultfile=$HOME/.bashrc
  ## parse options here
  filename=${filename:-"$defaultfile"}
</code></pre>
<pre><code class="language-bash">${var:+alternate}, ${var+alternate}: Use Alternate Values
The complement to the previous expansion substitutes an alternate value if the parameter is not empty or,
without a colon, if it is set. The first expansion will use alternate only if $var is set and is not empty.
</code></pre>
<p dir="auto">To prevent the leading space, you can use parameter expansion:</p>
<pre><code class="language-bash">$ var=
$ for n in a b c d e f g
&gt; do
&gt;
   var="${var:+"$var "}$n"
&gt; done
$ ba "$var"
:a b c d e f g:
another solution:
[ -n "$var" ] &amp;&amp; var="$var $n" || var=$n
</code></pre>
<pre><code class="language-bash">${var:?message}, ${var?message}: Display Error Message If Empty or Unset
</code></pre>
<p dir="auto">If var is empty or not set, message will be printed to the standard error, and the script will exit with a status<br />
of 1. If message is empty, parameter null or not set will be printed. Listing 5-2 expects two non-null<br />
command-line arguments and uses this expansion to display error messages when they are missing or null.</p>
<pre><code class="language-bash">Listing 5-2. checkarg, Exit If Parameters Are Unset or Empty
## Check for unset arguments
: ${1?An argument is required} \
  ${2?Two arguments are required}

## Check for empty arguments
: ${1:?A non-empty argument is required} \
  ${2:?Two non-empty arguments are required}

echo "Thank you."
</code></pre>
<p dir="auto">Ostatní viz dokumentace pdf...</p>
<pre><code class="language-bash">${var##PATTERN}: Remove the Longest Match from the Beginning
</code></pre>
<p dir="auto">The variable is expanded, and the longest string that matches PATTERN is removed from the beginning of the<br />
expanded value. This is often used to extract the name of a script from the $0 parameter, which contains the<br />
full path to the script:<br />
scriptname=${0##*/} ## /home/chris/bin/script =&gt; script</p>
<pre><code class="language-bash">${var:OFFSET:LENGTH}: Return a Substring of $var
</code></pre>
<p dir="auto">A substring of $var starting at OFFSET is returned. If LENGTH is specified, that number of characters is<br />
substituted; otherwise, the rest of the string is returned. The first character is at offset 0:</p>
<pre><code class="language-bash">$ var=Toronto
$ sa "${var:3:2}"
:on:
$ sa "${var:3}"
:onto:
</code></pre>
<pre><code>${!var}: Indirect Reference
</code></pre>
<p dir="auto">If you have one variable containing the name of another, for example x=yes and a=x, bash can use an<br />
indirect reference:</p>
<pre><code class="language-bash">$ x=yes
$ a=x
$ sa "${!a}"
:yes:
</code></pre>
<p dir="auto">The same effect can be had using the eval builtin command, which expands its arguments and<br />
executes the resulting string as a command:</p>
<pre><code class="language-bash">$ eval "sa \$$a"
:yes:
</code></pre>
<pre><code class="language-bash">${var^PATTERN}: Convert to Uppercase
$ var=toronto
$ sa "${var^}"
:Toronto:
$ sa "${var^^}"
:TORONTO:
</code></pre>
<p dir="auto">${var,PATTERN}: Convert to Lowercase<br />
This expansion works in the same way as the previous one, except that it converts uppercase to lowercase:</p>
<pre><code class="language-bash">$ var=TORONTO
$ sa "${var,,}"
:toronto:
$ sa "${var,,[N-Q]}"
:ToRonTo:There is also an undocumented expansion that inverts the case:
$ var=Toronto
$ sa "${var~}"
:toronto:
$ sa "${var~~}"
:tORONTO:
</code></pre>
<p dir="auto">Cyklovani pres vstupni positional parameters</p>
<pre><code class="language-bash">for param in "$@" ## or just:  for param
do
    : do something with $param
done
</code></pre>
<p dir="auto">And this is the second:</p>
<pre><code class="language-bash">while (( $# ))
do
    : do something with $1
    shift
done
</code></pre>
<p dir="auto">Arrays</p>
<pre><code class="language-bash">$ printf "%s\n" "${BASH_VERSINFO[*]}"
4 3 30 1 release i686-pc-linux-gnuoldld

$ printf "%s\n" "${BASH_VERSINFO[@]}"
4
3
30
1
release
i686-pc-linux-gnu
</code></pre>
<p dir="auto">Various parameter expansions work on arrays; for example, to get the second and third elements from<br />
an array, use this:</p>
<pre><code class="language-bash">$ printf "%s\n" "${BASH_VERSINFO[@]:1:2}" ## minor version number and patch level
3
30
</code></pre>
<p dir="auto">The length expansion returns the number of elements in the array when the subscript is * or @, and it<br />
returns the length of an individual element if a numeric index is given:</p>
<pre><code class="language-bash">$ printf "%s\n" "${#BASH_VERSINFO[*]}"
6
$ printf "%s\n" "${#BASH_VERSINFO[2]}" "${#BASH_VERSINFO[5]}"
2
17
</code></pre>
<p dir="auto">Přiřazení hodnot do pole a použití postupně inkrementujícího indexu pole</p>
<pre><code class="language-bash">$ unset a
$ a[${#a[@]}]="1 $RANDOM"  
$ a[${#a[@]}]="2 $RANDOM"
$ a[${#a[@]}]="3 $RANDOM"
$ a[${#a[@]}]="4 $RANDOM"
$ printf "%s\n" "${a[@]}"
1 6007
2 3784
3 32330
4 25914
</code></pre>
<pre><code class="language-bash">$ province=( Quebec Ontario Manitoba )
$ printf "%s\n" "${province[@]}"
Quebec
Ontario
Manitoba
</code></pre>
<pre><code class="language-bash">$ province+=( Saskatchewan )
$ province+=( Alberta "British Columbia" "Nova Scotia" )
$ printf "%-25s %-25s %s\n" "${province[@]}"
Quebec   Ontario   Manitoba
Saskatchewan   Alberta    British Columbia
Nova Scotia
</code></pre>
<p dir="auto">Associative Arrays<br />
Associative arrays, introduced in bash in version 4.0, use strings as subscripts and must be declared before<br />
being used:</p>
<pre><code class="language-bash">$ declare -A array
$ for subscript in a b c d e
&gt; do
&gt;     array[$subscript]="$subscript $RANDOM"
&gt; done
$ printf ":%s:\n" "${array["c"]}" ## print one element
:c 1574:
$ printf ":%s:\n" "${array[@]}" ## print the entire array
:a 13856:
:b 6235:
</code></pre>
]]></description><link>https://forum.arch-linux.cz/topic/22/bash-trénink</link><generator>RSS for Node</generator><lastBuildDate>Mon, 11 May 2026 15:20:12 GMT</lastBuildDate><atom:link href="https://forum.arch-linux.cz/topic/22.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 20 Mar 2022 11:15:46 GMT</pubDate><ttl>60</ttl></channel></rss>