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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

# This script is an attempt to provide some subset of the 'open'
# command found in MacOS X for the other UNIX variants I use.  The
# basic idea behind the 'open' command is that it figures out what
# application should be used to open a file based upon the file's
# type.  For example, 'open foo.jpeg' would open up foo.jpeg with
# 'Preview.app' on a mac, so this script, on UNIX, would open up
# foo.jpeg with 'xv'.

# Figure out file name and 'application'
if test "$1" = "-a"
  then # have -a option
    file_name=$3
    appl_name=$2
    # Test for no application given with -a flag..
    if test -z "$appl_name"
     then
       echo "Use: open [-a application] file"
       exit
    fi
    # Test for extra arguments (we only work with one file argument)
    if test "$4"
     then
       echo "Use: open [-a application] file"
       exit
    fi
  else  # no -a option
    file_name="$1"
    appl_name=
    # Test for extra arguments (we only work with one argument)
    if test "$2"
     then
       echo "Use: open [-a application] file"
       exit
    fi
fi

# Check for a file name
if test -z "$file_name" 
  then
    echo "Use: open [-a application] file"
    exit
fi

# if we have an application, then we are done
if test "$appl_name"
  then
    $appl_name $file_name
    exit
fi

# Figure out if we have an extension
if echo $file_name | grep '\.'
  then
    ext=`echo $file_name | sed 's/^.*\.//'`
    #echo File: $file_name
    #echo Ext: $ext
  else
    echo "don't know how to open $file_name (no extension)"
    echo "Use: open [-a application] file"
    exit
fi

# Figure out what to run the file with, or give up.
case $ext in
    pdf) 
        xpdf "$file_name" &
        ;;
    png|gif|jpeg|jpg|xwd|fits) 
        pqiv "$file_name" &
        ;;
    c|cc|cxx|cpp|h|f77|R|sh|pl|cl|el|scm|sql) 
        emacs "$file_name" &
        ;;
    txt|tex) 
        emacs "$file_name" &
        ;;
    dvi)
        xdvi "$file_name" &
        ;;
    ps|eps) 
        gv "$file_name" &
        ;;
    gz) 
        gunzip "$file_name"
        ;;
    html|htm) 
        lynx "$file_name"
        ;;
    tar)
        tar xvf "$file_name"
        ;;
    *) 
        echo "Don't know how to open files with extension: '$ext'"
        echo "Use: open [-a application] file"
        exit
        ;;
esac