Home > Web Front-end > JS Tutorial > 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

青灯夜游
Release: 2022-01-19 19:42:23
forward
3626 people have browsed it

How to write Shell script in Node? The following article will introduce to you how to use the zx library to write Shell scripts in Node. 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 of zx.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 stdout stdout and stderr stderr, 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 writing shell scripts without the need to write code to handle subprocesses, and it has features for handling stdout and ## Built-in language features for #stderr. But writing shell scripts 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.js library helps make writing shell scripts using Node.js efficient 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
Copy after login

Using

Google's

zx.js Provides functions that encapsulate the creation of child processes and the handling of stdout and stderr from these processes. The main function that will be used below is the $ function. Use zx.js to specify that the script is written into a file with the extension .mjs so that it can be used at the top level await. If you are used to the .js extension, wrap your script in something like void async function () {...}().

Let’s first use the extension

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

#! /usr/bin/env node
Copy after login

Let’s implement it below The function of

ls in a shell script creates the file ls.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);
Copy after login

and

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

chmod +x ./ls.mjs
Copy after login

Now let’s execute this

shell script written in Node.js, execute:

./ls.mjs
Copy after login

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

Google's

zx.js also 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
Copy after login

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template