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.
 

Shadow53

Member

Reaction score: 19
Messages: 33

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.
 

roccobaroccoSC

Aspiring Daemon

Reaction score: 210
Messages: 722

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.
 

drhowarddrfine

Son of Beastie

Reaction score: 2,346
Messages: 4,311

Shell is too hard for me.
So you think JavaScript will be easier?! JavaScript is its own can of worms on a larger scale of worms that needs an(other) interpreter or compiler to work. Not available everywhere or even expected. Shell scripting is simple.
 

ralphbsz

Son of Beastie

Reaction score: 2,342
Messages: 3,236

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.
 

richardtoohey2

Aspiring Daemon

Reaction score: 312
Messages: 630

I like Tcl too.

That thinBASIC was interesting (for Windows tasks); not heard of that before.

If you like BASIC, there are variants that are cross-platform e.g. FreeBASIC

It has OS commands e.g.:


I've NOT used it myself apart from an initial tinker a couple of years ago, so don't know how good it actually is, or whether it will be what you are looking for.
 

tingo

Son of Beastie

Reaction score: 638
Messages: 2,543

Sometimes, learning hard things are worth the effort. But you won't know that until you have learned <hard thing>...
 

20-100-2fe

Well-Known Member

Reaction score: 364
Messages: 303

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).
 

hruodr

Aspiring Daemon

Reaction score: 277
Messages: 889

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.
 

kpedersen

Son of Beastie

Reaction score: 2,087
Messages: 2,951

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 ;))
 

Mjölnir

Daemon

Reaction score: 1,503
Messages: 2,114

Preliminary answer to the original question: NO. ;) If you don't like shell, use Python or LUA.
P.S. Do not call awk(1)'s syntax awful on a UNIX forum. It's very clear & it's awk-ful. Concerning sed(1), any programmer should know regular expression syntax (re_format(7)) anyway.
 

20-100-2fe

Well-Known Member

Reaction score: 364
Messages: 303

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.
 

kpedersen

Son of Beastie

Reaction score: 2,087
Messages: 2,951

I would keep it simple and minimize your dependencies. I’d vote for sh or lua since it’s in base.

Did you mean sh and awk? Admittedly I am running a fairly old version of FreeBSD on this machine but Lua doesn't seem to be on here.
 
Top