mathstodon.xyz is one of the many independent Mastodon servers you can use to participate in the fediverse.
A Mastodon instance for maths people. We have LaTeX rendering in the web interface!

Server stats:

2.8K
active users

#shellscript

1 post1 participant0 posts today

Bash variables: A subshell cannot write back variables to its parent shell.

I want to get a variable AA back anyway and tried:
1. in the subshell: declare -p AA >./tmp/somefile
2. in the parent shell: source ./tmp/somefile

Can this fail? Well (2) is run in a function, so the 'declare ...' we source only declares a local variable. As soon as leaving the function, the AA is gone.

There does not seem to be a version of 'declare -p' which adds "-g" to the output.

Replied in thread

@jutty I sometimes *do* enjoy writing #shellscript. Kind of similar to how I sometimes even enjoy writing #Javascript.

First of all, I think the only sane way to write shell scripts is to aim for strict #POSIX compliance. Sure, if you write "internal" scripts for some OS or distribution, it's fine to target specifically the shell coming with that system, but that's a special case. For everything else, if your script has an extra dependency on a specific shell, it would most likely make more sense to use a more powerful language with possibly a *different* runtime dependency.

That said, sure, you will use an "obscure" language. The trick is: Think about the right tool for the job upfront. The #shell can be very powerful for a certain class of problems, e.g. as an orchestration layer for separate tools offering "shell-friendly" interfaces like flexible commandline arguments and a stdin/stdout format that's easy to parse for a shell script.

Here's my latest non-trivial work in shellscript and I'd use shellscript again for the same job:
github.com/Zirias/dos2ansi/blo

In a nutshell, it orchestrates xterm, less and my own dos2ansi tool to build an ansi-art viewer. It might be a bit "borderline" because it contains obscure functions to parse some string character by character (something a shell can't do in a sane way), but I think that was a fair price for avoiding to depend on some other interpreter, it's still a relatively short script.

Converter for old MS-DOS/ANSI.SYS text files. Contribute to Zirias/dos2ansi development by creating an account on GitHub.
GitHubdos2ansi/src/bin/showansi/showansi.in.tmpl at master · Zirias/dos2ansiConverter for old MS-DOS/ANSI.SYS text files. Contribute to Zirias/dos2ansi development by creating an account on GitHub.
Replied in thread

@NOISEBOB #HowTo run #ShellScript when sound in mic

Here is a #bash function "getvol" which uses #SoX to listen for 0.1 second and then tells you peak volume, between 0 and 1:

function getvol {
rec -p trim 0 .1 stat 2>&1 >/dev/null | grep -Po "(?<=Maximum amplitude:).*" | grep -Po "\S+"
}

You can loop the function and do something if the volume is high enough.

Replied in thread
Just as a tip, if you want to write portable #shellscript, stick to the #POSIX #shell: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sh.html – all the tools you can use are also documented, like e.g. sed: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html

This POSIX shell is more or less a #bourne shell, but specifies a few more features than the classic bourne shell had, e.g. usage of $( ... ) for command replacement instead of only backticks. #FreeBSD's /bin/sh is (AFAIK) POSIX compliant, but I'd assume even this shell can do a few things not specified in POSIX. For testing, I found shells/bosh very useful, it contains a few flavors of minimal shells, one of them a POSIX shell.

Regarding specifically arrays, indeed, the POSIX sh doesn't know them with the single exception of the "argv" array (both for the main script and any shell function), which you can also modify using set, but that of course has other implications. What's always available is word splitting, any variable can contain a list separated by whatever $IFS is set to. It defaults to whitespace. You may override $IFS, but that can be tricky as well, word splitting is used a lot, so expect surprising effects. 🙈

Most of the time, it's best to find a way to solve your problem without arrays. 😉
pubs.opengroup.orgsh

Another week another backlog project release! Another Git extension, and with git-activity (my cross-repo Git log / standup-prep tool) and git-random (my Git tree building tool) one I use a lot:

git-commit-message-summary-length measures the length of a commit message's first line, and compares that to soft and hard limits.

git-commit-message-summary-len

git-commit-message-summary-lengthgit-commit-message-summary-lengthCross-repo customizable Git activity log dotfile with CLI for viewing and filtering entries
#git#CLI#OpenSource