Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell.exec bug #3

Open
xgqfrms opened this issue Oct 14, 2021 · 14 comments
Open

shell.exec bug #3

xgqfrms opened this issue Oct 14, 2021 · 14 comments

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Oct 14, 2021

shell.exec bug

shell.exec not work

shelljs/shelljs#1048

Node version

v12.18.0

ShellJS version

  "dependencies": {
    "shelljs": "^0.8.4"
  }

Operating system:

macOS 10.15.7

Description of the bug:

wanted

$ ./spe.js  PORT_ENV=666 && node ./test.js
#
# test command = export PORT_ENV=666
# PORT_ENV = 666
# PORT_ENV =  666

actually

$ ./spe.js  PORT_ENV=666 && node ./test.js
#
# test command = export PORT_ENV=666
# PORT_ENV = undefined
# PORT_ENV = undefined

Example ShellJS command to reproduce the error:

spe.js

#!/usr/bin/env node

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms 2021.10.14
 *
 * @description Pset-process-env
 *
 */

 const shell = require("shelljs");

const fs = require("fs");

const {
  version,
  argv,
  platform,
} = process;

const isMac = (platform === 'darwin');

const args = argv.slice(2) || [];

const isPureNumber = (str = '') => {
  const numberDict = [...''.padEnd(10, 'x')].map((item, i) => `${i}`);
  const arr = `${str}`.split('');
  return arr.every(item => numberDict.includes(item));
}

let key = "";
let env = "";
if (!args[0]) {
  args[0] = "";
  key = 'PORT_ENV';
  env = 8080;
} else {
  const tempArr = args[0].split('=');
  key = tempArr[0] || 'PORT_ENV';
  env = isPureNumber(tempArr[1]) ? parseInt(tempArr[1]) : tempArr[1];
}

let command = ``;

  if(isMac) {
    command = `export ${key}=${env}`;
  } else {
    command = `set ${key}=${env}`;
  }


shell.exec(command);

console.log('\n test command =', command);
console.log('\n PORT_ENV =', process.env.PORT_ENV);

test.js

// console.log('process.env =', process.env);
console.log('PORT_ENV =', process.env.PORT_ENV);
@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

shell.exec run export not work

#!/usr/bin/env node

const shell = require("shelljs");

const key = 'PORT_ENV';
const env = 8888;

// const command = `export ${key}=${env}`;
// const command = `set ${key}=${env}`;

const command = 'export PORT_ENV=8888';

// shelljs 执行 command

shell.exec('whoami');
shell.exec('which sh');
shell.exec('which bash');
// shell.exec('pwd');
// shell.exec('ls -al');
shell.exec(command);

console.log('\n command =', command);
console.log('\n PORT_ENV =', process.env.PORT_ENV);

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

$ spe PORT_ENV=666 && node ./test.js

#
$ spe PORT_ENV=666 && node -e "console.log('PORT_ENV =', process.env.PORT_ENV);"

$ node ./spe-bug.js

#
$ export PORT_ENV=8090 && node -e "console.log('PORT_ENV =', process.env.PORT_ENV);"

xgqfrms added a commit that referenced this issue Oct 14, 2021
@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

process bug ???

  1. parent process => child process ✅ 可以继承 enV
  2. child process => parent process ❌ 无法改变 env

If you are the child process (the process that got execed), you can not set the environment variable of your parent process.

https://stackoverflow.com/questions/49372501/set-environment-variable-with-shelljs
https://www.coder.work/article/1378443

在所有 UNIX 和 类Unix系统中, 每个进程都有其各自的环境变量设置。 缺省情况下, 当一个进程被创建时, 除了创建过程中的明确更改外,它继承了其父进程的绝大部分环境设置。

https://en.wikipedia.org/wiki/Environment_variable

// shell.exec(command);

if(isMac) {
  // command = `export ${key}=${env}`;
  // shell.exec(command);
  // shell.env[`"${key}"`] = env;
} else {
  // command = `set ${key}=${env}`;
  // shell.exec(command);
  // shell.env[`"${key}"`] = env;
}

// shell.env['"' + key + '"'] = env;
// shell.env[`"${key}"`] = env;
shell.env[key] = env;

console.log('\n🎉 finished!');

image

不同的 node.js child process env

/*
// 不同的 node.js child process env bug ???

spe PORT_ENV=8090 && node ./bug.js

spe PORT_ENV=8090 && node -e "console.log('PORT_ENV =', process.env.PORT_ENV);"

*/

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

shell编程之环境变量配置文件 ✅

https://www.imooc.com/learn/361

shell编程之变量 ✅

https://www.imooc.com/learn/336

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

兄弟连-沈超

云计算教学总监,国内著名的 Linux 技术专家,十余年 Linux 从业经验,培训学员过万名,录制的 Linux 教学视频过百万次下载,曾获微软认证、红帽认证讲师、北京市优秀园丁奖…历任即时科研集团项目经理、光宇华夏游戏运维部高级工程师,负责百万级同时在线大型网络游戏:希望Online、问道的维护与运维工作。

