TrickierStinky Posted March 27, 2008 Share Posted March 27, 2008 can anyone tell me what wrong with this bash script #!/bin/sh count=0 bob=$# while [ $count -ne $(($# - 1)) ] do count=$(($count + 1)) eval files=\$$count eval end=\$$# case $end in [.*]$) echo here #if end param = 'something.*' exit ;; ^[*.]) echo there #if end param = '*.some' exit ;; esac done Link to comment Share on other sites More sharing options...
ichi Posted March 27, 2008 Share Posted March 27, 2008 (edited) while [ $count -ne $(($# - 1)) ] This gets you in an infinite loop if there are no parameters: while [ 0 -ne -1 ] Also it will just exit if there's only one parameter. [.*]$ This regular expresion would work with grep because it looks at substrings, but here you want the whole word to match. *edit: the example I posted didn't actually work, the loop always checks the same parameter. Now this should: #!/bin/sh while [ $# -ne 0 ] do case $1 in [:alnum:]*\.\*) echo "here" #if end param = 'something.*' ;; \*\.[:alnum:]*) echo "there" #if end param = '*.some' ;; esac shift done Edited March 27, 2008 by ichi Link to comment Share on other sites More sharing options...
TrickierStinky Posted March 27, 2008 Author Share Posted March 27, 2008 Thank you, but I dont think I have fully described my problem, or for that matter my script doesnt show the problem, and im grateful for the solution, however that is not the solution I am after I will explain what this script is trying to do. basically I want a script to take in N amount of paramaters but the last paramter is the crictical param. this one should be used within the for loop and if the param is say is bob.* or *.html it will be accepted in and checked to see if the param is *.[anything] or if its [anything].*. if i can get this sorted then i will hopefully want to do regex to check for (*.*)anything.anything hope i have cleared this problem up a bit more Link to comment Share on other sites More sharing options...
ichi Posted March 27, 2008 Share Posted March 27, 2008 I see... how about this: #!/bin/sh if [ $# -lt 2 ] then echo "Two of more parameters required" exit 0 fi count=0 bob=$# while [ $count -ne $(($# - 1)) ] do count=$(($count + 1)) eval files=\$$count eval end=\$$# case $end in [A-Za-z0-9]*\.\*) echo "something.*" ;; \*\.[A-Za-z0-9]*) echo "*.something" ;; [A-Za-z0-9]*\.[A-Za-z0-9]*) echo "something.something" ;; esac done You put a "exit" on every case option so it didn't loop through all the parameters, not sure if that was the intended behaviour. Link to comment Share on other sites More sharing options...
TrickierStinky Posted March 27, 2008 Author Share Posted March 27, 2008 that worked a treat thanks alot Link to comment Share on other sites More sharing options...
TrickierStinky Posted March 28, 2008 Author Share Posted March 28, 2008 ok another problem has emerged, i have the following on my case [A-Za-z0-9]*\.[A-Za-z0-9]*) echo "something.something" ;; \*\.\*) echo" here and there" ;; ......... but if i have a param *.* it always seems to go to the something.something Link to comment Share on other sites More sharing options...
Borbus Posted March 28, 2008 Share Posted March 28, 2008 Not a bash script. Shebang should be: #!/bin/bash Link to comment Share on other sites More sharing options...
ichi Posted March 28, 2008 Share Posted March 28, 2008 ok another problem has emerged, i have the following on my case[A-Za-z0-9]*\.[A-Za-z0-9]*) echo "something.something" ;; \*\.\*) echo" here and there" ;; ......... but if i have a param *.* it always seems to go to the something.something That's weird, because * doesn't match [A-Za-z0-9]* at all :huh: Not a bash script. Shebang should be:#!/bin/bash True, although it only really matters if your script is non POSIX compliant. Link to comment Share on other sites More sharing options...
Recommended Posts