scripting 相关问题

脚本编写是一种编程形式,通常以低正式性,松散类型和不需要显式编译为特征。有许多脚本语言,它们用于各种场景 - 命令行应用程序,GUI,服务器端应用程序,扩展模块。

Codesys 脚本引擎工作脚本,用于构建项目并将其部署到硬件

我想使用脚本引擎自动化 Codesys 项目。我尝试使用该网站来理解文档。 PrintDeviceTree.py 脚本正在运行,但我没能成功...

回答 1 投票 0

git:转到工作树根的快速命令

我想要一个简单的 git 命令来进入存储库的“根”。 我从一个脚本开始,但发现我无法更改 shell 的活动目录,我必须执行一个函数。不幸的是...

回答 9 投票 0

如何使用 iMacros 提取 Google 搜索中某个单词的所有实例?

//声明宏 var 宏IronMaiden; MacroIronMaiden =“代码:”; MacroIronMaiden +="版本构建=7500718 RECORDER=FX"+" ”; MacroIronMaiden +="设置!

回答 1 投票 0

用于从计算机中删除用户的 Powershell 脚本

我编写了一个脚本,使我能够删除与计算机相关的多个用户配置文件和注册表项。这是为了工作,多人共享电脑

回答 3 投票 0

如何在 shell 脚本中使用一个命令的输出作为另一个命令(也不是一个命令的参数)

我需要从文件中获取一个值,该值定义为variable1=value1。 所以如果我做 cat file | grepvariable1 我得到variable1=value1。 我想在脚本中执行此操作,以便变量 1 是

回答 1 投票 0

Bash 脚本(cron 作业?)来处理上传到目录的文件?

假设我有一个名为 jpeg2txt 的工具。我需要编写一个 bash 脚本: 不断检查源目录中是否有任何新文件(JPEG) 在它们上启动 jpeg2txt,并写入结果...

回答 1 投票 0

XML 脚本运行以打印所需的输出

我有一个示例 XML 输入文件,其中包含以下内容: 我有一个示例 XML 输入文件,其中包含以下内容: <?xml version="1.0" encoding="utf-8"?> <infobases> <infobase author="Chartered Professional Accountants of Canada" levelDefOrder="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13,Normal Level" levels="Level 1,Level 2,Level 3,Level 4,Level 5,Level 6,Level 7,Level 8,Level 9,Level 10,Level 11,Level 12,Level 13" name="info_a" title="CPA Canada Standards and Guidance Collection"> <file level="Level 1" heading="XYZ1-L1">XYZ1-L1 <file level="Level 2" heading="XYZ1-L12">XYZ1-L12 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> <file level="Level 3" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1</file> <file level="Level 1" heading="XYZ2-L1">XYZ2-L1 <file level="Level 2" heading="XYZ2-L12">XYZ2-L12</file> <file level="Level 2" heading="XYZ2-L123">XYZ2-L123 <file level="Level 3" heading="XYZ1-L123">XYZ1-L123 <file level="Level 4" heading="XYZ1-L123">XYZ1-L123</file> </file> </file> </file> </infobase> </infobases> 我想编写一个脚本来识别文件元素级别属性,并在打印文件元素的标题时给出适当的缩进。文件元素标题将获得 .ditamap 扩展名,该扩展名将其他文件元素作为子元素并忽略其他子元素(不在示例 xml 文件中)。如果文件元素没有子文件元素,则会添加 .dita 扩展名。 我用 javascript 编写了一个脚本,它倾向于进行正确的缩进,但分配的扩展名不正确。我得到所有文件元素标题的 .dita。这是代码: const fs = require('fs'); const XmlStream = require('xml-stream'); // Create a readable stream from the XML file const stream = fs.createReadStream('input1.xml'); // Create a writable stream to the output text file const outputStream = fs.createWriteStream('output.txt'); // Create a new XML stream parser const xmlParser = new XmlStream(stream); // Function to print headings with proper indentation function printHeadingsToFile(file, indentation = '') { // Calculate the indentation based on the level of the file const levelIndentation = ' '.repeat(parseInt(file.$.level.substr(6)) - 1); // Determine file extension based on child elements const fileExtension = file.file ? 'ditamap' : 'dita'; // Write the heading with indentation to the output file outputStream.write(`${indentation}${levelIndentation}${file.$.heading}.${fileExtension}\n`); // Check if there are nested files if (file.file) { // If nested files exist, recursively print their headings with increased indentation file.file.forEach(nestedFile => { printHeadingsToFile(nestedFile, `${indentation}${levelIndentation}`); }); } } // Event listener for when a new XML element is encountered xmlParser.on('startElement: file', function(element) { // Print headings for each file printHeadingsToFile(element); }); // Event listener for when the parsing ends xmlParser.on('end', function() { console.log('Parsing finished.'); // Close the output stream after finishing writing outputStream.end(); }); // Event listener for any errors during parsing xmlParser.on('error', function(err) { console.error('Error during parsing:', err); // Close the output stream if there is an error outputStream.end(); }); 我在这里得到的输出如下: XYZ1-L1.dita XYZ1-L12.dita XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.dita XYZ2-L12.dita XYZ2-L123.dita XYZ1-L123.dita XYZ1-L123.dita 预期输出: XYZ1-L1.ditamap XYZ1-L12.ditamap XYZ1-L123.dita XYZ1-L123.dita XYZ1-L123.dita XYZ2-L1.dita XYZ2-L1.ditamap XYZ2-L12.dita XYZ2-L123.ditamap XYZ1-L123.ditamap XYZ1-L123.dita 找到了使用不同 xml 解析器包的解决方案。 const fs = require('fs'); const sax = require('sax'); // Create a SAX parser const parser = sax.parser(true); // Create a write stream to a text file const outputStream = fs.createWriteStream('output.txt'); // Array to store file attributes let fileAttributes = []; // Event handlers for the SAX parser parser.onopentag = function (node) { // Check if the current node is a <file> element if (node.name === 'file') { // Extract attributes and push them to the array fileAttributes.push(node.attributes); } }; parser.onend = function () { // Parsing ends, log the final output //console.log('File attributes:', fileAttributes); // Function to determine the file extension function getFileExtension(currentLevel, nextLevel) { if (currentLevel < nextLevel) { return '.ditamap'; } else { return '.dita'; } } // Iterate through the fileAttributes array for (let i = 0; i < fileAttributes.length; i++) { const currentFile = fileAttributes[i]; const nextFile = fileAttributes[i + 1]; // Get the level numbers const currentLevel = parseInt(currentFile.level.match(/\d+/)[0]); const nextLevel = nextFile ? parseInt(nextFile.level.match(/\d+/)[0]) : 0; // Get the file extension const extension = getFileExtension(currentLevel, nextLevel); // Indentation based on the level const indentation = ' '.repeat((currentLevel - 1) * 4); // Prepare the text to write const textToWrite = indentation + currentFile.heading + extension + '\n'; // Write the text to the output stream outputStream.write(textToWrite); } // Close the output stream when done outputStream.end(); }; // Read XML file const xmlData = fs.readFileSync('input.xml', 'utf8'); // Parse XML data parser.write(xmlData).close(); 结果输出: L1.ditamap L2.ditamap L3.dita L3.dita L3.dita L1.dita L1.ditamap L2.dita L2.ditamap L3.ditamap L4.ditamap Level5Text.dita

