mastodon.world is one of the many independent Mastodon servers you can use to participate in the fediverse.
Generic Mastodon server for anyone to use.

Server stats:

9.6K
active users

#shellscripting

4 posts4 participants0 posts today

Apropos an ongoing project, looking at sed, and realising:

  • It can execute external commands (e)
  • It can read in entire files at a given address within the input stream (r).
  • It can read in specified external files on a line-by-line basis at a specified address within the input stream (R).

I've only been using sed for, oh, 40 years.

gnu.org/software/sed/manual/se

www.gnu.orgsed, a stream editorsed, a stream editor

Cute little script is cool! I wrote a little bash script to wrap around a thesaurus .txt file I found in a unix stack exchange answer (link in script).

It started out as a one-liner, but I adapted it after the results were not completely terrible, though I'm always looking for better ones.

Why are all thesauruses (thesauri? thesauruseseseses?) online and why aren't there any decent local offerings for linux?

Either way, enjoy this little standalone bash script - I'm p proud of the ranking algorithm implementation in the big piped one-liner bit at the end.

gitlab.com/sbrl/bin/-/raw/mast

Today I learned from the flock(1) man page (on Debian) that you can put this line at the top of a shell script to prevent more than one copy of it from ever running at a time:

[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :

This is an extremely clever hack and I don't know how I've never stumbled upon it in 38 years of writing shell scripts.
#unix #linux #scripting #shellScripting

I think I won't bother anymore with writing (bash) shell scripts that are longer than a few lines. I find the syntax too unintuitive and there are better programming languages like PHP that produce more readable code.

In the last two hours, I translated a bash shell script with ~250 lines to a PHP CLI script. The latter is nearly 400 lines long but definetly more readable and it also has more user-friendly output.

1/2

Interesting #ShellScripting quandary:

(solution below)

I'm uncertain how shells delimit filename variables internally.

I have a shell (bash) function that loops through a set of PDF files, opening them in zathura in a "suckless" tabbed window (so that the PDFs always open within the same window, so I can just hit q to zip through them one-at-a-time.

Here is my variable declaration:

local files=${*-*.[pP][dD][fF]}

But for simplicity, you could just imagine it to be:

files=*

Then I'm doing a

for f in $files; do

I'm wanting to print a status line for each item viewed, so I have an idea how many more there are to go, so I'm using this:

echo "[[$f]] ($count/$tot)"

And of course, there's a ((count++)) at the beginning of the loop.

But how to get the total ($tot)??

echo $files |wc -l will always result in 1, as does echo "$files" |wc -l

What I ended up doing was just creating a

for f in $files; do ((tot++)); done

One-liner loop before the actual loop, just to get a total.

Is there a better way?

Am I missing something really obvious?

Solution, courtesy of @khm

Use an array!

local files=(${*-*.[pP][dD][fF]})

echo "[[$f]] ($count/${#files[@]})"

Should've made this a long time ago:

function ciglob {
    #case-insensitive glob generator
    echo "$*" |while read -N1 c; do
        case "$c" in
            [a-zA-Z])   echo -n "[${c^^}${c,,}]";;
            *)          echo -n "$c"
        esac
    done
}
~ $ ciglob "Hello, world!"
[Hh][Ee][Ll][Ll][Oo], [Ww][Oo][Rr][Ll][Dd]!
~ $ ls -ld $(ciglob documents)
drwxr-xr-x 52 ~~~ ~~~ 20,480 Apr 10 11:45 Documents

(Not the most useful example, but I did have a use case in mind when I wrote it ;)

P.S. (This is a valid way to close a parenthesis. Fight me ;)

#bash#ksh#sh

I figured out how to "stub" specific commands in shell script tests, but it's not pretty.

# stub the "command" command as a function
function command() {
if [[ "$1" == "-v" ]] && [[ "$2" == "java" ]]; then
# stub that java is not installed
return 1
else
/usr/bin/command $*
fi
}

# and to remove the stub
unset command