Operating System - Linux
1753880 Members
7182 Online
108809 Solutions
New Discussion юеВ

Re: Scripting - conditional statments

 
Krish_4
Contributor

Scripting - conditional statments

Hi,

Our script has multiple if statments. Is there any way to find out for a particular if statment where is the closing "fi" statment in vi editor?

Thanks.
7 REPLIES 7
Steven E. Protter
Exalted Contributor

Re: Scripting - conditional statments

Shalom,

You can install and compile the ehanced vi editor, designed for Linux. Or you can try and compile emacs.

I do it the old fashioned way. I indent every if and make sure the concluding fi is indented the same number of characters.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sandman!
Honored Contributor

Re: Scripting - conditional statments

I don't know of any way for the if...fi statements. The "%" operator is used for matching the current parenthesis, braces or brackets.

cheers!
Kent Ostby
Honored Contributor

Re: Scripting - conditional statments

here's an awk script to do it if you are having trouble with one particular if or fi.

Create a file called countem.awk

/ if / {addit++; print addit, $0;next;}
/^if / {addit++; print addit, $0;next;}
/ fi / {print addit,$0; addit--;next;}
/^fi / {print addit,$0; addit--;next;}
/^fi$/ {print addit,$0; addit--;next;}
/ fi$/ {print addit,$0; addit--;next;}
{print $0}

Then run: awk -f countem.awk < myfile | more

If you use elses, you may need to make some modifications. Basically you count up and then print the ifs and print then count down the fis.

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Krish_4
Contributor

Re: Scripting - conditional statments

Thanks for replying.

I like steve's suggestion to install emacs.

Steve - Once it is installed, how to do it? Is there any command like "%" in vi?

Thanks.
David DiBiase
Frequent Advisor

Re: Scripting - conditional statments

I use gmacs and xemacs. I just place my cursor at the if and it will highlight the body until the fi
dirk dierickx
Honored Contributor

Re: Scripting - conditional statments

emacs sound a bit overkill for writing scripts doesn't it? in this case i would go with vim. but hey, let's not start the holy war here ;)
Muthukumar_5
Honored Contributor

Re: Scripting - conditional statments

% Move to the parenthesis or brace that matches the
parenthesis or brace at the current cursor position. Read man vi.

Hope vi is not supporting this. Use emacs.

--
Muthu
Easy to suggest when don't know about the problem!