Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-10-2010
ShinTec's Avatar
Registered User
 

Join Date: Jan 2010
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation 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

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
##############

Code:
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
Sponsored Links
    #2  
Old 09-10-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,364
Thanks: 130
Thanked 542 Times in 475 Posts
Hi.

What shell are you using?

Try:


Code:
while read x

instead of

Code:
while read ${x}

Sponsored Links
    #3  
Old 09-10-2010
ShinTec's Avatar
Registered User
 

Join Date: Jan 2010
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
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
keeps comming up with blanks
    #4  
Old 09-10-2010
Resident BOFH
 

Join Date: Dec 2007
Posts: 1,100
Thanks: 2
Thanked 76 Times in 74 Posts
how are you running it? please poste entire output after adding set -x to the top.
Sponsored Links
    #5  
Old 09-10-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 

Join Date: Jun 2009
Location: Switzerland - ZH
Posts: 5,364
Thanks: 130
Thanked 542 Times in 475 Posts
Removing all the junk:


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

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


Last edited by Scott; 09-10-2010 at 09:24 PM..
Sponsored Links
    #6  
Old 09-10-2010
Resident BOFH
 

Join Date: Dec 2007
Posts: 1,100
Thanks: 2
Thanked 76 Times in 74 Posts
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



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

Sponsored Links
    #7  
Old 09-10-2010
ShinTec's Avatar
Registered User
 

Join Date: Jan 2010
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

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

Code:
q1 text.txt

when i should of been doing this

Code:
q1 ~/text.text

Scottn does this line

Code:
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SHELL SCRIPT Function Calling Another Function Please Help... omkar.sonawane Shell Programming and Scripting 2 04-13-2010 10:20 AM
Passing global variable to a function which is called by another function sars Shell Programming and Scripting 4 06-30-2008 11:39 AM
shell script receiving variable and doing mathematical function damansingh Shell Programming and Scripting 3 05-23-2008 02:52 AM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 07:28 PM
passing variable to function Knotty UNIX for Dummies Questions & Answers 4 04-05-2007 12:49 AM



All times are GMT -4. The time now is 03:34 AM.