output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / chshebang
blob55c396f1bb53c427bb8971a0ea497a6d2d930f7f
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 chshebang - Change a script's default interpreter
10 =cut
12 EOF
15 force=0
16 insert=0
17 dummy=0
18 NL=$'\n'
20 while [ -n "$1" ]; do
21 case "$1" in
22 -h|--help)
23 echo "Usage: $0 [-h|--help] [-f|--force] [-i|--insert] [-d|--dummy] <INTERPRETER> <FILES> ...
24 -f force replace 1st line with INTERPRETER, even if it does not start \`#!' signature
25 -i insert, instead of replace
26 -d disable intelligent path preparation
27 INTERPRETER basename or full path of new interpreter, leading \`#!' signature is implicite
28 examplpes: /usr/bin/awk, bash, #!/bin/sh
29 FILES files to modify"
30 exit 0;;
31 -f|--force)
32 force=1;;
33 -i|--insert)
34 insert=1;;
35 -d|--dummy)
36 dummy=1;;
37 -*) echo "Unknown option: $1" >&2
38 exit 1;;
39 --) shift
40 break;;
41 *) break;;
42 esac
43 shift
44 done
47 shebang=$1
48 shift
50 if [ $dummy = 0 ]; then
51 if [ "${shebang:0:2}" != '#!' ]; then
52 if [ "${shebang:0:1}" != '/' ]; then
53 p=`which "$shebang"`
54 if [ $? = 0 ]; then
55 shebang=$p
56 else
57 echo "Command not found: $shebang" >&2
58 exit 2
61 shebang="#!$shebang"
65 # escape sed(1) meta chars in this order: \ , &
66 shebang=${shebang//\\/\\\\}
67 shebang=${shebang//,/\\,}
68 shebang=${shebang//&/\\&}
70 if [ $insert -eq 1 ]; then
71 for file; do
72 sed -n "1i$shebang${NL}p" -i "$file"
73 done
75 elif [ $force -eq 1 ]; then
76 for file; do
77 sed -n "1s,^.*\$,$shebang,;p" -i "$file"
78 done
80 else
81 for file; do
82 sed -n "1s,^#!/.*\$,$shebang,;p" -i "$file"
83 done