LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-11-2014, 10:04 PM   #1
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Rep: Reputation: Disabled
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
 
Old 08-11-2014, 10:28 PM   #2
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware, Fedora
Posts: 939

Rep: Reputation: 601Reputation: 601Reputation: 601Reputation: 601Reputation: 601Reputation: 601
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.

Last edited by notKlaatu; 08-11-2014 at 10:38 PM.
 
Old 08-11-2014, 11:16 PM   #3
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-11-2014, 11:21 PM   #4
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware, Fedora
Posts: 939

Rep: Reputation: 601Reputation: 601Reputation: 601Reputation: 601Reputation: 601Reputation: 601
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.
 
Old 08-11-2014, 11:32 PM   #5
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-11-2014, 11:34 PM   #6
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
Old 08-12-2014, 12:14 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 814

Rep: Reputation: 195Reputation: 195
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

Last edited by AnanthaP; 08-13-2014 at 09:53 AM.
 
1 members found this post helpful.
Old 08-12-2014, 02:05 AM   #8
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins Iyyappan Linux - Server 10 07-03-2013 06:19 AM
BASH: check if the first argument match a number cristalp Programming 5 11-15-2012 12:10 PM
[SOLVED] Bash Script - Check Argument with REGEX mrm5102 Programming 9 04-10-2012 11:07 AM
perl pass regex check as an argument lord-fu Programming 2 08-29-2007 03:55 PM


All times are GMT -5. The time now is 03:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration