how to check the argument value with a file
Hi All,
I am trying to check the argument passed with the existing users in the database. If matches then message "username matches" else "Username Mismatch". Below is the script i am trying and it always throws "Username Mismatch". /ora/sw/app/oracle/product/11.2.0.3/bin/sqlplus -s "/ as sysdba" << EOF >> spool /tmp/users.log column username for a25; column account_status for a33; select username,account_status from dba_users where account_status='OPEN' order by 1; spool off exit EOF if [ `echo $2 |grep -F "/tmp/users.log"` ]; then echo "UserName Matches" else echo "UserName Mismatch" fi Please advise what is wrong. Regards Rajesh |
The SQL appears ok. I think your issue is in your test:
Code:
if [ `echo $2 |grep -F "/tmp/users.log"` ] Code:
if [ -n `grep "${2}" -F /tmp/users.log` ] |
Hi.. Thanks for your reply. I also suspect with the test
I tried with the modified code: if [ -n `grep "${2}" -F /tmp/users.log` ] then echo "UserName Matches" else echo "UserName Mismatch" fi The output always says "Username MAtches" even though the username does not exist in the database. |
Yes, actually you probably only want
< EOF > in your database results. I don't see any reason to append each db query to past results. Rather than comparing, why not just have you script print exactly what your db is returning, so that you can be sure that what you are trying to compare makes sense. I'm assuming your DB returns a one-string username. |
I am not clear here.
My requirement is when a username is passed through shell script then it has to check with database and throw messages accordingly. Plese advise how the script can be modified |
My script is as below (Just changed the log file name..)
#!/bin/ksh ORACLE_HOME=/ora/sw/app/oracle/product/11.2.0.3 PATH=$ORACLE_HOME/bin:$PATH ORACLE_SID=$1 Table_owner=$2 export ORACLE_HOME export PATH export ORACLE_SID export Table_owner dbname=`ps -ef|grep pmon|grep -v grep|awk '{ print $NF }'|awk -F_ '{ print $3 }'` if [ `echo $1 |grep "$dbname"` ]; then echo "DB Name Matches" else echo "DB Name Mismatch" fi /ora/sw/app/oracle/product/11.2.0.3/bin/sqlplus -s "/ as sysdba" << EOF >> spool /tmp/users.log set echo off set lines 100 set pagesize 0 #set heading off #set feedback off set TERMOUT off column username for a25; column account_status for a33; select username,account_status from dba_users where account_status='OPEN' order by 1; spool off exit EOF if [ `echo $2 |grep -F "/tmp/users.log"` ]; then echo "UserName Matches" else echo "UserName Mismatch" fi |
Early on, issue an `echo $#` see whether the number of arguments is what you expect and follow it up with and `echo $2` before assigning $2 to Table_owner
Add a wc -l and check for numeric equals thus: Isn't Quote:
OK |
I have tried with the below and works fine.
if grep --quiet -wFi `echo $2` /tmp/users.log; then echo "Username Exists" else echo "Username not found" fi TKU very much for your efforts to Anantha & notKlaatu |
All times are GMT -5. The time now is 08:28 AM. |