This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: using aliases for cygwin commands on win2k


Hello,

Here are a couple of bash functions that I use to open
files from cygwin. The first function is what I found
on the web a while back, the second one is my
customization.

You can then do things like:
of foo.doc ../bar.xls 

Jean-Marc 

#-*-shell-script-*-
# open any file in the correct program: an associated
file-type (see assoc
# and ftype at Windows NT/2000) will be opened with
the associated program.
# Not associated filetypes and files which extension
is contained in
# $PATHEXT will be opened in the texteditor (here
Emacs). Files with
# extension .exe and .com will not be opened!

function open-file-with-w32 ()
{
	for i in $*; do
		if [ -f "$i" ]; then
			# get the extension only
			erg=`echo $i | sed -e 's|.*.||g;'`
			# check if we try to open a program
            if [ "$erg" != "exe" ] &&
               [ "$erg" != "EXE" ] &&
               [ "$erg" != "com" ] &&
               [ "$erg" != "COM" ]; then
                # check if we try to open a file with
a extension from $PATHEXT
                is_path_ext=`echo "$PATHEXT"; | grep
-i ".$erg;"`
                if [ "$is_path_ext" == "" ]; then
                    # check if this extension is
associated with a w32-program
                    erg=`cmd /x /c assoc ".$erg"
2>/dev/null`
                    if [ "$erg" != "" ]; then
                        # an association exists
therefore this call should
                        # theoretically not produce an
error.
                        cmd /x /c start /b `cygpath -w
"$i"` 2>/dev/null
                    else
                        # we must open the file with a
text editor because
                        # it is not associated.
                        gnuclient -q `cygpath -w "$i"`
                    fi
                else
                    # files with a extension contained
in $PATHEXT will be
                    # opened with the text editor.
                    gnuclient -q `cygpath -w "$i"`    
       
                fi
            else
                echo "$i is a program!"
            fi
        fi
    done
    unset erg is_path_ext;
}


function fast-open-file-with-w32 ()
{
	# Loop through the parameters, which works better
	# with spaces in path or filename.
	while [ $# -ne 0 ]
	do
		# get the extension only, in lower case
		erg=`echo $1 | sed -e 's|.*.||g;' | tr [:upper:]
[:lower:]`

		case $erg in
			exe|com)
				# We try to open a program
				echo "$1 is a program!"
				;;
			txt|bat|cmd|vbs|vbe|js|jse|wsf|wsh)
				# I make a special case for the text extensions,
				# to allow creating new text file.

				# This works for the native XEmacs:
				# gnuclient -q `cygpath -w "$1"`
				gnuclient -q "$1" # For the cygwin XEmacs
				;;

			$1)
				# There is no extension (i.e. erg == filename)
				# We open with XEmacs (could be a script).
				gnuclient -q "$1" # For the cygwin XEmacs				
				;;
			*)
				# check if this extension is associated with a
w32-program
				erg=`cmd /x /c assoc ".$erg" 2>/dev/null`
				if [ "$erg" != "" ]
				then
					# Windows does not allow to open a new file this
way...
					if [ -f "$1" ]
					then
						# an association exists therefore this call
should
						# theoretically not produce an error.
						# -a to run with remote paths.
						# -s to allow spaces in path or filename (grrrr)
						#echo "Use the Windows association for"
						cmd /x /c start /b `cygpath -wsa "$1"`
2>/dev/null
					else
						echo "This command cannot create the new file
$1"
					fi
				else
					echo "Invalid extension for $1"
					# gnuclient -q "$1"
				fi
				;;
		esac

		#Deal with the next argument
		shift

	done
}


alias of=fast-open-file-with-w32


__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]