bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash style of if-then-else?


From: Michael Witten
Subject: Re: Bash style of if-then-else?
Date: Thu, 23 Sep 2010 16:38:42 -0500

On Mon, Aug 23, 2010 at 16:40, Eric Blake <eblake@redhat.com> wrote:
> On 08/23/2010 03:29 PM, Peng Yu wrote:
>>
>> Hi,
>>
>> I'm wondering if there is a widely accepted coding style of bash scripts.
>>
>> lug.fh-swf.de/vim/vim-bash/StyleGuideShell.en.pdf
>>
>> I've seen the following style. Which is one is more widely accepted?
>>
>> if [ -f $file]; then
>>    do something
>> fi
>>
>> if [ -f $file];
>> then
>>    do something
>> fi
>
> Neither.  You're missing adequate quoting in case $file contains spaces.
>  More importantly, when using '[', the trailing ']' has to be its own
> argument.  Personally, I tend to use the style with fewer lines:
>
> if [ -f "$file" ]; then
>  do something
> fi

This is also possible:

  [ -f "$file" ] && do_something

or perhaps:

  [ -f "$file" ] && {
    do_something_0
    do_something_1
  }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]