Quick question regarding if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick question regarding if statement
# 1  
Old 07-16-2010
Quick question regarding if statement

Hi,
I'm trying to write an if statement that will check the USER parm against some text but I'm not quite sure how to use the or switch in the statement.. Can anyone help me out?...

If someone could also let me know when to use ( or [ in the statement.. Can never figure that out...

Code:
if (( $USER != "user1" || "user2" || "user3" || "user4" ))
then
  print ""
  print "Incorrect username passed in"
  print ""
  print  "Please check the spelling of $USER..."
  print ""
  exit 1
fi


Thanks,
Jaz
# 2  
Old 07-16-2010
I think it's an AND you need, not an OR?

Code:
if [ "$USER" != "user1" -a "$USER" != "user2" -a "$USER" != "user3" -a ... ]

Alternatively, you can use a case statement:

Code:
case "$USER" in
 user1|user2|user3|...)  echo "do something";;
 *) echo "do something else"
esac

I never see any reason to use ( over [
# 3  
Old 07-16-2010
Which shell? What are the exact patterns you're checking against?
# 4  
Old 07-16-2010
Sorry, using ksh...

I'm just passing in a parm (ie a username) through command line at the minute.. Trying to test this out first...

Wasn't sure if ( and [ had different results.. Whats the logic behind using double [[ or ((?
# 5  
Old 07-16-2010
All (, ((, [ and [[ have different meanings. You'll find their descriptions in your shell man pages.
I suppose you're looking for the case construct (see scottn's post above).
# 6  
Old 07-16-2010
Thanks for your help guys..

Code:
if [ "$USER" != "user1" -a "$USER" != "user2" -a "$USER" != "user3" -a ... ]

works fine..

I wanted the script to exit out if none of the users were matched..
# 7  
Old 07-17-2010
Quote:
Originally Posted by Jazmania
Thanks for your help guys..
I wanted the script to exit out if none of the users were matched..
use if-else-fi

Code:
else 
  exit

Or update from scottn's code:

Code:
case "$USER" in
 user1|user2|user3|...)  echo "do something";;
 *) exit;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

2. UNIX for Dummies Questions & Answers

Quick question

Hello all, Quick question from a fairly new to Unix developer. if then completedLogFile=$logfile.$(date +%Y%m%d-%H:%M:%S) mv $logfile $completedLogFile fi I understand that this portion of code is simply copying a tmp logfile to a completed logfile when a condition is true. The... (2 Replies)
Discussion started by: JohnnyBoy
2 Replies

3. UNIX for Dummies Questions & Answers

quick question

from command prompt I did grep two words on a same line for eg: grep abc | grep xyz and I got tht particular line, but I want to know when I vi that file how to directly search for that particular line? I appreciate if any one can provide answer, thanks in advance (2 Replies)
Discussion started by: pkolishetty
2 Replies

4. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

5. Shell Programming and Scripting

Ok quick question

Hi i just wanted to know is there anyway to log the keystrokes on a remote computer? For example i let my nieces play on my other computer downstairs *my computer and the one downstairs are on a LAN* and i want to see everything they type in to make sure they arent doing anything they are supposed... (1 Reply)
Discussion started by: Corrail
1 Replies

6. UNIX for Dummies Questions & Answers

Quick Question

Hi, I am new to UNIX, and am learning from this tutorial : http://www.ee.surrey.ac.uk/Teaching/Unix/index.html It keeps telling me to files downloaded from the internet (like .txt files) to the directory, and I dont know how to. How do I add .txt files to my directory? Thanks. (6 Replies)
Discussion started by: IAMTHEEVILBEAN
6 Replies

7. Shell Programming and Scripting

quick question

does anyone know what $? means? i echoed it on my box (running AIX Korn shell) and got 127 (2 Replies)
Discussion started by: penfold
2 Replies

8. UNIX for Advanced & Expert Users

Quick VI question

This "SHOULD" be a simple question, but looking through several books has turned up nothing, so I turn once again to the experts!! How do you vi a file so that you can see special characters. I believe my /etc/passwd file is being corrupted during an upgrade process, however the files... (6 Replies)
Discussion started by: Recon
6 Replies

9. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies

10. Shell Programming and Scripting

A very quick question

Just a super quick question: how do you put a link in your php code. I want to make a link to something in /tmp directory. i.e. how do you put a href into php, I think it's done a bit differently. thanks john (1 Reply)
Discussion started by: jmg5
1 Replies
Login or Register to Ask a Question