回答 1 投票 0

mv:在 shell 脚本中使用 mv 但不在终端中使用时缺少文件操作数

我正在尝试编写一个shell脚本,用户输入他们想要重命名为第一个变量的文件,然后输入他们想要的第二个变量的新名称。唯一的输出...

回答 3 投票 0

筛过滤器脚本正则表达式

我有以下筛子脚本: 需要 ["enotify","body","regex","imap4flags","variables"]; 如果标题:是“主题”“Sie haben ...

回答 1 投票 0

如何从 nmap 中提取开放端口号

现在我的目标是扫描单个IP并仅获取以逗号分隔的单行打印的开放端口。 下面的代码执行此操作,但它在末尾注入 % 。 目标是提取端口 n...

回答 2 投票 0

如何从nmap中提取开放端口号

现在我的目标是扫描单个IP并仅获取以逗号分隔的单行打印的开放端口。 下面的代码做到了这一点,但它在最后注入了 % 目标是实现端口号...

回答 1 投票 0

使用 API/宏将 UserField 插入 LibreOffice

我的任务是为 LibreOffice(准确地说是 Collabora online)编写 python 脚本,该脚本将用户字段插入到文档中。我在 LibreOffice 中记录了宏以及所需的操作 子主 雷姆 ------...

回答 1 投票 0

如何在 PowerShell 或命令行中将麦克风音量设置为 100%?

我正在尝试使用 PowerShell 或 Windows 11 上的命令行将麦克风音量调整到特定级别(例如 100)。我需要一种可靠的方法,可以通过编程方式执行,而无需...

回答 1 投票 0

任何人都可以帮助我获取 NSIS 脚本吗?我有一个案例,我需要检查案例可以是 3.3 3.42 23.5 23.56w 3.467 999.999 999.99a,但不是 1.b 或 8..a

此脚本检查: 版本字符串的长度至少为 3 个字符。 版本字符串不为空。 该版本包含句点。 之前的人物。是数字。 除

回答 1 投票 0

无需交互的 Bash 脚本

我想做以下事情: 我有一个 bash 脚本,它调用一个交互式命令,要求用户输入密码。我想将脚本中的密码指定为变量,然后将其传递

回答 4 投票 0

让 VS Code 检测没有文件扩展名的语言文件而不打开它们?

我正在尝试标准化我的一些 bin 脚本,并且我将不使用扩展。然而,事实证明很难看到我所有脚本的编程语言。其中一些是 bash,一些是...

回答 1 投票 0

很难一目了然地看到无扩展 bin 脚本的语言

我正在尝试标准化我的一些 bin 脚本,并且我将不使用扩展。然而,事实证明很难看到我所有脚本的编程语言。其中一些是 bash,一些是...

回答 1 投票 0

脚本退出后打印的重定向到子进程的输出

所以我有这个表现很奇怪的小测试脚本。这本来是我想做的一些测试的开始,但我什至无法让这个基本脚本正常运行。希望有人可以

回答 1 投票 0

bash 中的重定向问题

所以我有这个表现很奇怪的小测试脚本。这本来是我想做的一些测试的开始,但我什至无法让这个基本脚本正常运行。希望有人可以

回答 1 投票 0

仅在前一个容器运行并停止时启动容器

我有一种情况,我的 docker-compose 文件中有四个容器。 第二个依赖于第一个,第三个依赖于第二个,第四个依赖于第三个。但在这里我...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.