Update bash.md

`[` use is made obsolete by `[[` (which should be used only for bash scripts).
Add examples with commands conditionals (if ping ...; then...)
Add link to shellcheck + mywiki.wooledge.org
This commit is contained in:
Moviuro 2018-07-23 15:34:18 +02:00 committed by GitHub
parent e9ac8eb8f4
commit 194bd28bf9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 39 deletions

96
bash.md
View File

@ -78,9 +78,9 @@ See: [Functions](#functions)
{: id='conditionals-example'} {: id='conditionals-example'}
```bash ```bash
if [ -z "$string" ]; then if [[ -z "$string" ]]; then
echo "String is empty" echo "String is empty"
elif [ -n "$string" ]; then elif [[ -n "$string" ]]; then
echo "String is not empty" echo "String is not empty"
fi fi
``` ```
@ -237,7 +237,7 @@ done
### Reading lines ### Reading lines
```bash ```bash
cat file.txt | while read line; do < file.txt | while read line; do
echo $line echo $line
done done
``` ```
@ -283,7 +283,7 @@ myfunc() {
``` ```
```bash ```bash
result=$(myfunc) result="$(myfunc)"
``` ```
### Raising errors ### Raising errors
@ -319,61 +319,75 @@ Conditionals
### Conditions ### Conditions
Note that `[[` is actually a command/program that returns either `0` (true) or `1` (false). Any program that obeys the same logic (like all base utils, such as `grep(1)` or `ping(1)`) can be used as condition, see examples.
| Condition | Description | | Condition | Description |
| --- | --- | | --- | --- |
| `[ -z STRING ]` | Empty string | | `[[ -z STRING ]]` | Empty string |
| `[ -n STRING ]` | Not empty string | | `[[ -n STRING ]]` | Not empty string |
| --- | --- | | --- | --- |
| `[ NUM -eq NUM ]` | Equal | | `[[ NUM -eq NUM ]]` | Equal |
| `[ NUM -ne NUM ]` | Not equal | | `[[ NUM -ne NUM ]]` | Not equal |
| `[ NUM -lt NUM ]` | Less than | | `[[ NUM -lt NUM ]]` | Less than |
| `[ NUM -le NUM ]` | Less than or equal | | `[[ NUM -le NUM ]]` | Less than or equal |
| `[ NUM -gt NUM ]` | Greater than | | `[[ NUM -gt NUM ]]` | Greater than |
| `[ NUM -ge NUM ]` | Greater than or equal | | `[[ NUM -ge NUM ]]` | Greater than or equal |
| --- | --- | | --- | --- |
| `[[ STRING =~ STRING ]]` | Regexp | | `[[ STRING =~ STRING ]]` | Regexp |
| --- | --- | | --- | --- |
| `(( NUM < NUM ))` | Numeric conditions | | `(( NUM < NUM ))` | Numeric conditions |
| Condition | Description | | Condition | Description |
| --- | --- | | --- | --- |
| `[ -o noclobber ]` | If OPTIONNAME is enabled | | `[[ -o noclobber ]]` | If OPTIONNAME is enabled |
| --- | --- | | --- | --- |
| `[ ! EXPR ]` | Not | | `[[ ! EXPR ]]` | Not |
| `[ X ] && [ Y ]` | And | | `[[ X ]] && [[ Y ]]` | And |
| `[ X ] || [ Y ]` | Or | | `[[ X ]] || [[ Y ]]` | Or |
### File conditions ### File conditions
| Condition | Description | | Condition | Description |
| --- | --- | | --- | --- |
| `[ -e FILE ]` | Exists | | `[[ -e FILE ]]` | Exists |
| `[ -r FILE ]` | Readable | | `[[ -r FILE ]]` | Readable |
| `[ -h FILE ]` | Symlink | | `[[ -h FILE ]]` | Symlink |
| `[ -d FILE ]` | Directory | | `[[ -d FILE ]]` | Directory |
| `[ -w FILE ]` | Writable | | `[[ -w FILE ]]` | Writable |
| `[ -s FILE ]` | Size is > 0 bytes | | `[[ -s FILE ]]` | Size is > 0 bytes |
| `[ -f FILE ]` | File | | `[[ -f FILE ]]` | File |
| `[ -x FILE ]` | Executable | | `[[ -x FILE ]]` | Executable |
| --- | --- | | --- | --- |
| `[ FILE1 -nt FILE2 ]` | 1 is more recent than 2 | | `[[ FILE1 -nt FILE2 ]]` | 1 is more recent than 2 |
| `[ FILE1 -ot FILE2 ]` | 2 is more recent than 1 | | `[[ FILE1 -ot FILE2 ]]` | 2 is more recent than 1 |
| `[ FILE1 -ef FILE2 ]` | Same files | | `[[ FILE1 -ef FILE2 ]]` | Same files |
### Example ### Example
```bash
if ping -c 1 google.com; then
echo "It appears you have a working internet connection"
fi
````
```bash
if grep -q 'foo' ~/.bash_history; then
echo "You appear to have typed 'foo' in the past"
fi
```
```bash ```bash
# String # String
if [ -z "$string" ]; then if [[ -z "$string" ]]; then
echo "String is empty" echo "String is empty"
elif [ -n "$string" ]; then elif [[ -n "$string" ]]; then
echo "String is not empty" echo "String is not empty"
fi fi
``` ```
```bash ```bash
# Combinations # Combinations
if [ X ] && [ Y ]; then if [[ X ]] && [[ Y ]]; then
... ...
fi fi
``` ```
@ -384,11 +398,13 @@ if [[ "A" =~ "." ]]
``` ```
```bash ```bash
if (( $a < $b )) if (( $a < $b )); then
echo "$a is smaller than $b"
fi
``` ```
```bash ```bash
if [ -e "file.txt" ]; then if [[ -e "file.txt" ]]; then
echo "file exists" echo "file exists"
fi fi
``` ```
@ -529,7 +545,7 @@ python hello.py &>/dev/null # stdout and stderr to (null)
``` ```
```bash ```bash
python hello.py < foo.txt python hello.py < foo.txt # feed foo.txt to stdin for python
``` ```
### Inspecting commands ### Inspecting commands
@ -641,3 +657,5 @@ See [Special parameters](http://wiki.bash-hackers.org/syntax/shellvars#special_p
* [Bash-hackers wiki](http://wiki.bash-hackers.org/) _(bash-hackers.org)_ * [Bash-hackers wiki](http://wiki.bash-hackers.org/) _(bash-hackers.org)_
* [Shell vars](http://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_ * [Shell vars](http://wiki.bash-hackers.org/syntax/shellvars) _(bash-hackers.org)_
* [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_ * [Learn bash in y minutes](https://learnxinyminutes.com/docs/bash/) _(learnxinyminutes.com)_
* [Bash Guide](http://mywiki.wooledge.org/BashGuide) _(mywiki.wooledge.org)_
* [ShellCheck](https://www.shellcheck.net/) _(shellcheck.net)_