function not see variable in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers function not see variable in script
# 1  
Old 09-10-2010
Error 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
# 2  
Old 09-10-2010
Hi.

What shell are you using?

Try:

Code:
while read x

instead of
Code:
while read ${x}

# 3  
Old 09-10-2010
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 Smilie
keeps comming up with blanks
# 4  
Old 09-10-2010
how are you running it? please poste entire output after adding set -x to the top.
# 5  
Old 09-10-2010
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 10:24 PM..
# 6  
Old 09-10-2010
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:]+'

# 7  
Old 09-10-2010
Hammer & Screwdriver

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 Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass variable from one function to another function?

updateEnvironmentField() { linewithoutquotes=`echo $LINE | tr -d '"'` b() } I want to pass variable named $linewithoutquotes to another method called b(), which is called from updateEnvironmentField() method. How to do the above requirement with shell script (1 Reply)
Discussion started by: pottic
1 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

5. Shell Programming and Scripting

Pass a variable string in To_Date Oracle function in shell script

Hello, I am trying to execute an SQL query from shell script. A part of script is something like this: fromDate=`echo $(date +"%F%T") | sed "s/-//g" | sed "s/://g"` $ORACLE_HOME/sqlplus -s /nolog <<EOD1 connect $COSDBUID/$COSDBPWD@$COSDBSID spool... (4 Replies)
Discussion started by: sanketpatel.86
4 Replies

6. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

7. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. Shell Programming and Scripting

shell script receiving variable and doing mathematical function

Hello, Please help for the following scenario: 1. Shell Scipt should receive 2 variables values (say a and b). 2. Within shell script, there should be division of those numbers (a/b). 3. The result should be whole number i.e. in case the result comes out to be 9.4, it should be returned as... (3 Replies)
Discussion started by: damansingh
3 Replies

10. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies
Login or Register to Ask a Question