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
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
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:
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.