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.. |
Well you could do it with sed-- sed -i 's/^methodname()$/function methodname/' filename.sh
|
How about
Code:
sed -i 's/^\([^(]*\)() *$/function \1/' filename 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. |
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.. |
Code:
files=$('find . -exec grep -l '#!/bin/ksh' {}\;') Code:
files=$(find . -type f -exec grep -l '#!/bin/ksh' {} \;) |
All times are GMT -5. The time now is 03:35 PM. |