Remove last '1' in list of variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove last '1' in list of variables
# 1  
Old 10-29-2018
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:

Code:
#!/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
# 2  
Old 10-29-2018
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?
# 3  
Old 10-30-2018
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?
# 4  
Old 10-30-2018
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).
Code:
#!/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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh. So I have a... (8 Replies)
Discussion started by: DJR
8 Replies

2. Shell Programming and Scripting

Remove ^? characters in shell variables on using backspace

Friends, I observed a peculiar problem in shell. if I set a variable using standard input and backspace was used by the user, then the variable get ^? characters embedded in the variable. ### echo "Enter value for X=" read X echo $X expr $X + 1 ### If the variable is echoed, then there... (3 Replies)
Discussion started by: sachinverma
3 Replies

3. Shell Programming and Scripting

Need expect to read variables from a list while logged into the same device

Hi, I'm primarily a Cisco/Juniper networking guy, so you'll have to forgive my ignorance when it comes to scripting (although I do write simple backup scripts and things of that nature on a regular basis and I run Linux at home, so I am vaguely familiar with it). What I need to do should be... (2 Replies)
Discussion started by: wolverene13
2 Replies

4. Red Hat

List of all environment variables?

Hey all, I am simply trying to find a listing of all of the default BASH environment variables in RHEL 5.4. Namely, I need to find what the path variable is for libraries since one of my applications doesn't see a module that it needs to run. So far I've seen $LD_PRELOAD, $LD_LIBRARY_PATH and... (4 Replies)
Discussion started by: msarro
4 Replies

5. Shell Programming and Scripting

List file with variables expanded

Hello, I have this problem. I have script file, e.g. #!/usr/bin/ksh echo $MY_DIR ls -lt $MY_DIR I want to list the script but with MY_DIR variable expanded. E.g. MY_DIR=/abc/xyz (in shell MY_DIR is set) So I want to list the script and see: #!/usr/bin/ksh echo /abc/xyz ls... (6 Replies)
Discussion started by: r1omen
6 Replies

6. UNIX for Dummies Questions & Answers

For Loop for a list of tab delimited variables

Hello, I need to run a command for a set of input variables that are present in a tab delimited file, a sample of which is shown below: 1 3749 1 4129 1 5980 2 6201 2 9925 2 6894 3 1338 3 6477 3 6242 3 3632 Every row represents the two input values... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

7. Shell Programming and Scripting

Create variables from directory list problem

I am trying to take a list of directories in a folder and give them a variable name. I have this working with the exception that the shell_exec command wants to place a return. Here is my code which might explain better what I am trying to do: <?php session_start(); $student1=$_SESSION;... (0 Replies)
Discussion started by: robp2175
0 Replies

8. Shell Programming and Scripting

list file content as individual variables

Hello, I've created a couple of files within a list using the command "ls -ltr | tail -2 > list" These files are the newest files placed within a directory. From the "list" file, I need to place the filenames as a variable. In which the newest file will be called "new_ctrl" the older file... (4 Replies)
Discussion started by: petersf
4 Replies

9. UNIX for Dummies Questions & Answers

Using sed to remove paragraphs with variables

Hi everyone, I have a file with multiple entries and I would like to remove the ones that contain either /A"> or /A/, where A can be any letter of the alphabet. Here's an example of the entries: <Topic r:id="Top/World/Fran"> <catid>476</catid> <link... (1 Reply)
Discussion started by: BlueberryPickle
1 Replies

10. UNIX for Dummies Questions & Answers

Too many files to list / remove

I have a directory which has 614,000 files. When attempting to do an ls -ralt in the directory an error too many arguments is shown. 1. I would like to see what files and their stats in the directory 2. I would like to delete certain files of a certain age (3 Replies)
Discussion started by: dinplant
3 Replies
Login or Register to Ask a Question