Space in PATH variable

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Space in PATH variable
# 8  
Old 07-05-2017
Quote:
Originally Posted by bdby
My oratab in /var/opt/oracle/oratab reads the following:
Code:
  
SGCD:/export/home/oracle/app/oracle/product/12.1.0/dbhome_2:N

and I am using the following command to cut it from the oratab file:
Code:
 
ORACLE_HOME=`cat /var/opt/oracle/oratab|grep -v "^#"|cut -d ':' -f2`

Welcome to the world of scripting and its infinite possibilities. Let us go step by step, because you do several things wrongly here (as does every beginner - don't worry).

1) DO NOT use backticks - never! They are a leftover from a very old shell named "Bourne shell", to which bash is downwards compatible, hence the name "Bourne-again-shell". Bourne shell was the standard shell for UNIX systems in the early eighties, but these times are gone for some time now. Since you use bash you should use the modern way of "process substitution", which reads:
Code:
ORACLE_HOME=$(cat /var/opt/oracle/oratab|grep -v "^#"|cut -d ':' -f2)

2) You made the error of "useless use of cat". grep can read a file by itself and you don't need to dump the file contents into a data stream using cat. You can simply write:
Code:
ORACLE_HOME=$(grep -v "^#" /var/opt/oracle/oratab|cut -d ':' -f2)

This may look like a small point, but in fact it needs one process less to start. Starting a process is taxing on the resources of a system and while a single process more or less won't hurt at all if you use such a construct in an often-run loop the effects are considerable. It is never too early to start good habits and if you start doing it right in little scripts like this you will do it right once you start writing big ones either.

3) The grep command might not do what you think it does: grep filters lines. In your case you filter all lines starting with the character "#" - in fact you reversed this filter, so all lines NOT starting with "#" show up. But i.e empty lines or every other line not starting with "#" will show up too. I don't know your oratab file, but my suspicion is that your problem comes from there. Whenever you create regular expressions work on the following checklist:

- does the regex match the correct lines/text parts? (Obvious)
- are there misses i want matched? (false negatives)
- are there matches i did not want to match? (false positives)

Even the most seasoned creators of regular expressions will not always get it right on the first try. The trick is to be persistent and try to imagine the cases mentioned in the checklist above and then modify the regexp accordingly until satisfaction is reached.

In your case i suppose you are not interested in "any non-comment lines", but in a specific line - the one starting with "SGCD", as rbatte1 already suggested. So your line should probably read:

Code:
ORACLE_HOME=$(grep '^SGCD:' /var/opt/oracle/oratab|cut -d ':' -f2)

4) in your scripts try to be as robust as possible. That is: try to anticipate possible problems and deal with them. Suppose, for instance, the oratab file does not contain a line starting with "SGCD:". Then the ORACLE_HOME would be empty and your PATH statement (supposing the rest works correct) would look like:

Code:
PATH=/path/one:/path/two::

You could avoid this by testing if a line you search for is even present at all and stop processing if it isn't:

Code:
if grep -q '^SGCD:' /var/opt/oracle/oratab ; then
           ORACLE_HOME=$(grep '^SGCD:' /var/opt/oracle/oratab|cut -d ':' -f2)
else
      echo "no SGCD-line found in oratab"
fi

and, finally, a last point: whenever you use grep and then cut you could use sed instead which can do alone what these two do combined:

Code:
ORACLE_HOME=$(sed '/^SGCD:/ s/^[^:]*:\([^:]*\):.*$/\1/' /var/opt/oracle/oratab)

I hope this helps.

bakunin
# 9  
Old 07-05-2017
And again, sed by default reports the non-matching lines.
Quick fix:
Code:
ORACLE_HOME=$(sed -n '/^DGCS:/ s/^[^:]*:\([^:]*\):.*$/\1/p' /var/opt/oracle/oratab)

-n suppress default print
/p print if a match was found
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 07-05-2017
Quote:
Originally Posted by MadeInGermany
And again, sed by default reports the non-matching lines.
Yes, indeed. This was what i had in mind, but didn't write. Thank you for correcting it.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

2. Shell Programming and Scripting

Setting path variable with a space.

Hi I am using MKS Toolkit c shell. I am trying to set a path variable something like c:/Program Files/blah/blah so set path=(c:/Program Files/blah/blah) this, however, does not work as it splits this thing up into 'c:/Program' and 'Files/blah/blah'. Does anyone have any ideas on... (9 Replies)
Discussion started by: vas28r13
9 Replies

3. Shell Programming and Scripting

Path a variable to sed that includes a path

Hi I'm trying to select text between two lines, I'm using sed to to this, but I need to pass variables to it. For example start="BEGIN /home/mavkoup/data" end="END" sed -n -e '/${start}/,/${end}/g' doesn't work. I've tried double quotes as well. I think there's a problem with the / in the... (4 Replies)
Discussion started by: mavkoup
4 Replies

4. Shell Programming and Scripting

Appending a path in user's PATH variable

Hello Folks, I want to append a path in user's PATH variable which should be available in current session. Background Numerous persons will run a utility. Aim is to add the absolute path of the utility the first time it runs so that next runs have the PATH in env & users can directly run... (6 Replies)
Discussion started by: vibhor_agarwali
6 Replies

5. Shell Programming and Scripting

one liner to extract path from PATH variable

Hi, Could anyone help me in writing a single line code by either using (sed, awk, perl or whatever) to extract a specific path from the PATH environment variable? for eg: suppose the PATH is being set as follows PATH=/usr/bin/:/usr/local/bin:/bin:/usr/sbin:/usr/bin/java:/usr/bin/perl3.4 ... (2 Replies)
Discussion started by: royalibrahim
2 Replies

6. Shell Programming and Scripting

remove a path from PATH environment variable

Hi I need a script which will remove a path from PATH environment variable. For example $echo PATH /usr/local/bin:/usr/bin:test/rmve:/usr/games $echo rmv test/rmve Here I need a shell script which will remove rmv path (test/rmve) from PATH... (9 Replies)
Discussion started by: madhu84
9 Replies

7. Shell Programming and Scripting

Need to execute a command on a path with a space in it...

Hello, I need to execute the following command in a script: /usr/bin/ssh 205.21.1.1 vmware-cmd -v /home/virtual machines//Machine.vmx"createsnapshot Weekly_Backup >/dev/null 2>&1 The problem is that there is a space between virtual and machines and when I run the script I get no such... (2 Replies)
Discussion started by: mojoman
2 Replies

8. Shell Programming and Scripting

sftp how to deal with space in path to dir?

OK I am trying to use something similar to this: #!/bin/sh echo "OK, starting now..." sftp -b /dev/fd/0 user@remotehost <<EOF cd pub ascii put filename.txt bye EOF only difference is the dir I need to cd to has a space in it like this /Import/Server/Prospect File ,... (3 Replies)
Discussion started by: NewSolarisAdmin
3 Replies

9. Shell Programming and Scripting

Problems with Space at path

Hi everyone, I create script for seaching count of differrent file types at my FS. My script: #! /bin/sh #OLD_IFS=$IFS #export IFS=$(echo "\n\t ") echo "Wait a minute... Seaching is performed" d=0;b=0;c=0;p=0;s=0;l=0;f=0 non=0 for fl in $(find /opt/google/picasa/3.0/wine/drive_c... (8 Replies)
Discussion started by: babi4
8 Replies

10. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies
Login or Register to Ask a Question