在脚本中不可用的环境中传递的变量

时间:2022-07-28 19:26:47

I'm trying to write an if/elif/else statement where if a variable equals "Full" it will cat a file and if the same variable equals "Quick" it will read a different, smaller file. The variable is being set outside of the script and is being exported by export var1=Quick

我在写一个if/elif/else语句,如果一个变量等于“Full”,它将会显示一个文件,如果同一个变量等于“Quick”,它将读取另一个更小的文件。变量被设置在脚本之外,并由export var1=Quick导出

I have

我有

#!/bin/sh
echo $var1
export $var1=var1
{
#try cat-ing the file. If it cannot be read, exit with error. 
if [ "$var1" = "Full" ];
then 
echo "Full read chosen"
cat foo
exit 0
elif [ "$var1" = "Quick" ];
then
echo "Quick read chosen"
cat bar
exit 0
else
echo "Please choose quick or full"
exit 1
fi
}

When I try to run the script by calling ./test, it doesn't seem like the variable is being set

当我试图通过调用来运行脚本时,它看起来并不像正在设置的变量。

+ '[' '' = Full ']'
+ '[' '' = Quick ']'
+ echo 'Please choose quick or full'
Please choose quick or full
+ exit 1

Is there a reason why, even though the variable is exported outside of the script, the variable isn't being passed to the script?

为什么即使变量被导出到脚本之外,变量也没有被传递给脚本?

2 个解决方案

#1


2  

The following absolutely does work:

以下几点绝对有效:

$ export var1=Quick
$ ./yourscript

...where yourscript is the script in question.

…你的脚本是问题的脚本。

As such, your question needs to be adjusted to contain enough information and details to reproduce the problem.

因此,您的问题需要进行调整,以包含足够的信息和细节以重现问题。


As an aside, your script would be better written thusly:

顺便说一句,你最好这样写:

#!/bin/sh
case $var1 in
  Full)
    echo "Full read chosen" >&2
    cat foo
    ;;
  Quick)
    echo "Quick read chosen" >&2
    cat bar
    ;;
  *)
    echo "Please choose quick or full" >&2
    exit 1
    ;;
esac
exit 0

Both of the following work for me:

以下两项工作我都做过:

$ var1=Quick ./yourscript
Quick read chosen
cat: bar: No such file or directory
$ (export var1=Quick; ./yourscript)
Quick read chosen
cat: bar: No such file or directory

#2


1  

The problem is the name of your variable. Change $var1 to $1 will do the trick.

问题是变量的名称。将$var1更改为$1就可以了。

#1


2  

The following absolutely does work:

以下几点绝对有效:

$ export var1=Quick
$ ./yourscript

...where yourscript is the script in question.

…你的脚本是问题的脚本。

As such, your question needs to be adjusted to contain enough information and details to reproduce the problem.

因此,您的问题需要进行调整,以包含足够的信息和细节以重现问题。


As an aside, your script would be better written thusly:

顺便说一句,你最好这样写:

#!/bin/sh
case $var1 in
  Full)
    echo "Full read chosen" >&2
    cat foo
    ;;
  Quick)
    echo "Quick read chosen" >&2
    cat bar
    ;;
  *)
    echo "Please choose quick or full" >&2
    exit 1
    ;;
esac
exit 0

Both of the following work for me:

以下两项工作我都做过:

$ var1=Quick ./yourscript
Quick read chosen
cat: bar: No such file or directory
$ (export var1=Quick; ./yourscript)
Quick read chosen
cat: bar: No such file or directory

#2


1  

The problem is the name of your variable. Change $var1 to $1 will do the trick.

问题是变量的名称。将$var1更改为$1就可以了。