dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

Post by Thomas F » Fri, 09 Nov 2001 11:07:12



This is from a Bash shell script.  The purpose is to get at the base
name of the script (which is contained within the script variable $0),
using the regular expression mode of expr.

.* matches 0 to infinity (the *) chars (the .) except for a newline,
and the escaped parentheses are needed to encapsulate that expression
for some reason. \.sh$ specifies ".sh" occurring at the end of a line
(escape needed to specify a literal .).

Please explain how this strips off the .sh extension.  If you enter
dowhat.sh in Emacs, and search using this regular expression, it
happily matches the whole shebang, whereas as used in the script, the
extension is subtracted!

Good regular expressions tutorial:
http://www-105.ibm.com/developerworks/education.nsf/linux-onlinecours...

 
 
 

dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

Post by Jim Patterso » Fri, 09 Nov 2001 12:19:01


 > This is from a Bash shell script.  The purpose is to get at the base name
 >  of the script (which is contained within the script variable $0), using
 >  the regular expression mode of expr.
 >
 > .* matches 0 to infinity (the *) chars (the .) except for a newline, and
 >  the escaped parentheses are needed to encapsulate that expression for
 >  some reason. \.sh$ specifies ".sh" occurring at the end of a line
(escape
 >  needed to specify a literal .).
 >
 > Please explain how this strips off the .sh extension.  If you enter
dowhat.sh
 >  in Emacs, and search using this regular expression, it happily
 > matches the whole shebang, whereas as used in the script, the extension
 >  is subtracted!

Run 'info expr' for an explanation of why this works.

`STRING : REGEX'
      Perform pattern matching.  The arguments are converted to strings
      and the second is considered to be a (basic, a la GNU `grep')
      regular expression, with a `^' implicitly prepended.  The first
      argument is then matched against this regular expression.

      If the match succeeds and REGEX uses `\(' and `\)', the `:'
      expression returns the part of STRING that matched the
      subexpression; otherwise, it returns the number of characters
      matched.

Emacs does something different with sub-expressions, and supports more
than one. In Emacs, you can retrieve the part matched by subexpression 1
using something like this:
     (buffer-substring (match-beginning 1) (match-end 1))
and similarly for subexpressions 2, 3 ... if you have them.

By the way, in bash I find this a much simpler way to strip an extension:

    dowhat=${0%.sh}

which sets 'dowhat' to the variable $0 minus any trailing portion
matching .sh . Note that .sh is taken as a filename expression here, not
as a regular expression (i.e. the dot is not a wildcard).

 > Good regular expressions tutorial:
http://www-105.ibm.com/developerworks/education.nsf/linux-onlinecours...
 >

 >

--
----
Jim Patterson
Ottawa, Ont
CANADA

 
 
 

dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

Post by Thomas F » Fri, 09 Nov 2001 16:11:10


Thanks, but the RedHat documentation doesn't give all that
information!  This does however:
http://www.mkssoftware.com/docs/cmd_index.asp

I'm drunk with my new-found power!.. although I imagine there's a
cleaner way to do this, also.

# Get the directory of the script as invoked by the user & form a
#   fully-specified directory name

# installer_dir=`dirname $0`;
###
echo "0 installer_dir=\"$installer_dir\""

# get rid of any ./././'s

savethis="\(.*\)"
zapdir="\./"      # i.e., ./
i=0

while [ `expr $installer_dir : ".*/$zapdir"` != 0 ]
do
    i=`expr $i + 1`

    first=`expr $installer_dir : "$savethis/$zapdir"`
    remaining=`expr $installer_dir : ".*/$zapdir$savethis"`
    installer_dir=$first/$remaining
    echo "$i installer_dir=\"$installer_dir\""
done

# form absolute path if relative path was entered

if [ `expr $installer_dir : "\."` != 0 ]

# zap any leading ./

    if [ `expr $installer_dir : $zapdir` = 2 ]; then
        installer_dir=`expr $installer_dir : "$zapdir$savethis"`
        echo "installer_dir=\"$installer_dir\""
    fi

echo

# remove ../../../ from beginning of installer_dir
#    & corresponding number of directories from end of pwd

    updir="\.\./"         # i.e., ../
    pwd=`pwd`
    echo "pwd=\"$pwd\""

    while [ `expr $installer_dir : "$updir"` = 3 ]
    do
        installer_dir=`expr $installer_dir : "$updir$savethis"`
        echo "installer_dir=\"$installer_dir\""

        pwd=`expr $pwd : "$savethis/"`
        echo "pwd=\"$pwd\""
    done

# form the final directory string

    installer_dir=$pwd/$installer_dir
    echo "installer_dir=\"$installer_dir\""
fi

 
 
 

dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

Post by bowma » Fri, 16 Nov 2001 14:56:51



> Please explain how this strips off the .sh extension.??If?you?enter
> dowhat.sh in Emacs, and search using this regular expression, it
> happily matches the whole shebang, whereas as used in the script, the
> extension is subtracted!

the parens are used to contain a substring of the match. So, it there is
a match, the substring contains everything up to the literal '.sh', or
'dowhat' in this case. It is this substring that is returned from expr.

I haven't looked at the regex reference you cited, but it should talk about
substrings or subexpressions and using \1, \2. \1 is the string in the
first \(  \) , usw, usually up to \9. So, in those terms evaluating the
expression returns \1 implicitly.

 
 
 

dowhat=`expr "dowhat.sh" : "\(.*\)\.sh$"`;

Post by hairetik » Sun, 18 Nov 2001 13:26:59




> > Please explain how this strips off the .sh extension.??If?you?enter
> > dowhat.sh in Emacs, and search using this regular expression, it
> > happily matches the whole shebang, whereas as used in the script, the
> > extension is subtracted!

> the parens are used to contain a substring of the match. So, it there is
> a match, the substring contains everything up to the literal '.sh', or
> 'dowhat' in this case. It is this substring that is returned from expr.

> I haven't looked at the regex reference you cited, but it should talk about
> substrings or subexpressions and using \1, \2. \1 is the string in the
> first \(  \) , usw, usually up to \9. So, in those terms evaluating the
> expression returns \1 implicitly.

Ah.  sed-ish stuff.

Wouldn't it all be easier with $(basename $ext) ?

--
 Any technology distinguishable from magic is insufficiently advanced.
     TopQuark Software & Serv.  Contract programmer, server bum.