Skip to content

Commit

Permalink
update shabang
Browse files Browse the repository at this point in the history
  • Loading branch information
albertodonato committed Nov 19, 2024
1 parent 04440ce commit de321cb
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions bin/shebang
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
#!/bin/bash -e
#!/bin/bash
#
# Create a script with the specified name.
# Create a script with the specified name and interpreter.

set -e -o pipefail

SCRIPTNAME="$(basename "$0")"

SCRIPTNAME="$(basename "$0")"

filename="$1"
if [ -z "$filename" ]; then
echo "Usage: $SCRIPTNAME <filename> [shebang]" >&2
echo "Usage: $SCRIPTNAME <filename> [interpreter]" >&2
exit 1
fi

shift 1

shebang="/bin/bash -e"
interpreter="${2:-bash}"

if [ $# -gt 0 ]; then
shebang="$*"
if [[ "$shebang" != /* ]]; then
shebang="/usr/bin/env $shebang"
if [[ "$interpreter" = /* ]]; then
header="#!$interpreter"
else
header="#!/usr/bin/env $interpreter"
if [ "$interpreter" = "bash" ]; then
header="$header\n\nset -e -o pipefail"
fi
fi

line="#!$shebang"

touch "$filename"
if [ "$(stat --printf="%s" "$filename")" -gt 0 ]; then
sed -i '1 s,.*,'"$line", "$filename"
else
echo -e "$line\\n" > "$filename"
fi
echo -e "$header\n" > "$filename"
chmod +x "$filename"

0 comments on commit de321cb

Please sign in to comment.