Whats wrong with this bash script?


Recommended Posts

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

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 by ichi
Link to comment
Share on other sites

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

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

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

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.