aboutsummaryrefslogblamecommitdiff
path: root/bin/shb
blob: 49894b0fa29fe250d3e0180b9ddf7709cc8e39d6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                             
                                                  










                                                                            
#!/bin/sh
# Use PATH to build a shebang for a script given on stdin
self=shb

# Need at least two arguments
if [ "$#" -lt 2 ] ; then
    printf >&2 '%s: Need input file and command\n' "$self"
    exit 1
fi

# First argument is the script (might be - for stdin), second argument is the
# name of the interpreter
scr=$1 intn=$2
shift 2

# Try and find the path to the interpreter command, bail out if we can't
if ! intp=$(command -v "$intn" 2>/dev/null) ; then
    printf >&2 '%s: %s: command not found\n' "$self" "$intn"
    exit 1
fi

# Set the positional parameters to the path and any remaining arguments, and
# squash them together for the shebang line
set -- "$intp" "$@"
printf '#!%s\n' "$*"

# Emit the rest of the input
cat -- "$scr"