LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2010, 09:30 AM   #1
amlinux
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
Smile How to change the signature of all the functions in my scripts...


Hi all,

I am new to this community and i dont have much knowledge to linux shell scripting....this question may be a simple one...

I was trying to write a script to change the signature of the all the methods in a set of scripts.. here is a sample of the same :

old signature :

methodname()
{
method-body
}

New signature:

function methodname
{
method-body
}


That is i need to insert "function" keyword in front of the methodname and remove the () after the methodname.
Can anyone help me out with this.
Thanks in advance..
 
Old 03-23-2010, 11:23 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 164Reputation: 164
Well you could do it with sed-- sed -i 's/^methodname()$/function methodname/' filename.sh

Last edited by rweaver; 03-23-2010 at 11:25 AM. Reason: forgot to add filename... you could also use a wildcard
 
Old 03-23-2010, 02:11 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,620

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450
How about

Code:
sed -i 's/^\([^(]*\)() *$/function \1/' filename
This will also change it if the line to be edited has trailing spaces.
Make a backup first, I did not test the command.

[EDIT]
P.S.: The above command will only work correctly, if the file in question only contains old-signature-methods. Let us know if your files have 'mixed' signature methods.

Last edited by crts; 03-23-2010 at 02:15 PM.
 
1 members found this post helpful.
Old 03-24-2010, 08:36 AM   #4
amlinux
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Smile Thanks

Thanks for the suggestions....i tried

sed -i 's/^\([^(]*\)() *$/function \1/' filename

it worked
Thanks once again...


i have one more question.....

i am trying to find all the files having
#!/bin/ksh
as their first line and store the list of all such files in a varaible(or array).this i need so that i can change the signature of the methods(of which i have talked earlier)in all the scripts present in my project folder.


example :
i have a folder tests
inside this folder i have many files and directories..

abc.sh
a1.sh
b1 (shell script)
dl (directory)
ab.xml

i want to run my script which changes the signature of all the methods in all the scripts present in the directory tests.

I tried the following command
#!/bin/ksh

DIR="."

files=$('find . -exec grep -l '#!/bin/ksh' {}\;')

for X in $files
do
sed -i 's/^\([^(]*\)() *$/function \1/' $X
done


This gives the following error:
./trail.sh[5]: find . -exec grep -l #!/bin/ksh {}\;: not found [No such file or directory]

Can anyone please guide me how to do this...

Thanks in advance..
 
Old 03-24-2010, 10:15 AM   #5
nonamenobody
Member
 
Registered: Oct 2002
Posts: 138

Rep: Reputation: 22
Code:
files=$('find . -exec grep -l '#!/bin/ksh' {}\;')
You are trying to use un-escaped single quotes inside. The simple solution is to remove the outer set of single quotes, as they aren't necessary (at least in bash, not certain about ksh it's a long time since I used it). E.g.:
Code:
files=$(find . -type f -exec grep -l '#!/bin/ksh' {} \;)
I also added a "-type f" because I imagine you only want to grep files.

Last edited by nonamenobody; 03-24-2010 at 10:22 AM. Reason: Oops, extra apostrophe and forgot {}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help, Linux boot problem, kernel panic, "cant open '/scripts/functions' deranjer Linux - Desktop 1 04-29-2010 10:45 AM
How to Write & Call Functions Within Shell Scripts senthilmuthiah Linux - Newbie 2 03-27-2009 07:36 PM
Where do i change my signature? randyriver10 General 1 07-12-2004 03:21 PM
Linked Lists: Pointers sent to functions as arguments do not change their valur Jose Muņiz Programming 3 01-12-2004 08:45 PM
script to change signature file in pine ashesh Linux - General 1 06-13-2003 07:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration