Home>Article>Web Front-end> This article talks about how to use the zx library to write Shell scripts in Nodejs

This article talks about how to use the zx library to write Shell scripts in Nodejs

青灯夜游
青灯夜游 forward
2022-01-19 19:42:23 3473browse

How to write Shell script in Node? The following article will introduce to you how to use the zx library to write Shell scripts inNode. I hope it will be helpful to you!

This article talks about how to use the zx library to write Shell scripts in Nodejs

Shell script

Create a shell script, that is, a script executed by the shell, such as Bash or zsh, is a common method used to automate repetitive tasks, especially for operation and maintenance personnel. For front-end engineers, using Node.js to write shell scripts is a good choice because it provides many core modules and can import other front-end script libraries, reducing learning costs.

If you want to try to write a shell script that runs under Node.js without the help ofzx.js, you may find that it is not as smooth as you hope. Special handling needs to be written for the subprocess, taking care to escape the command line arguments and then use stdoutstdoutand stderrstderr, it is not particularly intuitive and becomes very difficult to write using shell scripting clumsy.

The Bash shell scripting language is the best choice for writingshellscripts without the need to write code to handle subprocesses, and it has features for handlingstdoutand ## Built-in language features for #stderr. But writingshellscripts in Bash is not that easy, and the syntax can be quite confusing, making it less convenient to implement logic or handle things like prompting user input.

Google's

zx.jslibrary helps make writingshellscripts usingNode.jsefficient and enjoyable.

Official website: https://github.com/google/zx#-zx

Installation

For the front end For engineers, it is commonplace to install a dependency. Run the following script:

npm install zx

Using

Google's

zx.jsProvides functions that encapsulate the creation of child processes and the handling ofstdoutandstderrfrom these processes. The main function that will be used below is the$function. Usezx.jsto specify that the script is written into a file with the extension.mjsso that it can be used at the top levelawait. If you are used to the.jsextension, wrap your script in something likevoid async function () {...}().

Let’s first use the extension

.mjs. Each.mjsfile will start with the following code:

#! /usr/bin/env node

Let’s implement it below The function of

lsin ashellscript creates the filels.mjs. The complete code is as follows:

#! /usr/bin/env node import { $ } from "zx"; $.verbose = false; const output = (await $`ls`).stdout.trim(); console.log(output);

and

shellSame as the script file, it needs to be converted into an executable file:

chmod +x ./ls.mjs

Now let’s execute this

shellscript written in Node.js, execute:

./ls.mjs

This article talks about how to use the zx library to write Shell scripts in Nodejs

Google's

zx.jsalso provides other practical functions to simplify shell script writing, such as:

cd(): Allow Change the current working directoryquestion(): A wrapper for Node.js's readline module that can directly prompt the user for input.

#! /usr/bin/env node import { $, cd } from "zx"; $.verbose = false; // 默认为true,以详细模式运行 const output = (await $`ls`).stdout.trim(); console.log(output); const dirName = "zx-mkdir-tmp"; await $`mkdir ${dirName}`; // 创建目录 cd(`./${dirName}`); const pwdOutput = (await $`pwd`).stdout.trim(); console.log(pwdOutput); // zx-mkdir-tmp

In addition to the practical functions provided by

zx.js, it also provides several popular script libraries, such as:

For more node-related knowledge, please visit:

nodejs tutorial! !

The above is the detailed content of This article talks about how to use the zx library to write Shell scripts in Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete