Menu

#6 UNIX Shebang functionality

open
nobody
5
2022-02-09
2018-06-12
No

I'd like to be able to make scripts executable on UNIX-like systems by having the first line of the script include the interpreter, in the UNIX tradition:

1
2
3
4
#!/usr/bin/bwbasic
FOR I = 1 TO 10
    PRINT I
NEXT I

This is called "shebang" or "hashbang". https://en.wikipedia.org/wiki/Shebang_(Unix)

The script also has to be made executable: chmod a+x <script-name>

This feature allows you to run the script by just typing its name rathar than having to type "bwbasic <script-name>. But, it not just that convenience, this feature also allows you to put scripts in a directory in your PATH and find them without having to type their full path name. I general, it would allow bwbasic scripts to be run just like any other UNIX command, making them a first class citizen of the system.

Currently the "#" on the first line causes an error with bwbasic. Making this work will require adding a special case in the code just for the "#" on the first line.

Discussion

  • Bill Chatfield

    Bill Chatfield - 2018-06-12

    The correct link is: Shebang

     
  • AF5NE

    AF5NE - 2019-07-02

    This functionality has been added to bwBASIC 3.30.. Here is the fix for bwb_fload() in bwbasic.c:

        My->NextValidLineNumber = MINLIN;
        while (fgets (tbuf, tlen, file))        /* bwb_fload */
        {
          tbuf[tlen] = NulChar;
          CleanLine (tbuf);
          if (is_empty_string (tbuf))
          {
            /* ignore */
    +     }
    +     else if (tbuf[0] == '#' && tbuf[1] == '!')
    +     {
    +       /* #!/usr/bin/bwbasic */
          }
          else if (bwb_strnicmp (tbuf, Magic_Word, Magic_Length) == 0)
    
     

    Last edit: AF5NE 2019-07-02
    • Daniel D. Rodrigues

      Hey Howard, can you share the code for bwbasic 3.30? Also, FYI, I have created a GitHub repository: https://github.com/nerun/bwbasic. If you want access, let me know.

       
  • AF5NE

    AF5NE - 2019-07-02

    Here is the testcase (SHEBANG1.BAS) added to bwBASIC 3.30:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    #!/usr/bin/bwbasic
    100 REM ------------------------------------------------------------
    110 REM   PURPOSE: VERIFY SHEBANG (#!) IS A COMMENT
    120 REM    AUTHOR: HOWARD WULF, AF5NE
    130 REM      DATE: 2019-07-01
    140 REM  FILENAME: SHEBANG1.BAS
    150 REM 
    200 FOR I = 1 TO 10
    210   PRINT I
    220 NEXT I
    999 PRINT "TEST PASSED"
    
     

    Last edit: AF5NE 2019-07-02
  • Bill Chatfield

    Bill Chatfield - 2020-08-15

    Thanks! You rock! Now I just have to find out why Fedora is still shipping bwBASIC 3.20.

     
  • Bill Chatfield

    Bill Chatfield - 2020-08-15

    I guess you could close the ticket now.

     
  • Paul Edwards

    Paul Edwards - 2020-08-15

    I think we need to wait for bwBASIC 3.30 to actually be released. I don't have all the code from Howard yet.

     
  • Bill Chatfield

    Bill Chatfield - 2020-08-16

    Oh. I see. I didn't realize it was still in progress.

     

Log in to post a comment.