如何将 if then else 输出保存在变量 unix 中

How to save the if then else output in a variable unix

我有一个来自 Datastage 的变量 (stageVar),需要检查该变量是否等于零然后替换 100 else stageVar。 之后我需要找到 mod 并保存在变量中。

我试过下面的代码但没有成功。

var= if [stageVar -eq 0] ; then "100" ; else stageVar ; fi; var2='expr $var % 100'; echo $var2;

您可以在 bash 中使用它:

[[ $stageVar -eq 0 ]] && var=100 || var=$stageVar
((var2 = var % 100))
echo $var2

如果想保留if/then/else构造,可以使用:

var=$(if [ "$stageVar" = 0 ]; then echo "100"; else echo $stageVar; fi)