Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Shell script with zx Library in Nodejs

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to write Shell scripts with zx library in Nodejs". In daily operation, I believe many people have doubts about how to write Shell scripts with zx libraries in Nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "how to write Shell scripts with zx libraries in Nodejs". Next, please follow the editor to study!

Shell script

Creating a shell script, that is, a script executed by shell, such as Bash or zsh, is a common way to automate repetitive tasks, especially for operators. It is a good choice for front-end engineers to write shell scripts in Node.js because it provides many core modules and can be imported into other front-end script libraries to reduce learning costs.

If you try to write a shell script that runs under Node.js without zx.js, you may find that it is not as smooth as you want it to be. You need to write special handling for child processes, pay attention to escaping command-line arguments, and then use standard output stdout and standard error stderr, which is not particularly intuitive, and scripting with shell becomes very clumsy.

The Bash shell scripting language is the best choice for writing shell scripts, there is no need to write code to handle child processes, and it has built-in language features for handling stdout and 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 deal with things like prompting the user for input.

Google's zx.js library helps you write shell scripts efficiently and happily with Node.js.

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

Installation

It is common for front-end engineers to install a dependency and run the following script:

Npm install zx

Use

Google's zx.js provides functions that encapsulate the creation of child processes and process stdout and stderr from those processes. The main function that will be used next is the $function, which uses zx.js to specify that the script be written to a file with a .mjs extension so that await can be used at the top level. If you are used to the .js extension, wrap the script in something like void async function () {...} ().

Let's start with the extension .mjs, with each .mjs file starting with the following code:

#! / usr/bin/env node

Let's implement the function of ls in a shell script and create a file ls.mjs. The complete code is as follows:

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

Like the shell script file, you need to make it executable:

Chmod + x. / ls.mjs

Let's execute the shell script written by this Node.js, and execute:

. / ls.mjs

Google's zx.js also provides other utility functions to simplify shell scripting, such as:

Cd (): allows you to change the wrapper of the readline module of the current working directory question (): Node.js, which can directly prompt the user for input.

#! / usr/bin/env nodeimport {$, cd} from "zx"; $.verbose = false; / / defaults to true and runs const output = (await $`ls`). Stdout.trim (); console.log (output); const dirName = "zx-mkdir-tmp"; await $`ls` {dirName} `; / / create directory cd (`. / ${dirName} `); const pwdOutput = (await $`pwd`). Stdout.trim (); console.log (pwdOutput); / / zx-mkdir-tmp

In addition to the practical features provided by zx.js, it also provides several popular scripting libraries, such as:

Chalk: allows you to add colors to the output of the script.

Minimist: a library that parses command-line arguments and exposes them under the argv object.

Fetch: you can use it to make HTTP requests.

Fs-extra: exposes the Node.js core fs module, as well as many other ways to make it easier to use the file system.

At this point, the study on "how to write Shell scripts with zx libraries in Nodejs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report