File: test-argv0.sh

package info (click to toggle)
scratchbox2 2.2.4-1debian1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 5,392 kB
  • ctags: 5,239
  • sloc: ansic: 21,734; sh: 4,360; perl: 2,170; cpp: 1,913; makefile: 610; python: 184
file content (46 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# argv[0] is passed correctly to scripts and executables
set -e


ASH=test-argvsh
ABIN=test-argvbin
GREP='grep -q'

cat > $ABIN.c <<EOF
#include <stdio.h>
int main(int argc,char **argv) {
printf("%s\n",argv[0]);
}
EOF

cat > $ASH <<EOF
#!/bin/sh
echo "\$0"
EOF

function failwith {
echo Failure in: $*
return 1
}

chmod +x ./$ASH
gcc -o $ABIN $ABIN.c

# Relative path
./$ASH | $GREP ^\./$ASH$ || failwith 'Relative invoking of shell'
./$ABIN | $GREP ^\./$ABIN$ || failwith 'Relative invoking of binary'

# Absolute path
$PWD/$ASH | $GREP ^$PWD/$ASH$ || failwith 'Absolute invoking of shell'
$PWD/$ABIN | $GREP ^$PWD/$ABIN$ || failwith 'Absolute invoking of binary'

mkdir -p argv0
DIR=$PWD
cd argv0
# Lookup from path with ..
PATH=$PATH:.. $ASH | $GREP ^\../$ASH$ || failwith 'Relative paths in PATH with shell'
PATH=$PATH:.. $ABIN | $GREP ^$ABIN$ || failwith 'Relative paths in PATH with binary'

# Lookup from path with absolute path
PATH=$PATH:$DIR $ASH | $GREP ^$DIR/$ASH$ || failwith 'Absolute paths in PATH with shell'
PATH=$PATH:$DIR $ABIN | $GREP ^$ABIN$ || failwith 'Absolute paths in PATH with binary'