r/bash • • 8d ago

critique My first bash scripts

The goal for this one is to take a package (eg. cmatrix) and its arguments and input them into the nix run command like this:

nix run nixpkgs#cmatrix -- -a -b ...

#!/usr/bin/env bash
PKG=$1
shift 1
ARGS=$@
nix run "nixpkgs#$PKG" -- "$ARGS"

This goal for this one is to take multiple packages (eg. cmatrix, sl) and put them into the nix shell command like this:

nix shell nixpkgs#cmatrix nixpkgs#sl

#!/usr/bin/env bash
ARGS=""
main() {
   args "$@"
   nix shell$ARGS
}
args() {
    while [[ $# -gt 0 ]]; do
        ARGS="$ARGS nixpkgs#$1"
        shift 1
    done
}

main "$@"

Any feedback or advice would be appreciated :)

EDIT: Here is my rewrite of the second one

#!/usr/bin/env bash
set -e
set -u
set -o pipefail

enter-temporary-shell() {
    if [[ $# -lt 1 ]]; then
        echo "USAGE: $0 PACKAGE [PACKAGE...]"
        exit 1
    fi

    local -a temporary_packages=${1:?}
    temporary_packages=$(prepend-nixpkgs "$@")

    echo "Entering temporary shell with""$temporary_packages" "enabled"
    echo "Press ^D to exit"
    nix shell $temporary_packages
}
prepend-nixpkgs () {
    local -a arguments=("$@")
    echo "${arguments[@]/#/ nixpkgs#}"
}

enter-temporary-shell "$@"
9 Upvotes

7 comments sorted by

3

u/frankensteinsKITTEN 8d ago

My tip would be to use shellcheck: https://www.shellcheck.net/

You can copy+paste your script into the web form, but the command-line tool is even more convenient.

I've only run your first script through shellcheck and it reported an error with your ARGS=$@ as assignment of an array to a string.

1

u/triscalade 8d ago edited 8d ago

Thanks, I didn't know about that tool. :)

EDIT: I tried it out and realized that I could just pass $@ instead of a variable so now it's a line shorter!

0

u/elatllat 8d ago

-1 for bringing the pointless restrictions of  nix to the freedom of /r/bash

3

u/triscalade 8d ago

What do you mean by restrictions?

1

u/Asly97 5d ago

The rewrite is a good step. One thing in it only works by accident though.

local -a temporary_packages=${1:?} makes an array, but the next line, temporary_packages=$(prepend-nixpkgs "$@"), just overwrites element 0 with one long string. So it's really a single string with spaces in it, and nix shell $temporary_packages works only because the unquoted expansion splits it back apart on spaces. Same squashing problem as the first script, just undone at the last step.

You can drop the helper and the subshell. The /#/ substitution you already used works on "$@" directly and keeps every item a separate word:

enter-temporary-shell() {
    if (( $# < 1 )); then
        echo "usage: ${0##*/} PACKAGE [PACKAGE...]" >&2
        exit 1
    fi
    local -a pkgs=("${@/#/nixpkgs#}")
    echo "Entering temporary shell with ${pkgs[*]}"
    echo "Press ^D to exit"
    nix shell "${pkgs[@]}"
}

Small stuff in there: usage goes to stderr so it doesn't end up in a pipe, ${0##*/} prints just the script name instead of the whole path, and ${1:?} isn't needed once you've checked $#.

And the first script fits in one line that keeps each arg separate:

nix run "nixpkgs#$1" -- "${@:2}"

1

u/michaelpaoli 7d ago
shift 1
ARGS=$@
nix run "nixpkgs#$PKG" -- "$ARGS"

You're squashing all your arguments after first into a single argument. That's probably not what you want.

$ (set -- '0 zero' '1 one' '2 two' '3 three'; shift; ARGS=$@; printf '%s\n' ">$ARGS<" ">$@<")
>1 one 2 two 3 three<
>1 one
2 two
3 three<
$