Remove last '1' in list of variables

Hi folks,

I have a list of variables as follows:

CDBTEST1
messdba1
sat11cru1
s12tgts1
sa12ss1

I need to remove the last '1' so I can use the remaining variables in a for loop:

CDBTEST
messdba
sat11cru
s12tgts
sa12ss

Something like this:

#!/bin/sh
for INSTANCE in 
CDBTEST1
messdba1
sat11cru1
s12tgts1
sa12ss1

do
. oraenv
$INSTANCE
$DBNAME = $INSTANCE - trailing 1  <-- how to get rid of the 1 to leave $DBNAME
commands
done

Thanks for any help!

jd

Any attempts / ideas / thoughts from your side?

Do you want to remove the verbatim 1 at the end of EACH lne in that file, or just the last character, whatever it might be?

As always, when starting a thread in the Shell Programming and Scripting forum, it helps to know what operating system and shell you're using.

If /bin/sh on your system is a pure Bourne shell from the 1980's, you might want to use something like expr 's : operator to return a string matching everything except a trailing 1 .

If /bin/sh on your system is a modern shell supporting the parameter expansions specified by the POSIX standards, using a parameter expansion to remove a trailing 1 would be much simpler, faster, and more efficient than using expr .

Are we supposed to assume that the shell variables CDBTEST , CDBTEST1 , messdba , messdba1 , sat11cru , sat11cru1 . s12tgts , s12tgts1 , sa12ss , and sa12ss1 are defined by oraenv ?

Also the for loop list needs to be in a certain format.
I usually put the values in a string and assign it to a variable, then let the shell expand the variable (via IFS i.e. white space and newline).

#!/bin/sh
values="
CDBTEST1
messdba1
sat11cru1
s12tgts1
sa12ss1
"
for INSTANCE in $values
do
  . oraenv
  # A Posix shell can delete a trailing 1 like this:
  DBNAME=${INSTANCE%1}
  # The old Bourne shell needs an external program:
  # DBNAME=`expr "$INSTANCE" : "\(.*[^1]\)"`
  echo "$INSTANCE -> $DBNAME"
done