Alternative method:
ssh_pipe() {
ssh -T -l "${1:?user}" "${2:?host}" sh -xe
}
ssh_pipe bob 192.168.1.1 <<EOF
... # here goes a shell script!
EOF
$ cat <<'EOF'
$USER
EOF
{ cat <<END_OF_VARS; cat <<'END_OF_SCRIPT'; } | ssh_pipe bob 192.168.1.1
remote_var="$local_var"
END_OF_VARS
echo "I can use $remote_var"
END_OF_SCRIPT
gopher://tilde.institute/0/~dacav/txt/sh.txt from 2021-01-19:
Alternative method:
ssh_pipe() {
ssh -T -l "${1:?user}" "${2:?host}" sh -xe
}
ssh_pipe bob 192.168.1.1 <<EOF
... # here goes a shell script!
EOF
I think there should also be a warning that anything called in the
remote shell script which reads from standard input would *not* work.
gopher://tilde.institute/0/~dacav/txt/sh.txt from 2021-01-19:
$ cat <<'EOF'
$USER
EOF
Okay, didn't realize that there is a quoted-literal version
of here-document too; thanks for mentioning it.
gopher://tilde.institute/0/~dacav/txt/sh.txt from 2021-01-19:
{ cat <<END_OF_VARS; cat <<'END_OF_SCRIPT'; } | ssh_pipe bob 192.168.1.1
remote_var="$local_var"
END_OF_VARS
echo "I can use $remote_var"
END_OF_SCRIPT
Haha, haven't personally used multiple here-documents in single line yet; POSIX specs explicitly mentioned it too, will have to use it sometimes.
Also, today I learned that POSIX also specified `<<- SOMETHING` too,
which is a kill-all-leading-TABs kind of here document- might be useful
for people who wish to indent stuff in their scripts without interfering
with the result. (Like, when using here-document inside one or more levels
of loops or conditions)
The shell interpreter is far from being a programming language.
Just look at how weird it is, and it will be clear that it
was _evolution_, not design.
My day job often offers me the opportunity of improving my
skills. At the same time I realized that many people
tend to ignore the details of this complicated art, ending
up to fragile and unmaintainable tools. Too bad!
I started to collect some best practices, things that I've
figured out over time. My collection is not complete, nor properly organized, but all projects need to start somewhere.
I've published them here for the moment:
gopher://tilde.institute/0/%7edacav/txt/sh.txt
Any feedback and contribution is well accepted!
Thanks for posting them, some handy tips. While I admire POSIX
scripting, I'm a bit too lazy myself and just stick with Bash.
Practically there's not many situations where Bash isn't an easy
and fairly small install, and it's usually already there anyway.
Feedback:
I don't really see the point of using getops to be honest, compared
to positional parameters. I know it's a common practice, but
personally I don't really see any advantage, unless there's a
limitation in POSIX that I don't know about.
"Perl style" booleans seem a little confusing for someone who always
thinks of FALSE as zero.
The ssh_pipe stuff is neat. Probably not useful for anything I do
myself, but neat.
Thanks for posting them, some handy tips. While I admire POSIX
scripting, I'm a bit too lazy myself and just stick with Bash.
Practically there's not many situations where Bash isn't an easy
and fairly small install, and it's usually already there anyway.
I try to avoid "young" features for the sake of compatibility
(and likelihood of future change) unless they're just irresistable.
On 2021-01-23, The Free Thinker <freet@aussies.space> wrote:
If I'm tossing together a quick script to automate something I'll run
for myself I may use Bash, but anything I intend to share deserves
that little extra effort to make it as portable as I can.
James Tomasino <tomasino@cosmic.voyage> writes:
If I'm tossing together a quick script to automate something I'll run
for myself I may use Bash, but anything I intend to share deserves
that little extra effort to make it as portable as I can.
Shouldnt AWK be near to omnipresent? I started to use it a bit more
when I noticed it being in OpenWrt's busybox, so even on a router
without LuCI (the WebGUI which pulls in Lua as dependency) you still
have SH and AWK and the later one sure is much more readable.
I only have some badly commented examples because on these small systems
one typically don't waste space:
https://openwrt.org/user/yeti/ncomm
https://openwrt.org/user/yeti/mirror-opkg-repositories
https://openwrt.org/user/yeti/opkg-list-user-installed-packages
I think AWK is massively underrated these days.
Sysop: | deepend |
---|---|
Location: | Calgary, Alberta |
Users: | 255 |
Nodes: | 10 (0 / 10) |
Uptime: | 126:57:57 |
Calls: | 1,718 |
Calls today: | 2 |
Files: | 4,099 |
D/L today: |
1 files (712K bytes) |
Messages: | 392,093 |