将值从Shell脚本传递到Javascript文件

时间:2019-08-21 00:57:19

标签: javascript bash

我正在尝试在运行时将输入提供给Shell脚本。我已经做到了,但是该值需要由JS文件使用。请帮我怎么做

TIME = $ {1:-80}

这就是我在运行bash脚本时要求用户提供输入的方式

Java脚本文件需要从bash文件中获取值

2 个答案:

答案 0 :(得分:0)

只需从shell脚本中调用CLI,然后将$TIME作为参数传递即可。

答案 1 :(得分:0)

您可以使用process.argv[2]在js文件中使用传递的参数。这是工作示例

#!/bin/bash
time="${1}"
echo "Argument is $time"
#pass the the argument to JS file
node test.js $time
#or you can pass without assigning to variable 
node test.js $1

您应该从bash脚本中调用nodejs脚本以传递参数。

test.js

const args = process.argv;
console.log(args);
// or you can try
var program_name = process.argv[0]; //value will be "node"
var script_path = process.argv[1]; //value will be "test.js"
var time_value = process.argv[2]; //value will be "time that is passed from bash file"
console.log("time in JS file",time_value);

您可以阅读有关cliargs here的更多信息,如果您对客户端感兴趣,请阅读here