Loading ...
Sorry, an error occurred while loading the content.
 

shell syntax highlighting

Expand Messages
  • Jeroen Valcke
    Hello, I m editing a (Bourne) shell script with vim. The code is highlighted. However I notice some weird behaviour with the tokens $( Vim puts these in red.
    Message 1 of 9 , Apr 30, 2001
      Hello,

      I'm editing a (Bourne) shell script with vim. The code is highlighted.
      However I notice some weird behaviour with the tokens $( Vim puts these in
      red. Put why? Because i=$(($i+1)) is a correct statement. Why then but the
      $( and the last ) in red?

      --
      Jeroen Valcke jeroen@...
      ICQ# 30116911 Home page: http://www.valcke.com/jeroen
      Phone +32(0)56 32 91 37 Mobile +32(0)486 88 21 26
      It's not easy, being green -Kermit the Frog
    • Charles E. Campbell
      Jeroen Valcke calmly inquired... JV I m editing a (Bourne) shell script with vim. The code is highlighted. JV However I notice some weird behaviour with the
      Message 2 of 9 , Apr 30, 2001
        Jeroen Valcke calmly inquired...
        JV> I'm editing a (Bourne) shell script with vim. The code is highlighted.
        JV> However I notice some weird behaviour with the tokens $( Vim puts these
        JV> in red. Put why? Because i=$(($i+1)) is a correct statement. Why then
        JV> but the $( and the last ) in red?

        ---------------------------------------------------------------------
        #! /bin/sh
        i=1
        echo "i=" $i
        i=$(($i+1))
        echo "i=" $i
        ---------------------------------------------------------------------

        When running the script above the Bourne shell responds:
        i= 1
        tmp: Syntax error at line 4: `i=$' unexpected

        Changing shells to the Korn shell (ksh) and re-running:
        i= 1
        i= 2

        Now there's no error and no highlighting error with ksh; however,
        there's both a runtime error and a highlighting thereof with the
        Bourne shell. Bash complains about the script, too, by the way
        (1+1: command not found), but this error isn't syntactical and
        so <sh.vim> doesn't catch it.

        Regards,
        C Campbell

        --
        Charles E Campbell, Jr, PhD _ __ __
        Goddard Space Flight Center / /_/\_\_/ /
        cec@... /_/ \/_//_/
        PGP public key: http://www.erols.com/astronaut/pgp.html
      • Jeroen Valcke
        ... I get no errors when running this script. Output is: Tomsk:~/bin$ sh testshell.sh i= 1 i= 2 I was unther the impression that the statement i=$(($i+1)) is
        Message 3 of 9 , Apr 30, 2001
          On Mon, Apr 30, 2001 at 02:10:25PM -0400, Charles E. Campbell wrote:
          > ---------------------------------------------------------------------
          > #! /bin/sh
          > i=1
          > echo "i=" $i
          > i=$(($i+1))
          > echo "i=" $i
          > ---------------------------------------------------------------------
          >
          > When running the script above the Bourne shell responds:
          > i= 1
          > tmp: Syntax error at line 4: `i=$' unexpected

          I get no errors when running this script.
          Output is:
          Tomsk:~/bin$ sh testshell.sh
          i= 1
          i= 2

          I was unther the impression that the statement i=$(($i+1)) is correct.
          I found a reference to this command in the book 'Beginning Linux
          Programming 2nd Ed'
          I quote:
          Arithmetic Expansion
          We've already used the expr command, which allows simple arithmetic
          commands to be processed, but this is quite slow to execute, since a new
          shell is invoked to process the expr command.
          A newer and better alternative is $((...)) expansion. By enclosing the
          expression we wish to evaluate in $((...)), we can perform arithmetic much
          more efficiently:

          #!/bin/sh

          x=0
          while [ "$x" -ne 10 ]; do
          echo $x
          x=$((x+1))
          done

          exit 0

          --
          Jeroen Valcke jeroen@...
          ICQ# 30116911 Home page: http://www.valcke.com/jeroen
          Phone +32(0)56 32 91 37 Mobile +32(0)486 88 21 26
          Friends may come and go, but enemies accumulate. -Thomas Jones
        • Juergen Salk
          ... $((...)) is a valid expression in bash. On Linux systems /bin/sh is just a symlink to /bin/bash. On other (commercial) Unix systems /bin/sh is Bourne shell
          Message 4 of 9 , Apr 30, 2001
            * Jeroen Valcke <jeroen@...> [010430 22:09]:

            > I was unther the impression that the statement i=$(($i+1)) is correct.
            > I found a reference to this command in the book 'Beginning Linux
            > Programming 2nd Ed'
            > I quote:
            > Arithmetic Expansion
            > We've already used the expr command, which allows simple arithmetic
            > commands to be processed, but this is quite slow to execute, since a new
            > shell is invoked to process the expr command.
            > A newer and better alternative is $((...)) expansion. By enclosing the
            > expression we wish to evaluate in $((...)), we can perform arithmetic much
            > more efficiently:

            $((...)) is a valid expression in bash. On Linux systems /bin/sh is just a
            symlink to /bin/bash. On other (commercial) Unix systems /bin/sh is Bourne
            shell (or POSIX shell) which differs from bash in many ways.

            Best regards - Juergen.
          • Charles E. Campbell
            ... You re not really using a Bourne shell; its probably either pdksh or bash, even if it is named sh. Try putting let is_kornshell=1 or let is_bash=1 in
            Message 5 of 9 , Apr 30, 2001
              Thus saith Jeroen Valcke:
              > I get no errors when running this script.
              > Output is:
              > Tomsk:~/bin$ sh testshell.sh
              > i= 1
              > i= 2
              >
              > I was unther the impression that the statement i=$(($i+1)) is correct.
              > I found a reference to this command in the book
              > 'Beginning Linux Programming 2nd Ed'

              You're not really using a Bourne shell; its probably either pdksh or
              bash, even if it is named sh. Try putting "let is_kornshell=1" or
              "let is_bash=1" in your <.vimrc>; that will tell <sh.vim> to
              highlight it with the ksh/bash extensions (which include $((...)) ).

              Regards,
              C Campbell

              --
              Charles E Campbell, Jr, PhD _ __ __
              Goddard Space Flight Center / /_/\_\_/ /
              cec@... /_/ \/_//_/
              PGP public key: http://www.erols.com/astronaut/pgp.html
            • Jeroen Valcke
              ... Indeed, in the sh.vim file there s a reference too $((...)) on line 115. It should be executed when is_bash is set. However I have the line let is_bash=1
              Message 6 of 9 , Apr 30, 2001
                On Mon, Apr 30, 2001 at 04:32:36PM -0400, Charles E. Campbell wrote:
                > bash, even if it is named sh. Try putting "let is_kornshell=1" or
                > "let is_bash=1" in your <.vimrc>; that will tell <sh.vim> to
                > highlight it with the ksh/bash extensions (which include $((...)) ).

                Indeed, in the sh.vim file there's a reference too $((...)) on line 115.
                It should be executed when is_bash is set. However I have the line "let
                is_bash=1" in my .vimrc and still it colors the $(...) red?
                How is this possible?
                Thanks for helping me out so far.

                --
                Jeroen Valcke jeroen@...
                ICQ# 30116911 Home page: http://www.valcke.com/jeroen
                Phone +32(0)56 32 91 37 Mobile +32(0)486 88 21 26
                The good thing about standards is that there are so many to choose from.
                -Andrew S. Tanenbaum-
              • Benji Fisher
                ... I think that you want ... in your vimrc file. The variables b:is_bash and b:is_kornshell should be local to the buffer, not set in the vimrc file. (And
                Message 7 of 9 , Apr 30, 2001
                  Jeroen Valcke wrote:

                  > On Mon, Apr 30, 2001 at 04:32:36PM -0400, Charles E. Campbell wrote:
                  >
                  >> bash, even if it is named sh. Try putting "let is_kornshell=1" or
                  >> "let is_bash=1" in your <.vimrc>; that will tell <sh.vim> to
                  >> highlight it with the ksh/bash extensions (which include $((...)) ).
                  >
                  > Indeed, in the sh.vim file there's a reference too $((...)) on line 115.
                  > It should be executed when is_bash is set. However I have the line "let
                  > is_bash=1" in my .vimrc and still it colors the $(...) red?
                  > How is this possible?
                  > Thanks for helping me out so far.

                  I think that you want

                  :let bash_is_sh = 1

                  in your vimrc file. The variables b:is_bash and b:is_kornshell should be
                  local to the buffer, not set in the vimrc file. (And you probably do not
                  need them.)

                  :help sh.vim

                  HTH
                  --Benji Fisher
                • Charles E. Campbell
                  Thus saith Charles E. Campbell: CC bash, even if it is named sh. Try putting let is_kornshell=1 or CC let is_bash=1 in your ; that will tell
                  Message 8 of 9 , May 1, 2001
                    Thus saith Charles E. Campbell:
                    CC> bash, even if it is named sh. Try putting "let is_kornshell=1" or
                    CC> "let is_bash=1" in your <.vimrc>; that will tell <sh.vim> to
                    CC> highlight it with the ksh/bash extensions (which include $((...)) ).

                    Thus saith Jeroen Valcke:
                    JV> Indeed, in the sh.vim file there's a reference too $((...)) on line 115.
                    JV> It should be executed when is_bash is set. However I have the line "let
                    JV> is_bash=1" in my .vimrc and still it colors the $(...) red?
                    JV> How is this possible?

                    Hmm, looks like there's a bug with 6.0's <sh.vim>; the
                    is_kornshell/is_bash global variables weren't being used
                    properly. I've sent a New&Improved version to Bram, and
                    its available at my website (http://www.erols.com/astronaut/vim ;
                    check out the Links at the bottom of the page). It
                    won't work with 5.7, though.

                    Since you're on the [vim] mailing list, does that mean that
                    you're using 5.7? If so, try putting the following line
                    in your <.vimrc>:

                    let b:is_bash=1

                    That's only a partial fix because bringing up the file
                    via :e filename for example won't have b:is_bash
                    set. In that case, try

                    :syn clear
                    :let b:is_bash=1
                    :so ..path../vim57/synax/sh.vim

                    Regards,
                    Chip Campbell

                    --
                    Charles E Campbell, Jr, PhD _ __ __
                    Goddard Space Flight Center / /_/\_\_/ /
                    cec@... /_/ \/_//_/
                    PGP public key: http://www.erols.com/astronaut/pgp.html
                  • Jeroen Valcke
                    ... Works fine! I m using VIM 5.7.28 Thanks a lot. -- Jeroen Valcke jeroen@valcke.com ICQ# 30116911 Home page:
                    Message 9 of 9 , May 1, 2001
                      On Tue, May 01, 2001 at 09:20:09AM -0400, Charles E. Campbell wrote:
                      > Since you're on the [vim] mailing list, does that mean that
                      > you're using 5.7? If so, try putting the following line
                      > in your <.vimrc>:
                      >
                      > let b:is_bash=1

                      Works fine! I'm using VIM 5.7.28
                      Thanks a lot.

                      --
                      Jeroen Valcke jeroen@...
                      ICQ# 30116911 Home page: http://www.valcke.com/jeroen
                      Phone +32(0)56 32 91 37 Mobile +32(0)486 88 21 26
                    Your message has been successfully submitted and would be delivered to recipients shortly.