mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-17 21:06:36 +02:00
Merge 29be58b3fa
into 26bc7e8d6c
This commit is contained in:
commit
ade0a749c6
39
README.md
39
README.md
|
@ -175,8 +175,12 @@ without leading/trailing white-space and with truncated spaces.
|
|||
trim_all() {
|
||||
# Usage: trim_all " example string "
|
||||
|
||||
# Disable globbing to make the word-splitting below safe.
|
||||
set -f
|
||||
# Save and disable the globbing state to make the word-splitting
|
||||
# below safe.
|
||||
case $- in
|
||||
*f*) DIDGLOB=false; ;;
|
||||
*) DIDGLOB=true; set -f; ;;
|
||||
esac
|
||||
|
||||
# Set the argument list to the word-splitted string.
|
||||
# This removes all leading/trailing white-space and reduces
|
||||
|
@ -186,8 +190,8 @@ trim_all() {
|
|||
# Print the argument list as a string.
|
||||
printf '%s\n' "$*"
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
# Re-set globbing to it's previous condition
|
||||
$DIDGLOB && set +f
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -270,9 +274,12 @@ This is an alternative to `cut`, `awk` and other tools.
|
|||
|
||||
```sh
|
||||
split() {
|
||||
# Disable globbing.
|
||||
# This ensures that the word-splitting is safe.
|
||||
set -f
|
||||
# Save and disable the globbing state to make the word-splitting
|
||||
# below safe.
|
||||
case $- in
|
||||
*f*) DIDGLOB=false; ;;
|
||||
*) DIDGLOB=true; set -f; ;;
|
||||
esac
|
||||
|
||||
# Store the current value of 'IFS' so we
|
||||
# can restore it later.
|
||||
|
@ -296,9 +303,8 @@ split() {
|
|||
# Restore the value of 'IFS'.
|
||||
IFS=$old_ifs
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
}
|
||||
# Re-set globbing to it's previous condition
|
||||
$DIDGLOB && set +f
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
@ -326,9 +332,12 @@ $ split "1, 2, 3, 4, 5" ", "
|
|||
trim_quotes() {
|
||||
# Usage: trim_quotes "string"
|
||||
|
||||
# Disable globbing.
|
||||
# This makes the word-splitting below safe.
|
||||
set -f
|
||||
# Save and disable the globbing state to make the word-splitting
|
||||
# below safe.
|
||||
case $- in
|
||||
*f*) DIDGLOB=false; ;;
|
||||
*) DIDGLOB=true; set -f; ;;
|
||||
esac
|
||||
|
||||
# Store the current value of 'IFS' so we
|
||||
# can restore it later.
|
||||
|
@ -355,8 +364,8 @@ trim_quotes() {
|
|||
# Restore the value of 'IFS'.
|
||||
IFS=$old_ifs
|
||||
|
||||
# Re-enable globbing.
|
||||
set +f
|
||||
# Re-set globbing to it's previous condition
|
||||
$DIDGLOB && set +f
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue