Different behaviour when running script as source file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Different behaviour when running script as source file
# 1  
Old 06-23-2009
Different behaviour when running script as source file

Hi all,

OS: Solaris 10

I've written a script to scan the oratab file and give me a list of Oracle SIDs to choose from, and obviously in order to set the ORACLE_SID environment variable from within a script for the current shell, the script must be run as a source file.

When I run it as a normal file, i.e. not a source file, it works fine:
Code:
$ ./orasid
Choose Oracle SID:
1 SIUAT
2 RMAN
SID: 1 <-- Entered 1 here 
no of array elements: 2
ORACLE_SID set to: SIUAT

..although the env var is not actually set, as expected

but when I run it as a source file I get this:
Code:
$ . ./orasid
Choose Oracle SID:
1 SIUAT
2 RMAN
SID: 1
no of array elements: 0
Enter 1 to 0, SID not set

The problem seems to be the ${#ORACLE_SID[@]} construct to establish the total number of array elements. But why does this work as a regular shell script and not as a source file? Is there an alternative which works with both?

Thanks in advance, and here's the script:

Code:
#
# orasid - scan oratab for Oracle DBs and set SID
#
# usage: . orasid
#
#####################################
#
# Determine how to suppress newline with echo command.
#
N=
C=
if echo "\c" | grep c >/dev/null 2>&1; then
    N='-n'
else
    C='\c'
fi

ORATAB=/var/opt/oracle/oratab
#
# Loop through ORATAB
#
i=0
echo Choose Oracle SID:

cat $ORATAB | while read LINE
do

  case $LINE in
    
\#*)                ;;        #comment-line in oratab

*)
      (( i=i+1 ))
      poss_sid=`echo $LINE | awk -F: '{print $1}' -`
      if [ ! "$poss_sid" = '*' ] ; then  # ignores NULL SID
          ORACLE_SID[i]=$poss_sid
          echo $i ${ORACLE_SID[i]}
      fi
    ;;
  esac

done

echo $N "SID: $C"
read usrsid
echo no of array elements: ${#ORACLE_SID[@]}

if [ ${usrsid} -gt ${#ORACLE_SID[@]} ]; then
  echo Enter 1 to ${#ORACLE_SID[@]}, SID not set
else
  ORACLE_SID=${ORACLE_SID[usrsid]}
  export ORACLE_SID
  echo ORACLE_SID set to: ${ORACLE_SID[usrsid]}
fi


Last edited by longjon; 06-24-2009 at 07:23 AM.. Reason: add code tags
# 2  
Old 06-24-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

For instance, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Thank You.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script Comment code blocks in a bash source file

Just began to learn on Shell Script. I got an exercise from my friend. I know how to make this happen in C, but I'm not familiar with Shell Script. Hope I can get some help from all of you. I want to write a bash script to comment code blocks in a bash source file. What I mean comment is '#', I... (1 Reply)
Discussion started by: HiFuture0801
1 Replies

2. Shell Programming and Scripting

Unable to source file in my script

Hi, ls -ltrq res.file -rwxrwxr-x 1 ora install 4278 Nov 30 07:19 res.file $ more test.sh source res.file $ ./test.sh ./test.sh: .: res.file: cannot open $ id uid=600000014(ora) gid=63855(install) uname -a SunOS mymac 5.11 11.2 sun4v sparc sun4v The same thing works fine on a... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. UNIX for Advanced & Expert Users

Rsync error while running from destination to source

hi All, i have 2 server setup now for Rsync, i configured Rsync on both of the server and it worked well when i did run from source to destination. and while running back from destination to source it produced this error: bash-3.2$ ksh rsync_bravo_db.ksh usa0300uz1247.apps.mc.xerox.com... (0 Replies)
Discussion started by: lovelysethii
0 Replies

4. Shell Programming and Scripting

Script to insert a function into a C source file

Hello, I think I need sed, but perhaps awk could help. I am trying to add a function to a C source file based off a struct declaration. for example: struct Rational { int numerator; int denominator; }; becomes struct Rational (4 Replies)
Discussion started by: afulldevnull
4 Replies

5. Shell Programming and Scripting

ksh script to create a generic csv file from different source formats

Hi all, I have a requirement to create a "superset" file out of a number of different sources with some different and some same columns. We intend to have a manually updateable SuperSetCols.csv which would look like "ColA","ColB","ColC","ColD","ColE","ColF","ColG" so someday we may add... (3 Replies)
Discussion started by: Leedor
3 Replies

6. UNIX for Advanced & Expert Users

Automated SCP script passing password to preserve source file timestamp

Hi My requirement is i want to copy files from remote server to the local server and also i need to preserve the timestamp of the remote file. By using scp -p , it is working fine in the interactive call but it is not preserving he file timestamp when i use it in the non interactive scp call... (1 Reply)
Discussion started by: skumar75
1 Replies

7. Shell Programming and Scripting

Weird script behaviour !

Hello, I am getting an infinite loop from a script in Linux. Here is the last version of the script in question. As you can see I tried to define everything properly: #!/bin/ksh # Script to loop over a series of dates set -ex typeset -i start_date=20090701 typeset -i... (2 Replies)
Discussion started by: stavros
2 Replies

8. Shell Programming and Scripting

source a property file in script give error

I have sourced a property file in my script like this to load some variables in the script Then i am able to echo all the property file values inside script but the script is not able to recognize other unix commands #!/bin/bash . build.properties mkdir build i am getting error ... (3 Replies)
Discussion started by: codeman007
3 Replies

9. Shell Programming and Scripting

Source Env file in the Shell Script

Hi I am having a script which sets the application environment. In this script i am sourcing the applications env file, when i am debugging the script i see its executing all the environment values and all the variable values are set properply. Once this main shell script executes, then... (4 Replies)
Discussion started by: umakanthly
4 Replies

10. Shell Programming and Scripting

Tidy shell script source file

Hi all, Is there a tool to tidy up shell script source file and reformat it. Preferably I am looking for a perfect tool to do this. Also I have heard it is possible to do with emacs editor but unfortunately it is looking very different and new from vi. Emacs is new to me and I could not find... (1 Reply)
Discussion started by: meharo
1 Replies
Login or Register to Ask a Question