mirror of
https://github.com/tldr-pages/tldr.git
synced 2025-08-22 17:23:58 +02:00
$, case, continue, coproc, getopts, select, suspend, ulimit, while: refresh page (#17687)
Refreshed $, case, continue, coproc, getopts, select, suspend, ulimit, while .md pages
This commit is contained in:
parent
779f768455
commit
bf47c685ac
9 changed files with 21 additions and 17 deletions
|
@ -33,4 +33,4 @@
|
|||
|
||||
- Print out a Bash array:
|
||||
|
||||
`echo ${array[@]}`
|
||||
`echo ${{{array_name[@]}}}`
|
||||
|
|
|
@ -13,11 +13,11 @@
|
|||
|
||||
- Allow matching multiple patterns:
|
||||
|
||||
`case {{$ANIMAL}} in {{cat}}) echo "It's a cat" ;;& {{cat|dog}}) echo "It's a cat or a dog" ;;& *) echo "Fallback" ;; esac`
|
||||
`case {{$ANIMAL}} in {{cat}}) {{echo "It's a cat"}} ;;& {{cat|dog}}) {{echo "It's a cat or a dog"}} ;;& *) {{echo "Fallback"}} ;; esac`
|
||||
|
||||
- Continue to the next pattern's commands without checking the pattern:
|
||||
|
||||
`case {{$ANIMAL}} in {{cat}}) echo "It's a cat" ;& {{dog}}) echo "It's either a dog or cat fell through" ;& *) echo "Fallback" ;; esac`
|
||||
`case {{$ANIMAL}} in {{cat}}) echo {{"It's a cat"}} ;& {{dog}}) {{echo "It's either a dog or cat fell through"}} ;& *) {{echo "Fallback"}} ;; esac`
|
||||
|
||||
- Display help:
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
|
||||
- Skip to the next iteration:
|
||||
|
||||
`while :; do continue; echo "This will never be reached"; done`
|
||||
`while :; do continue; {{echo "This will never be reached"}}; done`
|
||||
|
||||
- Skip to the next iteration from within a nested loop:
|
||||
|
||||
`for i in {1..3}; do while :; do continue 2; done; done`
|
||||
`for i in {{{1..3}}}; do {{echo $i}}; while :; do continue 2; done; done`
|
||||
|
|
|
@ -21,11 +21,11 @@
|
|||
|
||||
- Create a coprocess which repeatedly reads `stdin` and runs some commands on the input:
|
||||
|
||||
`coproc {{name}} { while read line; do {{command1; command2; ...}}; done }`
|
||||
`coproc {{name}} { while read {{line}}; do {{command1; command2; ...}}; done }`
|
||||
|
||||
- Create a coprocess which repeatedly reads `stdin`, runs a pipeline on the input, and writes the output to `stdout`:
|
||||
|
||||
`coproc {{name}} { while read line; do echo "$line" | {{command1 | command2 | ...}} | cat /dev/fd/0; done }`
|
||||
`coproc {{name}} { while read {{line}}; do {{echo "$line"}} | {{command1 | command2 | ...}} | cat /dev/fd/0; done }`
|
||||
|
||||
- Create and use a coprocess running `bc`:
|
||||
|
||||
|
|
|
@ -4,21 +4,25 @@
|
|||
> This command does not support longform options and thus using `getopt` is recommended instead.
|
||||
> More information: <https://www.gnu.org/software/bash/manual/bash.html#index-getopts>.
|
||||
|
||||
- Check if an option is set:
|
||||
- Check if an option is the first set option in the current context:
|
||||
|
||||
`getopts {{x}} {{opt}}; echo $opt`
|
||||
`getopts {{x}} {{opt}}; echo ${{opt}}`
|
||||
|
||||
- Set an option to require an argument and check said argument:
|
||||
- Check if an option is set in a string (specified option must be the first element of the string):
|
||||
|
||||
`getopts {{x}}: {{opt}}; echo $OPTARG`
|
||||
`getopts {{x}} {{opt}} "{{string text}}"; echo ${{opt}}`
|
||||
|
||||
- Set an option to require an argument and print them:
|
||||
|
||||
`getopts {{x}}: {{opt}}; echo ${{opt}} $OPTARG`
|
||||
|
||||
- Check for multiple options:
|
||||
|
||||
`while getopts {{xyz}} {{opt}}; do case $opt in x) echo x is set;; y) echo y is set;; z) echo z is set;; esac; done`
|
||||
`while getopts {{xyz}} {{opt}}; do case ${{opt}} in x) {{echo x is set}};; y) {{echo y is set}};; z) {{echo z is set}};; esac; done`
|
||||
|
||||
- Set `getopts` to silent mode and handle option errors:
|
||||
|
||||
`while getopts :{{x:}} {{opt}}; do case $opt in x) ;; :) echo "Argument required";; ?) echo "Invalid argument" esac;; done`
|
||||
`while getopts :{{x:}} {{opt}}; do case ${{opt}} in x) ;; :) {{echo "Argument required"}};; ?) {{echo "Invalid argument"}} esac;; done`
|
||||
|
||||
- Reset `getopts`:
|
||||
|
||||
|
|
|
@ -17,4 +17,4 @@
|
|||
|
||||
- Create a menu from a Bash array:
|
||||
|
||||
`{{fruits}}=({{apple orange pear banana}}); select {{word}} in ${{{fruits[@]}}}; do echo ${{word}}; done`
|
||||
`{{fruits}}={{(apple orange pear banana)}}; select {{word}} in ${{{fruits[@]}}}; do echo ${{word}}; done`
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
- Continue from suspension if `suspend` was used in a non-nested shell (run this in a separate terminal):
|
||||
|
||||
`pkill -CONT bash`
|
||||
`pkill -CONT {{bash}}`
|
||||
|
||||
- Force suspension even if this would lock you out of the system:
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
- Set max per-user process limit:
|
||||
|
||||
`ulimit -u 30`
|
||||
`ulimit -u {{30}}`
|
||||
|
||||
- Display help (Bash only):
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
- Read `stdin` and perform an action on every line:
|
||||
|
||||
`while read line; do echo "$line"; done`
|
||||
`while read line; do {{echo "$line"}}; done`
|
||||
|
||||
- Execute a command forever once every second:
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue