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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass a function with a variable parameter into another variable?
# 1  
Old 05-26-2011
How to pass a function with a variable parameter into another variable?

Hello again Smilie

Am currently trying to write a function which will delete a record from a file.

The code currently looks as such:

Code:
 
function deleteRecord() {
clear
                read -p "Please enter the ID of the record you wish to remove: " strID
                output=$(grep "$strID" recordDatabase)
                recordExists=findRecord $strID
 
#code will go here to check that recordExists returns a value.....

I hope what I am trying to do makes sense, the function findRecord searches the file for examples which match the ID. This works fine.

I simply need to store the returned value of findRecord $strID in recordExists.

Thanks in advance Smilie

Mikey
# 2  
Old 05-26-2011
Well, usually the grep needs to be anchored into one field.

Deleting a record from a file is usually making a new file without the record (grep -v).

Sed could find the delete records and divert them to a side file using the w and d options.
Code:
sed '
  /^'"$strID"':/{
    w side_file
    d
   }
 ' < original_file > new_file

# 3  
Old 05-26-2011
Show us what input you have and what result you need?
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. Homework & Coursework Questions

How to Dynamically Pass Parameter to plsql Function & Capture its Output Value in a Shell Variable?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: #! /bin/ksh v="ORG_ID" ... (2 Replies)
Discussion started by: sujitdas2104
2 Replies

3. Shell Programming and Scripting

How to pass function parameter to do loop?

Hi All, I have created one function for KSH and was running well with one parameter input since I just had to use $1 to get the parameter. Now I want to do loop for each parameter(actually filenames) . I have try to use do loop, but $i does not resolve to parameter instead it resolves to 1,... (5 Replies)
Discussion started by: mysocks
5 Replies

4. Shell Programming and Scripting

How to pass position parameter into function.?

Hi Gurus, I have request which needs to pass position parameter to a function. I tried below simple code, it doesn't work. #!/bin/bash func_1(){ echo $1 } func_1 $ ./set_file abc $ do I need add some to get the position para first? thanks in advance. (3 Replies)
Discussion started by: ken6503
3 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. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

Pass command as a function parameter

Hi guys, can someome help with this question, I have defined a function that takes a command as a parameter, but when the command is executed from the function it will throw errors because what I believe is a special character escaping issue. I tried using the backslash to escape the pipe | and >... (2 Replies)
Discussion started by: marouanix
2 Replies

8. Shell Programming and Scripting

How to pass a variable as a parameter to DB2 database from shell script

I need to pass a variable as a parameter from shell script into a DB2 database. var=bhuk_1123_Q_11/22/09 select * from tbl1 where serial_id='$var'; I have tried executing it using db2 -tvf scriptname Somebody please help me out with this. It is throwing an error. Please tell me how... (2 Replies)
Discussion started by: ss3944
2 Replies

9. Shell Programming and Scripting

how to pass variable in NR function used in awk command?

Hi I want to pass variables with the NR function in awk command. test_file1 is input file having 500 records. var1=100. var2=200 awk -F" " 'NR >= $var1 && NR <= $var2' test_file1 > test_file2. My end result should be that test_file2 should have records from line number between... (2 Replies)
Discussion started by: Nishithinfy
2 Replies

10. Shell Programming and Scripting

pass parameter to function

HI all I have a code like ############################################## minyear() { curryear=$1 echo $curryear } ##Main Program ## minyear exit ####### when i execute "sh scriptname 2005" output should be like 2005 but the output is blank. I guess i need to pass parameter to... (3 Replies)
Discussion started by: vasuarjula
3 Replies
Login or Register to Ask a Question