LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to change the signature of all the functions in my scripts... (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-change-the-signature-of-all-the-functions-in-my-scripts-797288/)

amlinux 03-23-2010 08:30 AM

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

rweaver 03-23-2010 10:23 AM

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

crts 03-23-2010 01:11 PM

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.

amlinux 03-24-2010 07:36 AM

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

nonamenobody 03-24-2010 09:15 AM

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.


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