• Hey! Register here to create your account, engage with the community, and talk about what is new!

How to run scripts like executables

Prox

Moderator
Moderator
Joined
Sep 9, 2020
Messages
384
Reaction score
31
Welcome to my way of wasting time. Enjoy a tutorial on running scripts like executables.
  1. Install Linux.
    No, this isn't a joke. Linux, or at least any UNIX compliant/like operating system (basically, not Windows) will let you use the shebang symbol (#!) at the start of a file to essentially run a "runner" whenever running a script. While Windows might have the ability to do this in a basic way (have file endings be opened by certain programs) Windows will NOT let you pass the proper arguments to the shebang. If your scripts don't require arguments to the runner, go ahead and skip this step.
  2. Create the script.
    I mean, it's kinda necessary. Take the following JavaScript (Node) script:
    JavaScript:
    console.log("Hello, World!");
    When run, it outputs Hello, World!
  3. Shebang it.
    The shebang is simple enough to understand. First, start with #! and immediately follow it with the filepath to some executable. This executable should be able to run the script as an interpreter. For JavaScript, this is the NodeJS environment. To run Node, use #!/bin/node granted node is installed. For python, #!/bin/python or #!/bin/python3 should work, depending on the version of python you use. For simple bash scripts, use #!/bin/bash[icode]. If you would like to limit yourself heavily, use [icode]#!/bin/sh to strip away bash utilities.

    Note that this MUST be an interpreter. A compiler, like GCC or Clang, will not work for C. Likewise, rustc and cargo won't work for Rust. However, something like Cling would in fact work for C. Much to my dismay, there is no real interpreter for Rust yet.

    On Windows, simply set up the computer to run certain file types with certain programs. Try googling "windows change file type association." That'll do the trick. Alternatively, you can right click the file, go to properties, and change the "opens with" program if your file doesn't have a file ending, for example.

    Now, for what windows can't do: suppose the interpreter has flags like -Z or --no-output. If you want to use these, simply put them after the runner path. You can even just pass arguments to it. Suppose a file has this shebang: #!/bin/node --trace-exit. According to Node, the trace-exit option will show the stack trace when the program suddenly exits (error logging). As such, the file will be treated as a node script and have its errors traced properly.
  4. Make it executable.
    With Windows, you can just click twice and open the file. Linux, on the other hand, needs to ensure that the file is executable. First, open a command terminal. Suppose the path to your file is ~/Scripts/my_script.js. If so, run chmod +x ~/Scripts/my_script.js to make it executable. Now, if you do not "own" the folder the script is in (for example, if the path is /opt/super-cool-script/script, you'll need to use sudo, which is just a fancy way of saying "I know what I'm doing; give me full administrator permissions." Just run sudo chmod +x /opt/super-cool-script/script instead. Yes, running chmod on just anything is a bad idea; sometimes a file just shouldn't be runnable. For example, that text file you keep of your 2FA backup keys that you should have if you use 2FA. Don't run that. Also, encrypt the file or something with a password you can remember.
Is this useful? No. Most of you use Windows.
 
D

Deleted member 6869

Guest
Thanks, I will not use this cause I have windows installed and know java and not javascript
 

strawspider

Forum adept
Emperor
Joined
Nov 5, 2022
Messages
194
Reaction score
7
Welcome to my way of wasting time. Enjoy a tutorial on running scripts like executables.
  1. Install Linux.
    No, this isn't a joke. Linux, or at least any UNIX compliant/like operating system (basically, not Windows) will let you use the shebang symbol (#!) at the start of a file to essentially run a "runner" whenever running a script. While Windows might have the ability to do this in a basic way (have file endings be opened by certain programs) Windows will NOT let you pass the proper arguments to the shebang. If your scripts don't require arguments to the runner, go ahead and skip this step.
  2. Create the script.
    I mean, it's kinda necessary. Take the following JavaScript (Node) script:
    JavaScript:
    console.log("Hello, World!");
    When run, it outputs Hello, World!
  3. Shebang it.
    The shebang is simple enough to understand. First, start with #! and immediately follow it with the filepath to some executable. This executable should be able to run the script as an interpreter. For JavaScript, this is the NodeJS environment. To run Node, use #!/bin/node granted node is installed. For python, #!/bin/python or #!/bin/python3 should work, depending on the version of python you use. For simple bash scripts, use #!/bin/bash[icode]. If you would like to limit yourself heavily, use [icode]#!/bin/sh to strip away bash utilities.

    Note that this MUST be an interpreter. A compiler, like GCC or Clang, will not work for C. Likewise, rustc and cargo won't work for Rust. However, something like Cling would in fact work for C. Much to my dismay, there is no real interpreter for Rust yet.

    On Windows, simply set up the computer to run certain file types with certain programs. Try googling "windows change file type association." That'll do the trick. Alternatively, you can right click the file, go to properties, and change the "opens with" program if your file doesn't have a file ending, for example.

    Now, for what windows can't do: suppose the interpreter has flags like -Z or --no-output. If you want to use these, simply put them after the runner path. You can even just pass arguments to it. Suppose a file has this shebang: #!/bin/node --trace-exit. According to Node, the trace-exit option will show the stack trace when the program suddenly exits (error logging). As such, the file will be treated as a node script and have its errors traced properly.
  4. Make it executable.
    With Windows, you can just click twice and open the file. Linux, on the other hand, needs to ensure that the file is executable. First, open a command terminal. Suppose the path to your file is ~/Scripts/my_script.js. If so, run chmod +x ~/Scripts/my_script.js to make it executable. Now, if you do not "own" the folder the script is in (for example, if the path is /opt/super-cool-script/script, you'll need to use sudo, which is just a fancy way of saying "I know what I'm doing; give me full administrator permissions." Just run sudo chmod +x /opt/super-cool-script/script instead. Yes, running chmod on just anything is a bad idea; sometimes a file just shouldn't be runnable. For example, that text file you keep of your 2FA backup keys that you should have if you use 2FA. Don't run that. Also, encrypt the file or something with a password you can remember.
Is this useful? No. Most of you use Windows.
does it work on all UNIX based operating systems?
 
Top Bottom