https://ke.qq.com/teacher/57362523

image

image

https://www.imooc.com/qadetail/245958?t=561731#id_561731

必须是,听声音也听出来了;

再看一下,讲课内容和风格,100% 了;

imooc的Tony老师 === 兄弟连的沈超老师

https://www.cnblogs.com/yongestcat/p/11736732.html

https://zhuanlan.zhihu.com/p/345035621

https://www.icourse163.org/course/itxdl-1002597008

image

兄弟连教育成立于2006年,专注于软件开发技术培训,是PHP/LAMP技术培训学校,主营业务包括PHP培训、Java培训、安卓培训、软件开发、人才输出、教材出版、企业培训等。总部设在北京,在上海、广州、深圳、沈阳、石家庄、西安、武汉等城市都设有教学中心。

李超在信中表示,因公司现金流问题,即日起兄弟连北京校区停止招生,同时员工全部遣散。上海、广州校区已相继经独立运营,不再归属集团总部,可更换品牌正常开展业务。沈阳、西安校区业务、院校合作业务,相关负责人带领员工根据自身情况更换品牌可选择自行创业,自负盈亏。

其实,早在2019年底,兄弟连的资金状况就已经发生问题,最后的孤注一掷在学生寒假的线下营收上,可是此次疫情猛烈,政府通知线下培训业务暂停,成为压倒兄弟连的最后一根稻草。

“死撑不下去了”,李超在信中写明了疫情发生后的机构处境,“这对资金储备少,包袱重、一直亏损的兄弟连无疑是雪上加霜。节前咱们勒紧腰带,缓发工资、全体动员,压缩成本,就是为了节后招生旺季打个翻身仗。哪知这次疫情来的如此凶猛、猝不及防,把我们的计划全部打乱。”

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

shelljs export bug

shelljs/shelljs#980

$ shell.echo(shell.env.PATH);

shelljs/shelljs#815 (comment)

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

    "scripts": {
        "dev": "cross-env NODE_OPTIONS='--max-old-space-size=8192' && npm run old_dev",
        "old_dev": "cross-env NODE_ENV=development webpack-dev-server --inline",
        "build": "rimraf dist && cross-env NODE_ENV=production webpack --progress --hide-modules",
        "eslint": "eslint --fix --ext js,vue src",
        "precommit": "lint-staged"
    },

NODE_OPTIONS

https://nodejs.org/api/cli.html

https://nodejs.org/api/cli.html#cli_node_options_options

http://nodejs.cn/api/cli/node_options_options.html

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

node js get system environment variable

https://stackoverflow.com/questions/4870328/read-environment-variables-in-node-js

process.env === process + env

https://nodejs.org/api/process.html#process_process_env

$ node -e 'process.env.foo = "bar"' && echo $foo

https://nodejs.dev/learn/how-to-read-environment-variables-from-nodejs

image

image

??? echo quickly blink bug ???

echo 一闪就消失了 ???

linux echo 一闪就消失了

http://www.bathome.net/thread-13418-1-1.html

批处理命令窗口闪一下就没了的原因:
1.如果没在行末加 pause,批处理默认以cmd /c的形式运行(运行完代码后不保存窗口——和其对应的是cmd /k命令),所以就会一闪就没了。
2.语法出错也会造成窗口一闪消失。

http://bbs.chinaunix.net/thread-1282392-1-1.html

不是看不见,是一闪而过

echo $foo;read a

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

Linux Shell 脚本如何执行完毕之后不自动关闭窗口

写给前端的 shell 脚本编程详解

https://segmentfault.com/a/1190000037797344

https://developer.huawei.com/consumer/cn/forum/topic/0204414228553730437

@xgqfrms
Copy link
Owner Author

xgqfrms commented Oct 14, 2021

cross-env usage bug

单个 process 直接执行 ✅

    "scripts": {
        "dev": "cross-env NODE_OPTIONS='--max-old-space-size=8192' cross-env NODE_ENV=development webpack-dev-server --inline",
        "build": "rimraf dist && cross-env NODE_ENV=production webpack --progress --hide-modules",
        "eslint": "eslint --fix --ext js,vue src",
        "precommit": "lint-staged"
    },

cross-env NODE_OPTIONS='--max-old-space-size=8192' ❌

&& 多个 process ❌ env 不会生效

    "scripts": {
        "dev": "cross-env NODE_OPTIONS='--max-old-space-size=8192' && cross-env NODE_ENV=development webpack-dev-server --inline",
        "build": "rimraf dist && cross-env NODE_ENV=production webpack --progress --hide-modules",
        "eslint": "eslint --fix --ext js,vue src",
        "precommit": "lint-staged"
    },
  // 单个 process 直接执行 ✅
  command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' yarn dev`;
  // && 多个 process ❌
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' && npm run dev`;
  // command = `./node_modules/cross-env/dist/bin/cross-env.js ${key}='${env}' && yarn dev`;

@xgqfrms
Copy link
Owner Author

xgqfrms commented Sep 6, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant