LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to check the argument value with a file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-check-the-argument-value-with-a-file-4175514295/)

Mitiraj 08-11-2014 09:04 PM

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

notKlaatu 08-11-2014 09:28 PM

The SQL appears ok. I think your issue is in your test:

Code:

if [ `echo $2 |grep -F "/tmp/users.log"` ]
then
  echo "UserName Matches"
else
  echo "UserName Mismatch"
fi

should be more like
Code:

if [ -n `grep "${2}" -F /tmp/users.log` ]
then
  echo "UserName Matches"
else
  echo "UserName Mismatch"
fi

Although it's hard to exact without knowing exactly what your DB is outputting.

Mitiraj 08-11-2014 10:16 PM

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.

notKlaatu 08-11-2014 10:21 PM

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.

Mitiraj 08-11-2014 10:32 PM

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

Mitiraj 08-11-2014 10:34 PM

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

AnanthaP 08-11-2014 11:14 PM

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:

if [ `echo $Table_owner |grep -F "/tmp/users.log" | wc -l` -ge 1 ]; then
echo "UserName Matches"
else
echo "UserName Mismatch"
fi
Why the -F flag? I always thought that it forces an exact match for the full length of the field (25 in your case). So you should pad-right $2 with spaces to make 25 bytes.

OK

Mitiraj 08-12-2014 01:05 AM

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.