Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

bash - 如何将BASH变量发送到多个脚本? [重复](How to send BASH variables to multiple scripts? [duplicate])

I have many BASH scripts called in sequence, eg, script1.sh contains:

(我有许多顺序调用的BASH脚本,例如script1.sh包含:)

#!/bin/bash

bash script2.sh
bash script3.sh
bash script4.sh

script2.sh contains:

(script2.sh包含:)

#!/bin/bash

file_a="1.txt"

cp $file_a /tmp/$file_a.tmp

script3.sh contains:

(script3.sh包含:)

#!/bin/bash

wc -l /tmp/$file_a.tmp

script4.sh contains:

(script4.sh包含:)

#!/bin/bash

cat /tmp/2.txt $file_a.tmp > file3.txt

Each file requires access to a small collection of variables.

(每个文件都需要访问少量变量。)

How can I pass the variables from one script onto the next?

(如何将变量从一个脚本传递到下一个脚本?)

  ask by Village translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have many options.

(您有很多选择。)

The first method would be making the variable as the environment variable and pass to the script before the second script get executed.

(第一种方法是将变量设为环境变量,并在执行第二个脚本之前将其传递给脚本。)

The second method would be making the second script to run in the same shell.

(第二种方法是使第二个脚本在同一shell中运行。)

The methods are described well here with the examples.

(此处通过示例详细介绍了这些方法。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...