output asked headers in the order they were asked; avoid header name spoofing by...
[hband-tools.git] / user-tools / cdexec
blobdc99c44f2f576bb773b50284565a5f2b3f4db482
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 cdexec - Run a given command in the given directory
10 =head1 SYNOPSIS
12 cdexec [--home | <B<DIRECTORY>>] [--] <B<COMMAND>> [<B<ARGS>>]
14 Run a given command in the given directory.
15 Set the target directory to the command's self directory if not given.
17 =head1 SEE ALSO
19 execline-cd by execlineb(1)
21 =cut
23 EOF
26 set -e
27 set -o pipefail
28 set -u
30 cd_into=''
32 while [ $# != 0 ]
34 case "$1" in
35 --help)
36 echo "Usage: $0 [--home | <DIRECTORY>] [--] <COMMAND> [<ARGS>]"
37 echo "Run a command in a specific directory."
38 echo "If DIRECTORY is not given and COMMAND is a path, chdir into COMMAND's directory."
39 exit
41 --)
42 shift
43 break
45 --home)
46 cd_into=$HOME
48 -*)
49 # ignore
50 true
53 if [ -z "$cd_into" ]
54 then
55 cd_into=$1
56 else
57 # command follows
58 break
61 esac
62 shift
63 done
65 if [ $# = 0 -a -n "$cd_into" ]
66 then
67 # no command given. probably the only parameter is meant to be the command, not the target directory.
68 set -- "$cd_into"
69 cd_into=''
72 if [ -z "$cd_into" ]
73 then
74 # target directory is the command's self directory
75 cmd=$1
76 if [[ $cmd =~ / ]]
77 then
78 cd_into=`dirname -- "$cmd"`
79 else
80 echo "cdexec: no target directory given." >&2
81 exit -1
85 cd "$cd_into"
86 exec "$@"