首页 > 解决方案 > 用于检查简单打字稿文件中的值的 Shell 脚本

问题描述

我有一个简单的 TS 文件

环境.ts

其中包含

export const BUILD_SDK = false 
export const DUMMY_PRESET = false

我有 build.sh 脚本来构建我们的 SDk。

我想编写一个shell 脚本(build.sh),如果 env.ts 中的值在构建之前为 false,则会引发错误。

有人可以帮我找出一种方法吗?

根据 Flashtube 的回答进行更新

所以我尝试了这个

#!/bin/bash
# With head -n 2 env.ts we get the first line of env.ts
# With cut -d " " we split the line at every space
# With -f 5 we specify we want the 5th spit value, in this case the boolean value
# If that is equals to true, we echo an error and pipe it to the error output with  1>&2 and exit with exit status 64
if [[ $(head -n 2 App/env/index.tsx | cut -d " " -f 5) = "false" ]]; then 
    echo "Error! check env variables and set it to true" 1>&2;
    exit 64;
fi

我的环境文件看起来像这样

//Please inform if you do any changes in this file so that we can update build script
export const SDK_BUILD = false

但这没有用

标签: linuxtypescriptmacosshell

解决方案


您可以使用以下 bash 脚本

#!/bin/bash
# With head -n 1 env.ts we get the first line of env.ts
# With cut -d " " we split the line at every space
# With -f 5 we specify we want the 5th spit value, in this case the boolean value
# If that is equals to true, we echo an error and pipe it to the error output with  1>&2 and exit with exit status 64
if [[ $(head -n 1 env.ts | cut -d " " -f 5) = "false" ]]; then 
    echo "Error!" 1>&2;
    exit 64;
fi

现在我们可以检测该值是假还是真。


推荐阅读