passing variable content to a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting passing variable content to a function
# 1  
Old 11-18-2011
passing variable content to a function

following on from below link
HTML Code:
https://www.unix.com/shell-programming-scripting/171076-shell-scripting.html#post302573569
i will be using file reading in while loop say for example
Code:
 
while read line123
do
        echo "line read is $line123"
        insert_funct $line123
done< mysqldump.sql
 
inser_funct()
{
    echo $1
}

here say the mysqldump.sql has a line

`reserved2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`reserved3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL

it reads the first line and it will be stored in line123 variable in while loop but when i pass this to a function insert_funct i am able to receive only `reserved` and rest of the content is not visibile. if i echo in while loop its showing whole line and in function its showing clipped one. if you see the html link of my previous code i have two function. in one function i need only `reserved` which is column name and in other i need whole line. but as i said before am able to receive only `reserved` even then i am not able to perform string manipulation on that. its length is shown null etc etc.

any help is deeply appreciated
regards,
vivek
# 2  
Old 11-18-2011
Try
Code:
insert_funct "$line123"

(without quotes, variable substitution means you're effectively passing 6 arguments to insert_funct, since your string has whitespace in it).

Last edited by CarloM; 11-18-2011 at 07:42 AM..
# 3  
Old 11-18-2011
like the below
Code:
`reserved2` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`reserved3` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`server` text COLLATE utf8_unicode_ci NOT NULL,

there are many columns.. so how can i extract only column name from this.. column name changes everytime and differs it specification
consider the above 1st or 2nd or 3rd line will be in a variable called var i want to extract only reserved2 or reserved3 or server to a variable. can you plz help me how to do that.. :-/

---------- Post updated at 05:45 PM ---------- Previous update was at 05:44 PM ----------

i am getting this error... :-/ if you go through the code in the hyper link you might be able to understand

Code:
this line not present : `reserved6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
sending these: Active and also `reserved6` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'echo "dollar 1: Active"
        echo "dollar 2: `reserved6` varchar(255) COLLATE utf8_u' at line 1
Script  failed please verify the sqls

# 4  
Old 11-21-2011
To get the column
Code:
#!/bin/bash
insert_func()
{
        column=$( echo $line | awk -F\` '{print $2}' )
        echo $column
}
while read line
do
        insert_func "$line"
done < mysqldump.sql

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 11-21-2011
Code:
column=$( echo $line | awk -F\` '{print $2}' )

this is not working... the variable 'column' is showing as empty. even i tried the same code by storing a line from the file in variable and tried it indivicually at shell promt even then its showing blank..
# 6  
Old 11-21-2011
Code:
insert_func() 
{
  val=$1
 column=$( echo $val | awk -F\` '{print $2}' )
 echo $column
 }

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 7  
Old 11-22-2011
Code:
#
#function for deleting the extra column
#
delete_column()
{
        mysql -udunkin -pdunkin123 ditdb << EOF
        column_name=$2
        echo "script to delete the column"
        echo "column : $column_name"
#       len=$(#column)
#       echo "length : $len"
#       column_name=$(column:1:$len-1)
#       echo "column_name: $column_name"
        ALTER TABLE $1 drop COLUMN $column_name;
        QUIT
EOF
        if [ $? -eq 0 ]
        then
                   echo "$2 dropped from $1 Successfully "
                   else
                   echo "Script  failed please verify the sqls"
                   exit 1
        fi

}

may i know what is wrong with this.. i am getting error as below

Code:
value1 : 22
value2 : 20
========printing lines not present in new mysql dump(delete)=========
this line not present : `reserved1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
column name is : reserved1
./fileread: line 71: column:1:-1: command not found
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column_name=reserved1
        echo "script to delete the column"
        echo "column : reserv' at line 1
Script  failed please verify the sqls

---------- Post updated at 01:03 PM ---------- Previous update was at 01:02 PM ----------

ahamed your code for column extract worked.. thanks :-)

