Solved Should I write all of my scripts in JavaScript?

  • Thread starter Deleted member 63539
  • Start date
D

Deleted member 63539

Guest
Shell is too hard for me. I'm porting a JavaScript interpreter could be run with #!/usr/bin/env js, it's written in C, it's small and fully support the ES2020 specification. The real value of it is it's a real interpreter, doesn't compile to C and relying on C compiler to compile and run the binary. That make it could be used like python and as a real scripting solution.

I come from Java, know a bit of C. I dislike JS, too. But if having to choose between shell and JS, I prefer JS. I like a full language that could be used as itself without relying on extra small utility with awful syntax like sed, awk to done the job. Yes, I know it's against the Unix philosophy. But I don't care about philosophy anyway.

On Windows, I used Thinbasic. Is there anything like that on Unix or we forced to stick with the Unix philosophy? I guess it's python.
 
Most people who don't use shell scripts use Python for scripting instead. I'd really suggest using Python, since it's more well-suited to general programming than JavaScript, which was built for web.
 
You are trying to swim against the current. Javascript is quite clumsy for such tasks and you have to write way more code to do simple things. And there's the feeling you have running js or vbs'es on Windows - with Unix shell scripting is so much more powerful.
I support Shadow53's suggestion to use Python, or better yet - learn zsh. I have programmed whole products on pure zsh, it's awesome.
 
The beauty of real shell is that it integrates so well with running external commands (real programs). For example, if programs foo, bar and blatz do something and/or set their exist status, then the following concise lines:
Code:
variable=`foo -option argument`
if [ `bar` == 42 ] ; then ... else ... fi
if blatz ; then ... else ... fi
will all do sensible things: put the output of the program into a variable, check the output of a program, and do something if a program returns a zero or nonzero exit code. All this is much harder in Python, and I don't know how JS integrates with running outboard programs.

Honestly, anyone who can program in Java, is willing to learn JS, should also be able to learn a scripting language. I would use good old /bin/sh as the scripting language to start with (not bash or zsh, they have way too many "interesting" features that are convenient but confusing).

The other thing one needs to know is when to use which tool. For something that can be done in 10 or 100 lines, and is heavy on running programs, a shell script is great. For something that's 1000 to 10K lines, and occasionally runs external programs, but is mostly code, Perl or Python (or Rexx or Ruby or JS) are good. For 100K to 10M lines, the choice of programming language is the least of your problems; to write such a program you need a team of experts, and they will solve the programming language choice as one of the minor assignments.
 
Sometimes, learning hard things are worth the effort. But you won't know that until you have learned <hard thing>...
 
gh_origin: what kind of scripts do you want to write?

For simple scripting, I use sh, but Python, Lua, Tcl or Perl are fine too.
When it gets more complex (i.e. closer to fully-fledged programming than to bare scripting), I use Java and it becomes simple. :)

However, if you really want to use JS, you should consider NodeJS: it is already available (no need to port anything) and it already has everything you'll ever need (just npm install it).
 
When it gets more complex (i.e. closer to fully-fledged programming than to bare scripting), I use Java and it becomes simple.

I am not sure. I know few about object oriented programming and have no experience.
But I can imagine that tcl's object oriented programming capabilities are more powerful
combined with tcl's capability of dynamically modifying the programs behaviour. That
may not be the case of java that is compiled to bytecode. And does java has a X11 toolkit
like tk? With tk is very easy to write GUIs

My last use of tcl/tk is together with sql. Sqlite3 and tcl integrate very good, so that databases
can be used as file format of the application. The combination is really powerful.
 
  • Like
Reactions: a6h
extra small utility with awful syntax like sed, awk to done the job.

For most things Awk and Javascript have very similar syntax

Code:
# Awk
for(i = 0; i < 10; i++)
{
  print("Hello Number: " i)
}

Code:
// JS
for(var i = 0; i < 10; i++)
{
  console.log("Hello Number: " + i);
}

However neither make it particularly easy to get data straight from a pipe unlike /bin/sh

Code:
FILES=$(ls "$HOME")
# or via backticks
FILES=`ls "$HOME"`

From what I have experienced, Perl is the closest step up from /bin/sh. i.e the backtick stuff works in that

Code:
my $FILES = `ls "$ENV{'HOME'}"`;

Unfortunately the rest of the Perl stuff can often not make much sense unless you have followed its evolution or follow the UNIX philosophy.

/bin/sh is great for simple simple scripts (and moderate scripts once you get good at it). For anything larger, I recommend Awk. Mostly for three reasons. The first is that it is part of Busybox so easy to get running on Windows (if you need to port any scripts). The second is that it isn't extensible. No dumb PIP, CPAN, NPM, Crates repositories to tempt people to drag in a load of sh*t from. Finally it is also part of the UNIX (and POSIX) standard which means it has to exist on standards compliant operating systems. (Even broken ones like SystemdOS must include it too ;))
 
Java provides support for reflection, aka. introspection: you can access the full definition of any object you have a pointer to, and you can read and modify its properties and invoke its methods without having its source code. If an object implements an interface, you can also programmatically generate "proxy objects" implementing the same interface and pass these instead of the original ones to any piece of code expecting the interface. If you like coding magical features, you'll love Java. :)

Java also ships from the start with a terrific standard class library. Moreover, many open source tools, frameworks and libraries have been created over time, some of them even becoming industry standards. Almost anything you may need already exists and is freely available. And yes, Java comes with a standard API to write GUI, other API being available as open source projects.

However, it takes time to learn Java. It is easy if you already know a C-like language and are familiar with OOP, but it still takes time because it is so rich. Furthermore, you'll have to learn conventions (e.g. Java beans, design patterns) and tools (e.g. Maven, JUnit, Spring Boot, Hibernate) that became implicit requirements over time, though not part of Java itself.

As for myself, I can use Java to write small applications because I already know it, but if I didn't, it wouldn't be worth learning it because Java has preferential use cases. If you're writing an enterprise application, for instance, Java will undoubtedly make your life easier and safer, it has been tailored and optimized for this.

For other types of development, TCL/TK is probably a better choice, as is Python. Both will be able to run your application on any platform where an interpreter is available (as with Java), and you have much more power under the hood than in languages primarily designed for scripting, making it easy and possible to develop sophisticated features, all this with a much shorter learning curve than Java.

I am not sure. I know few about object oriented programming and have no experience.
But I can imagine that tcl's object oriented programming capabilities are more powerful
combined with tcl's capability of dynamically modifying the programs behaviour. That
may not be the case of java that is compiled to bytecode. And does java has a X11 toolkit
like tk? With tk is very easy to write GUIs

My last use of tcl/tk is together with sql. Sqlite3 and tcl integrate very good, so that databases
can be used as file format of the application. The combination is really powerful.
 
Back
Top