mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-09 01:02:30 +02:00
docs: update
This commit is contained in:
parent
cca0abedae
commit
26859234a9
58
README.md
58
README.md
|
@ -17,6 +17,7 @@ align="center">A collection of pure POSIX sh alternatives to external processes.
|
||||||
* [Check if string starts with sub-string](#check-if-string-starts-with-sub-string)
|
* [Check if string starts with sub-string](#check-if-string-starts-with-sub-string)
|
||||||
* [Check if string ends with sub-string](#check-if-string-ends-with-sub-string)
|
* [Check if string ends with sub-string](#check-if-string-ends-with-sub-string)
|
||||||
* [Split a string on a delimiter](#split-a-string-on-a-delimiter)
|
* [Split a string on a delimiter](#split-a-string-on-a-delimiter)
|
||||||
|
* [Trim quotes from a string](#trim-quotes-from-a-string)
|
||||||
* [FILES](#files)
|
* [FILES](#files)
|
||||||
* [Get the first N lines of a file](#get-the-first-n-lines-of-a-file)
|
* [Get the first N lines of a file](#get-the-first-n-lines-of-a-file)
|
||||||
* [Get the number of lines in a file](#get-the-number-of-lines-in-a-file)
|
* [Get the number of lines in a file](#get-the-number-of-lines-in-a-file)
|
||||||
|
@ -234,6 +235,10 @@ split() {
|
||||||
# This ensures that the word-splitting is safe.
|
# This ensures that the word-splitting is safe.
|
||||||
set -f
|
set -f
|
||||||
|
|
||||||
|
# Store the current value of 'IFS' so we
|
||||||
|
# can restore it later.
|
||||||
|
old_ifs=$IFS
|
||||||
|
|
||||||
# Change the field separator to what we're
|
# Change the field separator to what we're
|
||||||
# splitting on.
|
# splitting on.
|
||||||
IFS=$2
|
IFS=$2
|
||||||
|
@ -249,6 +254,9 @@ split() {
|
||||||
# Print each list value on its own line.
|
# Print each list value on its own line.
|
||||||
printf '%s\n' "$@"
|
printf '%s\n' "$@"
|
||||||
|
|
||||||
|
# Restore the value of 'IFS'.
|
||||||
|
IFS=$old_ifs
|
||||||
|
|
||||||
# Re-enable globbing.
|
# Re-enable globbing.
|
||||||
set +f
|
set +f
|
||||||
}
|
}
|
||||||
|
@ -271,6 +279,56 @@ $ split "1, 2, 3, 4, 5" ", "
|
||||||
5
|
5
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Trim quotes from a string
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
trim_quotes() {
|
||||||
|
# Usage: trim_quotes "string"
|
||||||
|
|
||||||
|
# Disable globbing.
|
||||||
|
# This makes the word-splitting below safe.
|
||||||
|
set -f
|
||||||
|
|
||||||
|
# Store the current value of 'IFS' so we
|
||||||
|
# can restore it later.
|
||||||
|
old_ifs=$IFS
|
||||||
|
|
||||||
|
# Set 'IFS' to ["'].
|
||||||
|
IFS=\"\'
|
||||||
|
|
||||||
|
# Create an argument list, splitting the
|
||||||
|
# string at ["'].
|
||||||
|
#
|
||||||
|
# Disable this shellcheck error as it only
|
||||||
|
# warns about word-splitting which we expect.
|
||||||
|
# shellcheck disable=2086
|
||||||
|
set -- $1
|
||||||
|
|
||||||
|
# Set 'IFS' to blank to remove spaces left
|
||||||
|
# by the removal of ["'].
|
||||||
|
IFS=
|
||||||
|
|
||||||
|
# Print the quote-less string.
|
||||||
|
printf '%s\n' "$*"
|
||||||
|
|
||||||
|
# Restore the value of 'IFS'.
|
||||||
|
IFS=$old_ifs
|
||||||
|
|
||||||
|
# Re-enable globbing.
|
||||||
|
set +f
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ var="'Hello', \"World\""
|
||||||
|
$ trim_quotes "$var"
|
||||||
|
Hello, World
|
||||||
|
```
|
||||||
|
|
||||||
# FILES
|
# FILES
|
||||||
|
|
||||||
## Get the first N lines of a file
|
## Get the first N lines of a file
|
||||||
|
|
Loading…
Reference in a new issue