---------- Post updated at 01:40 PM ---------- Previous update was at 01:03 PM ----------

Code:
#
#function for deleting the extra column
#
delete_column()
{
        mysql -udunkin -pdunkin123 ditdb << EOF
        column_name=$2
        echo "script to delete the column"
        echo "column : $column_name"
#       len=$(#column)
#       echo "length : $len"
#       column_name=$(column:1:$len-1)
#       echo "column_name: $column_name"
        ALTER TABLE $1 drop COLUMN $column_name;
        QUIT
EOF
        if [ $? -eq 0 ]
        then
                   echo "$2 dropped from $1 Successfully "
                   else
                   echo "Script  failed please verify the sqls"
                   exit 1
        fi

}

may i know what is wrong with this.. i am getting error as below

Code:
value1 : 22
value2 : 20
========printing lines not present in new mysql dump(delete)=========
this line not present : `reserved1` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL
column name is : reserved1
./fileread: line 71: column:1:-1: command not found
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'column_name=reserved1
        echo "script to delete the column"
        echo "column : reserv' at line 1
Script  failed please verify the sqls

also your code for extracting column name worked.. thanks ahamed :-)

---------- Post updated at 06:55 PM ---------- Previous update was at 01:40 PM ----------

Hi everyone my code is finally working if the mysqldump1.sql and mysqldump2.sql has only one table and also it performs the function of adding a column and dropping a column. the code is given below. now i need to proceed as creating a table if it doesnt exists. and also most importantly making the code work if the mysqldump files has many tables (for eg 100 plus). i willl do it step by step.. for now the code is below..

Code:
#!/bin/sh
#
# Here we are considering testdump1.sql as old mysql dump of database
# and testdump2.sql as latest mysql dump file to be changed to hence
# forth there will be two functions namely to insert missing 
# columns from new mysql dump to database, and also deleting new 
# columns from database which are not present in new mysql dump.
#
#
# Test purpose testdump1.sql and testdump2.sql has one table each with 
# few columns names missing.
#
#
value1=0
value2=0

while read line1
do
       value1=`expr $value1 + 1`
#       the abovel ine gives the number of line in the file
 
done < testdump1.sql
while read line2
do
        value2=`expr $value2 + 1`
#       the abovel ine gives the number of line in the file 
  
done < testdump2.sql
echo "value1 : $value1"
echo "value2 : $value2"
counter1=0
counter2=0
len1=0
len2=0
#
#function for inserting missed columns
#
insert_column()
{
        mysql -udunkin -pdunkin123 ditdb << EOF
 
        ALTER TABLE $1 ADD COLUMN $2;
        QUIT
EOF
        if [ $? -eq 0 ]
        then
                  echo "$2 added to $1 Successfully "
                  else
                  echo "Script  failed please verify the sqls"
                  exit 1
        fi

}
#
#function for deleting the extra column
#
delete_column()
{
 mysql -udunkin -pdunkin123 ditdb << EOF
 
        ALTER TABLE $1 drop COLUMN $2;
 
  QUIT
EOF
        if [ $? -eq 0 ]
        then
                   echo "$2 dropped from $1 Successfully "
                   else
                   echo "Script  failed please verify the sqls"
                   exit 1
        fi

}

#
# this below piece of code is used to find columns not present in new database
# and hence will involve method for deleting that column.
#
echo "========printing lines not present in new mysql dump(delete)========="
echo ""
while read line1
do
 
