Bash - OR within an IF statement


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Bash - OR within an IF statement
# 1  
Old 01-15-2013
Bash - OR within an IF statement

Hey guys,

Currently trying to write a wee script that runs only when logged in as one of two users. The rest of the script is working fine, but no matter what user I try to run it as, it always fails! This is the puzzling part:
Code:
if [[ $USER != "admin1"  ||  $USER != "admin2" ]]; then
        echo "Run script as admin "
        exit 1
else
        ... runs some script . . . 
fi

Does anyone have any ideas !? It's probably staring me straight in the face but as I'm still learning bash/scripting then it's going right past me!

Thanks guys for any help

Jim
# 2  
Old 01-15-2013
Your test can't be but true, use an AND, not an OR:

Code:
if [[ $USER != "admin1"  &&  $USER != "admin2" ]]; then
...

This User Gave Thanks to jlliagre For This Post:
# 3  
Old 01-15-2013
Code:
if [ "$USER" = "admin1" ] || [ "$USER" = "admin2" ]
then
        echo "Run scripts here"
else
        echo "Run script as admin "
        exit 1
fi

This User Gave Thanks to Yoda For This Post:
# 4  
Old 01-15-2013
Thanks guys! Brilliant - I've been tearing my hair out (well, whats left of it!) wondering how I couldn't get it to work, should have stopped by earlier.

You guys rock! Smilie
# 5  
Old 01-16-2013
bash also allows for a regex matching:
Code:
$ [[ "$USER" =~ admin[12] ]] && echo in || echo out
in

# 6  
Old 01-16-2013
Or ordinary pattern matching:
Code:
if [[ $USER != admin[12] ]]; then

or extended pattern matching:
Code:
shopt -s extglob
if [[ $U = !(admin1|admin2) ]]; then

Or you could use a case statement:
Code:
case $USER in 
  admin[12]) echo yes ;;
  *)         echo no ;;
esac

Code:
case $USER in 
  admin1|admin2) echo yes ;;
  *)             echo no ;;
esac

--
@RudiC: that would need to be ^admin[12]$

Last edited by Scrutinizer; 01-16-2013 at 07:10 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-16-2013
Absolutely right - thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH Grouping tests in IF statement

Ok I got the following to work by removing the parentheses arount $year and it works. Can a kind soul please explain this behavior. Also, to my topic question, am I using curly braces correctly in the IF statement? The intent is to check for a leap year. If the year is divisible by 400 then no need... (6 Replies)
Discussion started by: Riker1204
6 Replies

2. UNIX for Dummies Questions & Answers

Bash statement equivalent

Hi all, i need a equivalent for the statement i run in bash, so it would also run in other shells. Specially i need it for ksh to run on AIX. Here the statements: exec > >(tee -a $log) exec 2> >(tee -a $log >&2) Thanks. (5 Replies)
Discussion started by: Kosak
5 Replies

3. Shell Programming and Scripting

[Solved] If statement in bash

I have the following code in bash, however "set red frmt" is not displayed. echo "iarg_rd = $iarg_rd" iarg_rd="2" if ; then echo "Hello World" fi if ; then frmt="${gap}${!frmt_titl_yl}" elif ; then frmt="${gap}${!frmt_titl_bk}" elif ; then echo... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

6. Shell Programming and Scripting

bash if statement help needed

Hi I need a script with an if statement that goes. I need it to search through all files within a directory with the extension .test if it finds the string '71502FSC1206' then do sed 's/71502FSC1206/\n&/g' > send.test If it finds the string '715MCH' or '715JAC' then I need it to move the... (1 Reply)
Discussion started by: firefox2k2
1 Replies

7. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

8. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

9. Shell Programming and Scripting

compound Bash if then statement question

I am writing a Bash script that will either clone a database or setup a standby database. So Parameter 2 will be the operation type. If the value is not clone or standby I want to throw an error message. I suppose I can also do a case block. So far i have been unable to get the syntax working... (1 Reply)
Discussion started by: gandolf989
1 Replies

10. Shell Programming and Scripting

Bash: evaluating $? variable (if statement)

Hello, i'm unable to write a correct if... statement to evaluate the $? variable. Could anybody send to me an example? for example, this lines of code didn't work... if ; then etc etc if ; then etc etc Thank you in advanced. (5 Replies)
Discussion started by: aristegui
5 Replies
Login or Register to Ask a Question