Parameter problem in my script

Hi all

I hope that any one help me with my small problem :slight_smile:

My problem is:

inside my script their is a parameter x=example.tar.Z

I want to write a script to let y=example (without .tar.Z)

Thank you in advanced :slight_smile:

Try this

x=example.tar.z
y=`echo $x |cut -f 1 -d . `
echo $y

Useless use of echo and cut :slight_smile:

x=example.tar.z
y=${x%.tar.z}
echo $y

danmero, can you explain your code please
what "%" means here??
thanks in advance

You can retrieve (expand) variables value by enclosing the variable name in curly braces and preceding the left curly brace with a dollar sign, like that you eliminate the use of echo and backticks.
% will expand the variable removing the regexp(.tar.z) from the end of the variable value.

Regards,

Two things to note:

  1. The ${var%pattern} construct is a ksh (and perhaps bash) shell thing, not bourne shell or csh.
  2. The thing on the righthandside of the % is a pattern, not a regexp.

See SH(1) USER COMMANDS SH(1), do a search in the page for a percent sign. It's only about the 2nd or third entry down; you should find information about using this construct fairly readily.

I use Bourne shell(/vin/sh)sh

Yes, however you can use regexp.

y=${x%.*.?}

Bourne shell does not have ${var%pattern}. If you think you're using bourne shell, you're probably on Linux (sh points to bash).

Can you point to the docs saying that the pattern is actually a regexp? I doubt it... and I can't find it in the man pages.

FreeBSD sh , and I don't have bash.

Thank you all for your help :b:

On Solaris, using /bin/sh (definitely a good old-fashioned Bourne shell):

#!/bin/sh
x="hellothere"
y=${x%.*.?}
echo "x is: %x, y is: $y"

Gives:
bad substitution

Simplifying, by changing the ${x%.*.?} to ${x%there} gives the same response.

If I use ksh:

#!/bin/ksh
x="hellothere"
y=${x%there}
echo "x is: %x, y is: $y"

I properly get: x is: hellothere, y is: hello. Bash works, as well. Looks like it's pattern matching only.

But assuming that regular expressions are supported, I'll test

#!/bin/sh
x="hellothere"
y=${x%..*}
echo "x is: %x, y is: $y"

...This should match "1 of anything, followed by 0 or more instances of anything." That is, y should be completely empty. Testing with sh, ksh, and bash:

  • sh gives "bad substitution"
  • ksh gives "x is: hellothere, y is: hellothere"
  • bash gives the same

...So it doesn't work. If I assume the variable substitution is a pattern instead, that is I assume y=${x%..*} means "y equals x but remove a literal dot followed by a literal dot followed by anything (or nothing)", then let me change x to be something that such a pattern should match:

x="front..hellothere"

In this case, y should be "front". The results are:

  • sh I won't even try
  • ksh gives "x is: front..hellothere, y is: front"
  • bash gives "x is: front..hellothere, y is: front"

...That is, both ksh and bash work properly.

This shows that for Bourne shell, ksh88, and bash, this variable construct uses pattern matching, not regular expressions.

See Regular Expressions - a Simple User Guide for more information about regular expressions. See also The Regex Coach - interactive regular expressions for a very useful (Windows-based) tool.

There's a great explanation and examples of Korn shell variable expansion here.

InformIT: Variables and Parameters in the Korn Shell > Variable Expansion