how to compare text value in shell


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users how to compare text value in shell
# 1  
Old 06-19-2007
how to compare text value in shell

Hi,

I need to check that if user is not oracle the the script should exit out .
But the below script always exits evenif I am logged as oracle.

Plz help me...........

USER=`id`
echo $USER

if [ "$USER" != "oracle" ]; then
echo " You are logged as `id` , it must be oracle "
exit 2
fi

Regards
# 2  
Old 06-19-2007
Quote:
Originally Posted by reply2soumya
USER=`id`
Instead of `id` use `whoami`
# 3  
Old 06-19-2007
Quote:
USER=`id`
echo $USER

if [ "$USER" != "oracle" ]; then
echo " You are logged as `id` , it must be oracle "
exit 2
fi

Code:
#!/bin/bash
USER=`whoami`
echo $USER

if [ "$USER" != "oracle" ]; then
echo " You are logged as $USER , it must be oracle"
exit 2
fi

Output:

Quote:
Compaq
You are logged as Compaq , it must be oracle
The best way to check your script is to debug it. if you would have used the sh -x option, you would have seen for itself that id command returns not the username but a lot of information.

Quote:
$ sh -x soumya
++ id
+ USER='uid=1006(Compaq) gid=513(None) groups=0(root),513(None),544(Administrato
rs),545(Users),1007(Debugger Users)'
+ echo 'uid=1006(Compaq)' 'gid=513(None)' 'groups=0(root),513(None),544(Administ
rators),545(Users),1007(Debugger' 'Users)'
uid=1006(Compaq) gid=513(None) groups=0(root),513(None),544(Administrators),545(
Users),1007(Debugger Users)
+ '[' 'uid=1006(Compaq) gid=513(None) groups=0(root),513(None),544(Administrator
s),545(Users),1007(Debugger Users)' '!=' oracle ']'
++ id
+ echo ' You are logged as uid=1006(Compaq) gid=513(None) groups=0(root),513(Non
e),544(Administrators),545(Users),1007(Debugger Users) , it must be oracle '
You are logged as uid=1006(Compaq) gid=513(None) groups=0(root),513(None),544(A
dministrators),545(Users),1007(Debugger Users) , it must be oracle
+ exit 2
Hope this helps !!!!
kamitsin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to compare text and send a mail

Hello, Somebody can help me to develop a script for the below requirement I have a 3 files in a directory which was dynamically changed everyday basis. I need a script to fetch the 1pattern of strings in each file. if the three matching pattern is same send a mail with subject as success else... (1 Reply)
Discussion started by: MATHANKUMAR
1 Replies

2. Shell Programming and Scripting

Compare two fields in text files?

Hi, I have two text files, compare column one in both the files and if it matches then the output should contain the id in column one, the number and the description. Both the files are sorted. Is there a one liner to get this done, kindly help. Thank you File 1: NC_000964 92.33 ... (2 Replies)
Discussion started by: pulikoti
2 Replies

3. Shell Programming and Scripting

Script to compare two text files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt name1 name2 name3 file1 has to be comared with file2 defaultfile.txt name1 name2 name3 name4 and during comparision with defaultfile.txt if... (2 Replies)
Discussion started by: draghun9
2 Replies

4. UNIX for Dummies Questions & Answers

Compare two text files

Hello guys, I have file1 and file2, two text files containing various lines. I'm trying to find a way to compare file1 and file2: If the first 7 characters of a line in file2 match the first 7 characters of a line in file1, then do not do anything. Print out the lines of file1 (in file3,... (3 Replies)
Discussion started by: bobylapointe
3 Replies

5. Shell Programming and Scripting

How to compare two text files

Hi Team, Could you please help me on below one .. etrademail1.txt etDefaultLogin=pdayanan mail=poojaaragam.dayanand@exchange.etr.comx employeeNumber=31567 etDefaultLogin=sudrupa mail=sudrupa.ayanand@exchange.etr.comx employeeNumber=318967 etDefaultLogin=gurathi (1 Reply)
Discussion started by: nivas_k2006
1 Replies

6. Shell Programming and Scripting

compare text in log file

Hi all, I am posting the thread similar to previous posts but here my scenario is i need to know what the files size from start date. Here end time file size will be 0. Also need to know how much time does it took to complete in seconds. log file: Name1 START 11:36:45 ... (5 Replies)
Discussion started by: Olivia
5 Replies

7. Shell Programming and Scripting

Text Compare

Hello, Sorry for my english. Thank you for your help. I have two huge text files. first text: john66 genete tom77 zedene mike44 hedene second text: tom77 zerten feder22 arkan mike44 barkan (3 Replies)
Discussion started by: hoo
3 Replies

8. Shell Programming and Scripting

Compare 2 files and output only the different text.

I know the diff does this but it does output more info than just the different text (e.g. $ diff file1 file2 29a30 > /home/alex/Pictures/hello.jpg 1694a1696 > /home/alex/Pictures/hi.jpg ) How can I make it output only /home/alex/Pictures/hello.jpg /home/alex/Pictures/hi.jpg ? thank... (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

Compare text strings.

Hi Im trying to write a script that compare a text string. But it fails, I think it adds a extra line feed to the result and fails beacuse of that. The script. DT=`date +'%Y%m%d%H%M%S'` #ALARM_BIN=/users/alarms/ssa/alarms/bin QUEUE_THR=10 #unset offset #offset="***Server reports data... (3 Replies)
Discussion started by: vettec3
3 Replies

10. UNIX for Dummies Questions & Answers

compare text files

This may be the 3rd time I'm posting this question. I'm so new here that I'm not even sure how to post! I'm trying to compare two files but can't do a line by line comparison so comm and diff are out. I've been told that I would need to use the awk programing language. I've looked up what I... (14 Replies)
Discussion started by: jimmyflip
14 Replies
Login or Register to Ask a Question