Home>Article>Web Front-end> This article talks about how to use the zx library to write Shell scripts in Nodejs
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!
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 stdoutstdout
and 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 writingshell
scripts without the need to write code to handle subprocesses, and it has features for handlingstdout
and ## Built-in language features for #stderr. But writing
shellscripts 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.
zx.jslibrary helps make writing
shellscripts usingNode.jsefficient and enjoyable.
Installation
For the front end For engineers, it is commonplace to install a dependency. Run the following script:npm install zx
Using
Google'szx.jsProvides functions that encapsulate the creation of child processes and the handling of
stdoutand
stderrfrom these processes. The main function that will be used below is the
$function. Use
zx.jsto specify that the script is written into a file with the extension
.mjsso that it can be used at the top level
await. If you are used to the
.jsextension, wrap your script in something like
void async function () {...}().
.mjs. Each
.mjsfile will start with the following code:
#! /usr/bin/env nodeLet’s implement it below The function of
lsin a
shellscript 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);and
shellSame as the script file, it needs to be converted into an executable file:
chmod +x ./ls.mjsNow let’s execute this
shellscript written in Node.js, execute:
./ls.mjsGoogle's
zx.jsalso provides other practical functions to simplify shell script writing, such as:
cd(): Allow Change the current working directory
question(): 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-tmpIn addition to the practical functions provided by
zx.js, it also provides several popular script libraries, such as:
object.
module, along with many other ways to make working with the file system easier.
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!