Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将BASH变量发送到多个脚本?_Bash_Variables - Fatal编程技术网

如何将BASH变量发送到多个脚本?

如何将BASH变量发送到多个脚本?,bash,variables,Bash,Variables,我有许多按顺序调用的BASH脚本,例如script1.sh包含: #!/bin/bash bash script2.sh bash script3.sh bash script4.sh #!/bin/bash file_a="1.txt" cp $file_a /tmp/$file_a.tmp #!/bin/bash wc -l /tmp/$file_a.tmp #!/bin/bash cat /tmp/2.txt $file_a.tmp > file3.txt scri

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

#!/bin/bash

bash script2.sh
bash script3.sh
bash script4.sh
#!/bin/bash

file_a="1.txt"

cp $file_a /tmp/$file_a.tmp
#!/bin/bash

wc -l /tmp/$file_a.tmp
#!/bin/bash

cat /tmp/2.txt $file_a.tmp > file3.txt
script2.sh包含:

#!/bin/bash

bash script2.sh
bash script3.sh
bash script4.sh
#!/bin/bash

file_a="1.txt"

cp $file_a /tmp/$file_a.tmp
#!/bin/bash

wc -l /tmp/$file_a.tmp
#!/bin/bash

cat /tmp/2.txt $file_a.tmp > file3.txt
script3.sh包含:

#!/bin/bash

bash script2.sh
bash script3.sh
bash script4.sh
#!/bin/bash

file_a="1.txt"

cp $file_a /tmp/$file_a.tmp
#!/bin/bash

wc -l /tmp/$file_a.tmp
#!/bin/bash

cat /tmp/2.txt $file_a.tmp > file3.txt
script4.sh包含:

#!/bin/bash

bash script2.sh
bash script3.sh
bash script4.sh
#!/bin/bash

file_a="1.txt"

cp $file_a /tmp/$file_a.tmp
#!/bin/bash

wc -l /tmp/$file_a.tmp
#!/bin/bash

cat /tmp/2.txt $file_a.tmp > file3.txt
每个文件都需要访问一小部分变量。如何将变量从一个脚本传递到下一个脚本?

您有很多选择

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

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

通过实例对这些方法进行了很好的描述