Notices

Overclockers Forums > Software > Alternative Operating Systems
Alternative Operating Systems Linux, Mac, Unix Operating Systems and Applications
Forum Jump

help with bash IF

Post Reply New Thread Subscribe Search this Thread
 
 
Thread Tools
Old 06-13-05, 03:32 PM Thread Starter   #1
eNightmare
Member

 
eNightmare's Avatar 

Join Date: Feb 2005

 
help with bash IF


I have a problem...
I'm trying to write a bash script to compare $1

I want the first argument to be the "options" eg. script -d

so here is what i tried

if [$1 == -*]

it doesn't seem to give me the results i want. It "should" tell me whether the first character of the first positional parameter is a hyphen, but it doesn't seem to do that.

Does anyone know how to get that working right?
eNightmare is offline   QUOTE Thanks
Old 06-13-05, 04:10 PM   #2
khiloa
Open Source Senior

 
khiloa's Avatar 

Join Date: Jul 2004
Location: /usa/sc/florence

 
Mind posting the whole script for us to look over?

__________________


Say NO to ads!

Did someone just help you? Thank them.

Along with F@H, please help support some of our other teams.

khiloa is offline   QUOTE Thanks
Old 06-13-05, 04:27 PM Thread Starter   #3
eNightmare
Member

 
eNightmare's Avatar 

Join Date: Feb 2005

 
I guess I could, but its theres not much...
I'm just wondering how I can see if the first positional parameter starts with a hyphen. Its going to the else statement on every occasion with the:

if [$1 == -*]

EDIT: if you really think it will help, i'll post up the script tomorrow.
eNightmare is offline   QUOTE Thanks
Old 06-13-05, 08:17 PM   #4
Christoph
JAPH Senior

 
Christoph's Avatar 

Join Date: Oct 2001
Location: Redmond, WA

 
ideamagnate@feather:~$ f()
> {
>...if [ $1 == '-d' ]
>...then
>.....echo yup
>...else
>.....echo nope
>...fi
> }
ideamagnate@feather:~$ f -d
yup
ideamagnate@feather:~$ f -e
nope
ideamagnate@feather:~$ f 345
nope
ideamagnate@feather:~$


If you're writing anything longer than about 40-50 lines, I highly recommend The ABS Guide.

__________________
FoldSig | Heatware | Alt OS FAQWiki | JAPH | ADO

"Coming onto the forums after you've had a concussion is prolly not a good idea, but I'm doin' it anyways!"
-dark_15
Christoph is offline   QUOTE Thanks
Old 06-13-05, 09:55 PM Thread Starter   #5
eNightmare
Member

 
eNightmare's Avatar 

Join Date: Feb 2005

 
will the (*) wildcard work there?
eNightmare is offline   QUOTE Thanks
Old 06-13-05, 11:34 PM   #6
khiloa
Open Source Senior

 
khiloa's Avatar 

Join Date: Jul 2004
Location: /usa/sc/florence

 
If you put in a wildcard in that example, I believe it would say yup for everything. In what you are doing it may work though.

__________________


Say NO to ads!

Did someone just help you? Thank them.

Along with F@H, please help support some of our other teams.

khiloa is offline   QUOTE Thanks
Old 06-14-05, 02:15 AM   #7
Christoph
JAPH Senior

 
Christoph's Avatar 

Join Date: Oct 2001
Location: Redmond, WA

 
'Fraid not. If you use an asterisk, it matches an asterisk. This is one of the areas where bash doesn't shine. If I had to do this in bash, I'd probably do something like:

ideamagnate@feather:~\ 2 $ f()
> if [ "$(echo $1 | grep -- '-.')" != "" ]; > then
>...echo yes
> else
>...echo no
> fi
ideamagnate@feather:~\ 2 $ f -quux
yes
ideamagnate@feather:~\ 2 $ f -d
yes
ideamagnate@feather:~\ 2 $ f -
no
ideamagnate@feather:~\ 2 $ f --
yes
ideamagnate@feather:~\ 2 $ f 42
no


However, it's a total hack, especially since it relies on executing a separate program to get a simple string. If I actually needed to write code that did something like that, I'd rewrite the code in something more elegant like Perl or C or 1's and 0's.

For reference, in Perl it'd be:
if ($1 ~= /-.*/){
which is much more concise and very easy to read if you're used to Perl. If you're not, it looks like something /dev/random sneezed out, but that's how Perl's supposed to be.

__________________
FoldSig | Heatware | Alt OS FAQWiki | JAPH | ADO

"Coming onto the forums after you've had a concussion is prolly not a good idea, but I'm doin' it anyways!"
-dark_15
Christoph is offline   QUOTE Thanks
Old 06-14-05, 08:43 AM   #8
khiloa
Open Source Senior

 
khiloa's Avatar 

Join Date: Jul 2004
Location: /usa/sc/florence

 
Thats strange; anywho, I think I might be doing some bash programming sometime soon.

__________________


Say NO to ads!

Did someone just help you? Thank them.

Along with F@H, please help support some of our other teams.

khiloa is offline   QUOTE Thanks
Old 06-14-05, 08:51 PM Thread Starter   #9
eNightmare
Member

 
eNightmare's Avatar 

Join Date: Feb 2005

 
I think i'm starting to not like bash
eNightmare is offline   QUOTE Thanks
Old 06-14-05, 11:22 PM   #10
Christoph
JAPH Senior

 
Christoph's Avatar 

Join Date: Oct 2001
Location: Redmond, WA

 
It's got its place. In the same way you wouldn't write an OS kernel in Perl or a MySQL-driven website in C, you wouldn't want to write a script of more than a couple hundred lines in bash. If you're on the CLI and need to do some operation on 500 sequentially named files, it's good to know that you can do it a with a simple bash for loop.

__________________
FoldSig | Heatware | Alt OS FAQWiki | JAPH | ADO

"Coming onto the forums after you've had a concussion is prolly not a good idea, but I'm doin' it anyways!"
-dark_15
Christoph is offline   QUOTE Thanks

Post Reply New Thread Subscribe


Overclockers Forums > Software > Alternative Operating Systems
Alternative Operating Systems Linux, Mac, Unix Operating Systems and Applications
Forum Jump

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Mobile Skin
All times are GMT -5. The time now is 06:38 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
You can add these icons by updating your profile information to include your Heatware ID, Benching Profile ID or your Folding/SETI profile ID. Edit your profile!
X

Welcome to Overclockers.com

Create your username to jump into the discussion!

New members like you have made this the best community on the Internet since 1998!


(4 digit year)

Why Join Us?

  • Share experience
  • Max out your hardware
  • Best forum members anywhere
  • Customized forum experience

Already a member?