function not see variable in script

Hi Forum

Can anyone tell me whats wrong with my script. What i want to do read in values from a input file using a while loop then taking that input from the file into a function that i created. Every time i execute the script it goes through the while loop but the function doesn't see the values that im trying to pass it from the file.

Here is my code

##################################
#this function validate the cost of a product
validCost()
{
  
  echo ${1} | grep -E '[:digit:]+.[:digit:]+'

  if [ $? = 0 ]; then
  {
      echo 'this is valid'
      echo ${1}
  }
  else
  {
      echo 'fail hard out'
      echo ${1}
  }
  fi   
}


##################################
#Main body
##################################

#testing a while loop reading in file

echo '##################'
echo ${1}
echo '##################'
echo 'start of the loop'

echo '##################'
while read ${x} 
do
  
echo '##################'
  echo 'in the loop'
  validCost ${x}
echo '##################'
done < ${1}


echo 'end of loop'

############
here my input file
##############

CHI132456
CHI132456
CHI132
1.5
2.6

i just want to know why the function cant see any data that i pass to it from the file.Any help would be much appreciated and thank you in advance

Hi.

What shell are you using?

Try:

while read x

instead of

while read ${x} 

im using the bash shell oh i give that a try

---------- Post updated at 11:03 AM ---------- Previous update was at 09:35 AM ----------

didnt work :frowning:
keeps comming up with blanks

how are you running it? please poste entire output after adding set -x to the top.

Removing all the junk:

validCost()
{
  
  echo $1 | grep -E '[0-9]+\.[0-9]+' > /dev/null

  if [ $? -eq 0 ]; then
      echo 'this is valid'
      echo $1
  else
      echo 'fail hard out'
      echo $1
  fi   
}
##################################
#Main body
##################################

#testing a while loop reading in file

while read x
do
  validCost $x
done < ${1:-file1}

Output:

fail hard out
CHI132456
fail hard out
CHI132456
fail hard out
CHI132
this is valid
1.5
this is valid
2.6

1) add a slash before the .
2) not an issue but i suggest using -q option to grep instead of redirecting stdout to /dev/null

echo ${1} | grep -q -E '[:digit:]+\.[:digit:]+'

Oh i found the problem I was not give the full path name to the script
i was using this

q1 text.txt

when i should of been doing this

q1 ~/text.text

Scottn does this line

done < ${1:-file1}

tell the script to look in the current directory or am i way off

ps thank scottn/frank for the solution i really appreciate it :smiley:

Hi.

No. I used ${1:-file1} just because I got too lazy typing the filename in every time I ran the script! It means, if no filename is given on the command line, use a file called file1 instead.

So, apart from the ${x} versus x thing, there was really nothing wrong with your script!

This reads "if no argument($1 not set or null) is passed use file1 as the default". If file1 is not in the current working directory then it will fail unless you pass it.

---------- Post updated at 20:50 ---------- Previous update was at 20:46 ----------

actually there was a problem with the regex -- . was matching all characters. some how this was siliently fixed in your code :rolleyes:

Hi.

A minor note: for the type of character class that you wanted to use:

You need to use an extra set of brackets:

       Finally,  certain  named  classes  of  characters are predefined within
       bracket expressions, as follows.  Their names are self explanatory, and
       they   are   [:alnum:],  [:alpha:],  [:cntrl:],  [:digit:],  [:graph:],
       [:lower:], [:print:], [:punct:], [:space:], [:upper:], and  [:xdigit:].
       For  example,  [[:alnum:]]  means  [0-9A-Za-z],  except the latter form
       depends upon the C locale and the ASCII character encoding, whereas the
       former  is  independent  of  locale  and character set.  (Note that the
       brackets in these class names are part of the symbolic names, and  must
       be  included  in  addition  to  the  brackets  delimiting  the  bracket
       expression.)

-- excerpt from man grep, q.v.

cheers, drl

Oh yes, the regex... :slight_smile: (not sure what you mean by silently)

But apart from that...