简体   繁体   English

设置环境变量的shell脚本

[英]Shell script to set environment variables

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 提示符下单独执行命令。

You need to run the script as source or the shorthand .您需要将脚本作为source或速记来运行.

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 中执行脚本。

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 are 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'

Note: The OP was not explicitly asking for steps on how to create export variables in an interactive shell using a shell script.注意:OP 没有明确询问如何使用 shell 脚本在交互式 shell 中创建导出变量的步骤。 He only asked his script to be assessed at most.他最多只要求评估他的剧本。 He didn't mention details on how his script would be used.他没有提到如何使用他的脚本的细节。 It could have been by using .它可能是通过使用. or source from the interactive shell.或来自交互式 shell 的source It could have been a standalone scipt, or it could have been source 'd from another script.它可能是一个独立的素文字,或者它可能已被source “d从另一个脚本。 Environment variables are not specific to interactive shells.环境变量并非特定于交互式 shell。 This answer solved his problem.这个答案解决了他的问题。

将脚本作为 source= 运行以在调试模式下运行。

source= ./myscript.sh

I cannot solve it with source ./myscript.sh .我无法用source ./myscript.sh解决它。 It says the source not found error.它说找不到源错误。
Failed also when using . ./myscript.sh使用时也失败. ./myscript.sh . ./myscript.sh . . ./myscript.sh It gives can't open myscript.sh.它导致无法打开 myscript.sh。

So my option is put it in a text file to be called in the next script.所以我的选择是把它放在一个文本文件中,以便在下一个脚本中调用。

#!/bin/sh
echo "Perform Operation in su mode"
echo "ARCH=arm" >> environment.txt
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 -?" >> environment.txt
echo "Export CROSS_COMPILE done"
# continue your compilation commands here
...

Tnen call it whenever is needed: Tnen 在需要时调用它:

while read -r line; do
    line=$(sed -e 's/[[:space:]]*$//' <<<${line})
    var=`echo $line | cut -d '=' -f1`; test=$(echo $var)
    if [ -z "$(test)" ];then eval export "$line";fi
done <environment.txt

In my case, I gave extra spaces before and after = .就我而言,我在=之前和之后给了额外的空格。 For example, in my shell file(say deploy.sh )例如,在我的 shell 文件中(比如deploy.sh

I initially write我最初写

GIT_SHA = $(git rev-parse HEAD)

But I fixed it by using:但我使用以下方法修复了它:

GIT_SHA=$(git rev-parse HEAD)

So please note that we should not give any spaces before and after the = .所以请注意,我们不应该在=前后留任何空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM