LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-07-2010, 11:17 PM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Bash conditional help


Code:
if  [ ${METAL} = "Au" -o ${METAL} = "Ag" ]; then
  echo
else
  echo "This utility is only for Ag and Au."
  exit 1
fi
How can you negate this Bash condition so that no else is required? I am trying to get rid of the else.

In C++, what I am trying to do would be:

Code:
if (!(metal == "Au" || metal == "Ag")) {
  cout << "This utility is only for Ag and Au.";
  exit(1);
}

Last edited by the_gripmaster; 09-07-2010 at 11:28 PM.
 
Old 09-07-2010, 11:48 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Code:
if  [ ${METAL} != "Au" -o ${METAL} != "Ag" ]; then
    echo "This utility is only for Ag and Au."
  exit 1
fi
Code:
if  [[ ${METAL} != "Au" || ${METAL} != "Ag" ]]; then
    echo "This utility is only for Ag and Au."
  exit 1
fi

Last edited by smoker; 09-07-2010 at 11:52 PM.
 
Old 09-08-2010, 12:01 AM   #3
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by smoker View Post
Code:
if  [ ${METAL} != "Au" -o ${METAL} != "Ag" ]; then
    echo "This utility is only for Ag and Au."
  exit 1
fi
Code:
if  [[ ${METAL} != "Au" || ${METAL} != "Ag" ]]; then
    echo "This utility is only for Ag and Au."
  exit 1
fi
I did try code1 before. But unfortunately none of these works.
 
Old 09-08-2010, 12:08 AM   #4
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Found the fix!

Code:
if ! [ ${METAL} = "Au" -o ${METAL} = "Ag" ]; then
  echo "This utility is only for Ag and Au."
  exit 1
fi
 
Old 09-08-2010, 01:01 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can do this with case/esac
Code:
case "$METAL" in
  Au|Ag ) echo "This utitlity..... ";;
  *) echo "....";;
esac
 
Old 09-08-2010, 01:33 AM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
I hate posting on 'solved' threads, but...

If you have to use old style tests, you can also do it '[ foo ] || [ bar ]' (or &&) since '[ foo -o bar ]' (or -a) can be kinda funky.

Code:
if [ ${METAL} != "Au" ] && [ ${METAL} != "Ag" ]; then
or

Code:
if ! [ ${METAL} = "Au" ] && ! [ ${METAL} = "Ag" ]; then
<pedantic>If using the old-style test, quoting "$METAL" is a good idea and it doesn't need the braces.</pedantic>

But tweaking smoker's line

Code:
[[ ${METAL} != "Au" && ${METAL} != "Ag" ]]; then
or using ghostdog74's is probably about the best.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] conditional substitution sharky Programming 12 03-28-2010 09:22 AM
BASH script alphabetical conditional operator query. lupusarcanus Linux - Newbie 5 03-15-2010 03:14 AM
conditional operator in c archanac07 Linux - Software 2 02-24-2010 12:58 AM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 12:20 AM
bash conditional questions teufelhund Programming 19 04-22-2006 04:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:34 AM.

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
Open Source Consulting | Domain Registration