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

Faster whitespace trim. Safely passing commands/args to remote bash. Capturing STDERR but not STDOUT #141

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 65 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ trim_string() {
}
```

```sh
trim_string_fast() {
local -
set -f
echo $1
}
```

The `local -` makes `$-` local so that `set -f` is only effective within this function.

**Example Usage:**

```shell
Expand All @@ -223,10 +233,10 @@ without leading/trailing white-space and with truncated spaces.
# shellcheck disable=SC2086,SC2048
trim_all() {
# Usage: trim_all " example string "
local -
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
```

Expand Down Expand Up @@ -1893,9 +1903,10 @@ f()for i in "$@"; do echo "$i"; done

```shell
# One line
# Note: The 3rd statement may run when the 1st is true
# Note: The 3rd statement may run when the 1st is true and 2nd false
[[ $var == hello ]] && echo hi || echo bye
[[ $var == hello ]] && { echo hi; echo there; } || echo bye
# Note: The 3rd statement wont run when the 1st is true and 2nd false
[[ $var == hello ]] && { echo hi; echo there; :; } || echo bye

# Multi line (no else, single statement)
# Note: The exit status may not be the same as with an if statement
Expand Down Expand Up @@ -2164,7 +2175,7 @@ This will run the given command and keep it running, even after the terminal or

```sh
bkr() {
(nohup "$@" &>/dev/null &)
("$@" &>/dev/null &)
}

bkr ./some_script.sh # some_script.sh is now running in the background
Expand All @@ -2182,12 +2193,62 @@ to_upper() {

ptr=${ptr^^}
}
# Alternative: to_upper() { eval "${1}"="${!1^^}";}

foo="bar"
to_upper foo
printf "%s\n" "${foo}" # BAR
```


## Making a string safe for passing as argument to another command

This is useful when executing a script on a remote system without copying the script (and when the remote shell is not Bash)

```sh
mk_safe_arg() {
# Escape all single-' with '"'"'
echo "${1//\'/\'\"\'\"\'}"
}

# A bash script for testing
cat >s.sh<<-'__EOF__'
#! /usr/bin/env bash
echo "Hello single-' and double-\" and '$USER' on '$HOSTNAME'"
__EOF__
# Load the script into the variable s
s="$(mk_safe_arg "$(<s.sh)")"

# Execute the script remotely without copying the script
ssh user@host "bash -c '$s'"

# Execute locally (for testing)
sh -c "bash -c '$s'"
```

## Capturing STDERR without capturing STDOUT.

The STDOUT passes through and STDERR is stored in `err`.
```sh
{ err="$( { echo 1>&2 "Hello-STDERR"; exit 123; } 2>&1 1>&3 3>&- )"; } 3>&1
echo "ret=$?, err=$err"
# ret=123, err=Hello-STDERR
```

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`.

The variable `err` then contains the STDERR of the inner `{..}`.

## Block coomments

```sh
:<<-'###COMMENT-BLOCK'
This is a block comment in bash
Test $HOME $(id) `id` {} (:;)
###COMMENT-BLOCK
```


<!-- CHAPTER END -->

# AFTERWORD
Expand Down