# echo "$line1"
 counter2=0
 len1=${#line1}
 while read line2
 do
  len2=${#line2}
  char1=${line1:$len1-1:$len1}
  char2=${line2:$len2-1:$len2}
  if [ "$char1" = "," ]
  then
   line11=${line1:0:$len1-1}
  else
   line11=$line1
  fi
  
  if [ "$char2" = "," ]
                then
   line22=${line2:0:$len2-1}
  else
                        line22=$line2
  fi
  if [ "$line11" != "$line22" ]
  then
   counter2=`expr $counter2 + 1`
  fi
  
  if [ $counter2 -eq $value2 ]
  then
   echo "this line not present : $line11"
#   here calling the delete method   
   table_name=$( awk -F\` '/CREATE TABLE/{print $2}' testdump2.sql )
   val=$line11
#   column_name=$( echo $val | awk -F\` '{print $2}' )
   column_name=$( echo $line11 | awk -F\` '{print $2}' )
   echo "column name is : $column_name"
   delete_column "$table_name" "$column_name"
   
  fi
    
 done < testdump2.sql
done < testdump1.sql

#
# this below piece of code is used to find columns not present in old database
# and hence will involve method for inserting that column.
#
echo ""
echo "========printing lines not present in old mysql dump(insert)========="
echo ""
while read line2
do
#       echo "$line2"
        counter1=0
 len2=${#line2}
        while read line1
        do
  
  len1=${#line1}
 
  char1=${line1:$len1-1:$len1}
                char2=${line2:$len2-1:$len2}
                if [ "$char1" = "," ]
                then
                        line11=${line1:0:$len1-1}
                else
                        line11=$line1
                fi
                if [ "$char2" = "," ]
                then
                        line22=${line2:0:$len2-1}
                else
                        line22=$line2
                fi
                if [ "$line11" != "$line22" ]
                then
                        counter1=`expr $counter1 + 1`
                fi
                if [ $counter1 -eq $value1 ]
                then
                        echo "this line not present : $line22"
   column_name=$line22
#   here calling the insert method
   table_name=$( awk -F\` '/CREATE TABLE/{print $2}' testdump2.sql )
   
   echo "sending these: $table_name and also $line22"
   insert_column "$table_name" "$column_name"
                fi
        done < testdump1.sql
done < testdump2.sql

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Programming

PASSING PART OF FILE CONTENT TO VARIABLE

All, I have a log file containing lots of data now i want to extract all text between block below(names) without the title or end pattern but only names, ++++START++++ SCOTT TIGER HENRY PAUL JARED OTIENO OMOLLO JA NIGERIA ++++END++++ the names i want to return and store in a variable in... (1 Reply)
Discussion started by: Scott2000
1 Replies

4. Shell Programming and Scripting

Passing file content as parameter

Hi All, I am passing a file value as parameter to awk command; Par.txt A|B Input.txt A,1 B,3 C,4 D,5 My desired output should be A,1 B,3 (4 Replies)
Discussion started by: kmsekhar
4 Replies

5. Shell Programming and Scripting

Passing the parameters using a function

Hi All, I am new to shell scripting required some help in passing the parameter value to the shell script. I am writing a shell script, in the script I have created two functions as below. first function get_trend_ids () { Here I am connecting to the database and getting all the... (3 Replies)
Discussion started by: shruthidwh
3 Replies

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

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

8. UNIX for Advanced & Expert Users

Passing a unix variable value to a Plsql function

Suppose I have a unix variable called RGNM which is holding a value. Now I want to call a plsql function in my script. THis plsql function takes one IN parameter. I want to pass my UNIX VARIABLE Value to the plsql function. Can i just give it by giving $RGNM in the function after calling sqlplus... (1 Reply)
Discussion started by: cobroraj
1 Replies

9. UNIX for Dummies Questions & Answers

passing variable to function

Hi, I am trying to sum up numbered columns and in order to tidy up the program I have wrote a function to do the adding of some numbers. I have a problem though with passing a variable to the function in the UNIX bash shell. The function only gives the first number in the variable list and does... (4 Replies)
Discussion started by: Knotty
4 Replies

10. Shell Programming and Scripting

Passing a variable name to be created within a function

Is it possible to pass a variable name, as a parameter to a function, so it can be created within this function ? Something like this: func_uppercase abcdefgh var_name where the 1st parameter is the string I want to convert and the 2nd is the desired variable name... $2=`echo "$1" |... (2 Replies)
Discussion started by: 435 Gavea
2 Replies
Login or Register to Ask a Question