Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 970ceba

Browse files
committed
Faster space trim. Safe args. Capturing STDERR
faster space trim. Safe args. Capturing Stderr nohup not required double fork makes PID=1 the parent and thus automatically removed it from the current session group - and by this will not receive a SIGHUP on exit (which is what we want. Nohup is redundant in this case). Update README.md Update README.md
1 parent 5f5126c commit 970ceba

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

README.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,16 @@ trim_string() {
199199
}
200200
```
201201

202+
```sh
203+
trim_string_fast() {
204+
local -
205+
set -f
206+
echo $1
207+
}
208+
```
209+
210+
The `local -` makes `$-` local so that `set -f` is only effective within this function.
211+
202212
**Example Usage:**
203213

204214
```shell
@@ -223,10 +233,10 @@ without leading/trailing white-space and with truncated spaces.
223233
# shellcheck disable=SC2086,SC2048
224234
trim_all() {
225235
# Usage: trim_all " example string "
236+
local -
226237
set -f
227238
set -- $*
228239
printf '%s\n' "$*"
229-
set +f
230240
}
231241
```
232242

@@ -1893,9 +1903,10 @@ f()for i in "$@"; do echo "$i"; done
18931903
18941904
```shell
18951905
# One line
1896-
# Note: The 3rd statement may run when the 1st is true
1906+
# Note: The 3rd statement may run when the 1st is true and 2nd false
18971907
[[ $var == hello ]] && echo hi || echo bye
1898-
[[ $var == hello ]] && { echo hi; echo there; } || echo bye
1908+
# Note: The 3rd statement wont run when the 1st is true and 2nd false
1909+
[[ $var == hello ]] && { echo hi; echo there; :; } || echo bye
18991910
19001911
# Multi line (no else, single statement)
19011912
# Note: The exit status may not be the same as with an if statement
@@ -2164,7 +2175,7 @@ This will run the given command and keep it running, even after the terminal or
21642175
21652176
```sh
21662177
bkr() {
2167-
(nohup "$@" &>/dev/null &)
2178+
("$@" &>/dev/null &)
21682179
}
21692180
21702181
bkr ./some_script.sh # some_script.sh is now running in the background
@@ -2188,6 +2199,53 @@ to_upper foo
21882199
printf "%s\n" "${foo}" # BAR
21892200
```
21902201
2202+
## Making a string safe for passing as argument to another command
2203+
2204+
This is useful when executing a script on a remote system without copying the script (and when the remote shell is not Bash)
2205+
2206+
```sh
2207+
mk_safe_arg() {
2208+
# Escape all single-' with '"'"'
2209+
echo "${1//\'/\'\"\'\"\'}"
2210+
}
2211+
2212+
# A bash script for testing
2213+
cat >s.sh<<-'__EOF__'
2214+
#! /usr/bin/env bash
2215+
echo "Hello single-' and double-\" and '$USER' on '$HOSTNAME'"
2216+
__EOF__
2217+
# Load the script into the variable s
2218+
s="$(mk_safe_arg "$(<s.sh)")"
2219+
2220+
# Execute the script remotely without copying the script
2221+
ssh user@host "bash -c '$s'"
2222+
2223+
# Execute locally (for testing)
2224+
sh -c "bash -c '$s'"
2225+
```
2226+
2227+
## Capturing STDERR without capturing STDOUT.
2228+
2229+
The STDOUT passes through and STDERR is stored in `err`.
2230+
```sh
2231+
{ err="$( { echo 1>&2 "Hello-STDERR"; exit 123; } 2>&1 1>&3 3>&- )"; } 3>&1
2232+
echo "ret=$?, err=$err"
2233+
# ret=123, err=Hello-STDERR
2234+
```
2235+
2236+
The inner `{..}` writes to STDERR and exits with error code 123. The STDERR from the inner `{..}` is redirected to STDOUT with `2>&1`. The normal STDOUT is redirected to the newly created File Descriptor '3' with `1>&3 3>&-`. The outter `{..}` redirects File Descriptor '3' back to STDOUT with `3>&1`.
2237+
2238+
The variable `err` then contains the STDERR of the inner `{..}`.
2239+
2240+
## Block coomments
2241+
2242+
```sh
2243+
:<<-'###COMMENT-BLOCK'
2244+
This is a block comment in bash
2245+
Test $HOME $(id) `id` {} (:;)
2246+
###COMMENT-BLOCK
2247+
```
2248+
21912249
<!-- CHAPTER END -->
21922250
21932251
# AFTERWORD

0 commit comments

Comments
 (0)