用于设置环境变量的Shell脚本

时间:2022-06-01 16:48:53

I wish to write a shell script to export variables.

我希望编写一个shell脚本来导出变量。

Below I have listed the script .

下面我列出了脚本。

echo "Perform Operation in su mode"
export ARCH=arm
echo "Export ARCH=arm Executed"
export PATH='/home/linux/Practise/linux-devkit/bin/:$PATH';
echo "Export path done"
export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-';
echo "Export CROSS_COMPILE done"

But this doesn't seem to work properly. I have to individually execute the commands at the shell prompt instead.

但这似乎不能正常工作。我必须在shell提示符下单独执行命令。

2 个解决方案

#1


27  

Please show us more parts of the script and tell us what commands you had to individually execute and want to simply.

请向我们展示脚本的更多部分,并告诉我们您必须单独执行和想要的命令。

Meanwhile you have to use double quotes not single quote to expand variables:

同时你必须使用双引号而不是单引号来扩展变量:

export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"

Semicolons at the end of a single command is also unnecessary.

单个命令末尾的分号也是不必要的。

So far:

至今:

#!/bin/sh
echo "Perform Operation in su mode"
export ARCH=arm
echo "Export ARCH=arm Executed"
export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"
echo "Export path done"
export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-' ## What's next to -?
echo "Export CROSS_COMPILE done"
# continue your compilation commands here
...

For su you can run it with:

对于su,您可以运行它:

su -c 'sh /path/to/script.sh'

(For those who downvote this answer, you should know that the OP was not explicitly requiring a shell script that would export variables on an -interactive- shell - and not on a one-time run shell script where using source or . to call it would be meaningless unless you're actually calling the script from another script. Of course you could use source or . if it is truly necessary, but there was also another problem that needed to be fixed, and it seems like the actual problem on this context was the usage of single quotes.

(对于那些拒绝这个答案的人,你应该知道OP并没有明确要求一个shell脚本可以在-interactive- shell上导出变量 - 而不是在使用source或。来调用它的一次性运行shell脚本上除非你真的从另一个脚本调用脚本,否则将毫无意义。当然你可以使用source或。如果它确实是必要的,但是还有另一个需要修复的问题,它似乎就是这个问题的实际问题上下文是单引号的用法。

Don't downvote it just because it doesn't help you. The answer was for the question made, and for the OP.

不要仅仅因为它对你没有帮助而进行投票。答案是针对提出的问题和OP。

It would have applied if the OP -actually mentioned- that it was indeed for setting-up exportable environment variables on an interactive shell through a shell-script.)

如果OP实际提到它确实用于通过shell脚本在交互式shell上设置可导出的环境变量,那么它将会应用。)

#2


138  

You need to run the script as source or the shorthand .

您需要将脚本作为源或速记运行。

source ./myscript.sh

or

要么

. ./myscript.sh

This will run within the existing shell, ensuring any variables created or modified by the script will be available after the script completes.

这将在现有shell中运行,确保脚本完成后脚本创建或修改的任何变量都可用。

Running the script just using the filename will execute the script in a separate subshell.

仅使用文件名运行脚本将在单独的子shell中执行脚本。

#1


27  

Please show us more parts of the script and tell us what commands you had to individually execute and want to simply.

请向我们展示脚本的更多部分,并告诉我们您必须单独执行和想要的命令。

Meanwhile you have to use double quotes not single quote to expand variables:

同时你必须使用双引号而不是单引号来扩展变量:

export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"

Semicolons at the end of a single command is also unnecessary.

单个命令末尾的分号也是不必要的。

So far:

至今:

#!/bin/sh
echo "Perform Operation in su mode"
export ARCH=arm
echo "Export ARCH=arm Executed"
export PATH="/home/linux/Practise/linux-devkit/bin/:$PATH"
echo "Export path done"
export CROSS_COMPILE='/home/linux/Practise/linux-devkit/bin/arm-arago-linux-gnueabi-' ## What's next to -?
echo "Export CROSS_COMPILE done"
# continue your compilation commands here
...

For su you can run it with:

对于su,您可以运行它:

su -c 'sh /path/to/script.sh'

(For those who downvote this answer, you should know that the OP was not explicitly requiring a shell script that would export variables on an -interactive- shell - and not on a one-time run shell script where using source or . to call it would be meaningless unless you're actually calling the script from another script. Of course you could use source or . if it is truly necessary, but there was also another problem that needed to be fixed, and it seems like the actual problem on this context was the usage of single quotes.

(对于那些拒绝这个答案的人,你应该知道OP并没有明确要求一个shell脚本可以在-interactive- shell上导出变量 - 而不是在使用source或。来调用它的一次性运行shell脚本上除非你真的从另一个脚本调用脚本,否则将毫无意义。当然你可以使用source或。如果它确实是必要的,但是还有另一个需要修复的问题,它似乎就是这个问题的实际问题上下文是单引号的用法。

Don't downvote it just because it doesn't help you. The answer was for the question made, and for the OP.

不要仅仅因为它对你没有帮助而进行投票。答案是针对提出的问题和OP。

It would have applied if the OP -actually mentioned- that it was indeed for setting-up exportable environment variables on an interactive shell through a shell-script.)

如果OP实际提到它确实用于通过shell脚本在交互式shell上设置可导出的环境变量,那么它将会应用。)

#2


138  

You need to run the script as source or the shorthand .

您需要将脚本作为源或速记运行。

source ./myscript.sh

or

要么

. ./myscript.sh

This will run within the existing shell, ensuring any variables created or modified by the script will be available after the script completes.

这将在现有shell中运行,确保脚本完成后脚本创建或修改的任何变量都可用。

Running the script just using the filename will execute the script in a separate subshell.

仅使用文件名运行脚本将在单独的子shell中执行脚本。