LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 03-14-2010, 12:19 PM   #1
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Rep: Reputation: 146Reputation: 146
Question BASH script alphabetical conditional operator query.


Hello All.

I got a simple question, but Google has not assisted me in this query.

I am using "if" to force a word under the condition that the first letter of that word must be a letter of the alphabet, regardless of capitalization, using the " "" != "" " syntax.

Like so:

Code:
if [[ "$interface" != "WHAT DO I PUT HERE?" ]] ; then
   echo "Invalid input"
   exit 1
fi
Thank you in advance.

Last edited by lupusarcanus; 03-14-2010 at 01:01 PM. Reason: better code
 
Old 03-14-2010, 12:38 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Code:
#!/bin/bash

test="gtw3 tw"
if [[ "$test" =~ ^[A-Za-z] ]]; then
  echo "ok"
else
   echo "Invalid input"
   exit 1
fi
 
Old 03-14-2010, 12:53 PM   #3
lupusarcanus
Senior Member
 
Registered: Mar 2009
Location: USA
Distribution: Arch
Posts: 1,022
Blog Entries: 19

Original Poster
Rep: Reputation: 146Reputation: 146
Quote:
Originally Posted by troop View Post
Code:
#!/bin/bash

test="gtw3 tw"
if [[ "$test" =~ ^[A-Za-z] ]]; then
  echo "ok"
else
   echo "Invalid input"
   exit 1
fi
That did work. However, for sake of code cleanliness, I was hoping for the syntax to say: If the variable does not begin with a letter of the alphabet (regardless of capitalization), then display an error message and exit with status code 1. Is this possible?

The reason is because if I go with your code, this is how it would look:

Code:
if [[ "$interface" =~ ^[A-Za-z] ]]; then
	echo "" >/dev/null
else
   echo "
	ERROR 7: No usable interface label detected.	
"
   exit 1
fi
Thank you for your reply. You gave much more information than my weak Googling skills gave me.

Last edited by lupusarcanus; 03-14-2010 at 12:54 PM.
 
Old 03-14-2010, 01:14 PM   #4
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Code:
if ! [[ "$interface" =~ ^[A-Za-z] ]]; then
    error message ...
else
    other code
fi
 
Old 03-15-2010, 03:04 AM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 666

Rep: Reputation: 133Reputation: 133
Code:
case "$interface" in
   [A-Za-z]*)
       Do something
   ;;

   *)
    error message ...
   ;;
It's not bash specific and works for other shells too
 
Old 03-15-2010, 03:14 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
alternatives
Code:
[[ ${interface:0:1} != [A-Za-z] ]]
[[ ! ${interface:0:1} = [[:alpha:]] ]]
[[ ! $interface =~ ^[A-Za-z] ]]
[[ $interface != [[:alpha:]]* ]]
where you can have
Code:
if [[ ... ]]; then
	echo "ERROR 7: No usable interface label detected."
	exit 1
fi

...
 
  


Reply

Tags
bash, conditional, if, script, ubuntu


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
conditional operator in c archanac07 Linux - Software 2 02-24-2010 12:58 AM
Bash script - Infinite loop caused by a logical OR operator skuzye Programming 9 02-04-2009 01:31 PM
Conditional (IF) test in bash sh script - presence of first parameter Critcho Linux - Newbie 6 10-01-2008 12:20 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
bash script and the , operator xviddivxoggmp3 Programming 1 08-14-2004 12:33 AM


All times are GMT -5. The time now is 11:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration