Shell script to connect


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to connect
# 8  
Old 02-19-2016
As an idea (completly untested):
Code:
#!/bin/bash
#
#	Variables
#
	OLD_PATH=$PATH
	case "$(uname)" in
	   *SunOS*) ORATAB=/var/opt/oracle/oratab
		    ;;
	   *)       ORATAB=/etc/oratab
		    ;;
	esac
#
#	Functions
#
	get_ora_file() {
	# Try to locate the file, if locate is installed
	# Otherwise try find
		if which locate 2>/dev/null >&2
		then	list=$(locate oratab)
		else	list=$(for D in etc opt var $HOME/{etc,opt,var} usr/local/{etc,opt,var} ;do find /$D/ -name oratab 2>/dev/null)
		fi
		[ -z $list ] && return 1
		# Make it selectable if multiple found
		count=$(echo $list | wc -w)
		if [ ${count:-0} -gt 1 ]
		then	select output in $list;do echo $output;return 0;done
		elif [ ${count:-0} -eq 1  ]
		then	echo $list
		else	echo "No oratab could be found"
			return 1
		fi
	}
#
#	Preps
#	
	# Check for default file, if missing, call function, but if the function fails, exit the script
	[ ! -f $ORATAB ] && \
		echo "File $ORATAB not at default location!" >&2 && \
		{ ORATAB=$(get_ora_file) || exit 1 ; }
	grep -v "^#" $ORATAB |while IFS=":" read ORACLE_SID ORACLE_HOME AUTOSTART
	do
		if  ps -eaf |grep "pmon[_]$ORACLE_SID" >/dev/null; then
			echo "My db  : $ORACLE_SID"
			echo "My home: $ORACLE_HOME"
			PATH=$ORACLE_HOME/bin:$OLD_PATH
			sqlplus -s / as sysdba <<-EOF
				select instance_name,status,database_status from v\$instance;
				select * from v\$version;
			EOF
		fi
	done

As another apporach, if you dont know where those files are, you could try finding them, and apply them to a case statement:
Code:
get_oratab() { # SERVER
# Parse $1 
# and return its according value
	# Make string all small chars
	case "${1,,}" in
	*bd1)	str="/etc/oratab"	;;
	*bd2)	str="/path/oratab"	;;
	*bd3)	str="/path/of/oratab"	;;
	*)		echo "Input \"$1\" is invalid!"
			return 1
			;;
	esac
	echo $str
	return 0
}

ps -fea |grep [p]mon | while read _ _ _ _ _ _ _ ORACLE_SID
do
	ORATAB=$(get_oratab $ORACLE_SID)
	echo "myDB: $ORACLE_SID"
	echo "myHome: $ORATAB"
done

Hope this helps
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to connect to Oracle server from shell script

Hi, I am facing an issue in connecting to database from Linux shell script. Here is my code export ORACLE_BASE=xxx export ORACLE_SID=xxx export ORACLE_HOME=xxxx DB_PATH=xxx/'yyy@yy' UserId=`sqlplus -s $DB_PATH <<EOF set head off feedback off select max(USER_ID)+1 from USERS;... (6 Replies)
Discussion started by: yuvi
6 Replies

2. Shell Programming and Scripting

How to Connect to Teradata and Run in Shell Script?

Hi Master, I need to create dan run script in linux to get data from teradata I did this ---------- Post updated at 02:28 AM ---------- Previous update was at 02:25 AM ---------- LOG_DIR="/home/"; OUTDIR="/home/"; SCRIPT_NAME=“test"; DUMPFILE="${OUTDIR}${SCRIPT_NAME}"_"$(date... (2 Replies)
Discussion started by: radius
2 Replies

3. Shell Programming and Scripting

Shell script to connect from one server to other

Dear Experts, I am new to the shell scripting. I am looking for a shell script to connect to one Unix/Linux server1 to other Unix/Linux server2 and trigger a SAP Event in that server2 (Which will trigger a job in SAP). Is this possible to connect from one server to the other server securely... (7 Replies)
Discussion started by: Venu V Reddy
7 Replies

4. Shell Programming and Scripting

How to connect to Oracle database using shell script?

Hi All, I am newbie to unix shell scripting. I want to connect to oracle database using shell script. I know how to connect DB using user name and password but my question is if my password is having '@' sign then how to connect. I have tried like below, cnt=`sqlplus -s /nolog << EOFSQL ... (3 Replies)
Discussion started by: pmotewar
3 Replies

5. Shell Programming and Scripting

Shell Script to connect to the oracle database

Hi Unix Gurus, I have a requirement to write a script, Oracle DB gets referesh every 6hrs, hence we need write a script to connect to the database , if it connects it should generate a file to proceed the next job application or when an error when connectiong to DB it should not generate any... (8 Replies)
Discussion started by: bshivali
8 Replies

6. Shell Programming and Scripting

connect to db2 using shell script

Guys, I am trying to write a shell script that connect to wcsprod database and read the query #!/bin/ksh sqlplus -s < connect to wcsprod user wcsadm using pwd > select * from catentry fetch first 1 row only with ur; databse: wcsprod user: wcsadm pwd: pwd thanks (1 Reply)
Discussion started by: skatpally
1 Replies

7. Shell Programming and Scripting

Connect to oracle db using shell script

Hi, I want to connect to oracle db using unix shell script. And i want to retrieve data from db through shell script. Please help me as soon as possible. Thanks, J.P. Das (1 Reply)
Discussion started by: jyotidas
1 Replies

8. Shell Programming and Scripting

vpn connect/disconnect shell script

Hi I am not so good in scripting..trying ot learn it...need guidance of the experts in shell scripting.. Let me explain the scenario first.. a server MX1 is connected to another server MX2 through vpn..every 5 minute a script runs to test vpn connectivity between the 2 servers.when the vpn... (12 Replies)
Discussion started by: renuka
12 Replies

9. Shell Programming and Scripting

Connect two servers in shell script

Hello all, I know that is a question which has made too many times, and I have been looking into the threads but all posted was not sucessfully for me so... I need a shell script which connect two unix servers, but NOT using ssh... Is there another way to do it? I've tried ssh but it didn't... (1 Reply)
Discussion started by: Geller
1 Replies

10. Shell Programming and Scripting

How to connect DB2 table using shell script

Hi All, I want to connect two tables in DB2 using shell script and then compare the contents of two tables field by field. Could any one please help me in connecting database tables using Unix and retriving data from the same. Thanks, Durwas (0 Replies)
Discussion started by: dtidke
0 Replies
Login or Register to Ask a Question