2017-10-07 95 views
0

可有人请帮助我知道如何做到这一点:出口的bash变量

SCRIPT1:“xx.sh”低于

#!/bin/sh 
echo "Content-type: text/html" 
echo "" 
tr=$(mktemp -d) 
trr=$(echo $tr | awk '{split($0,array,"/")} END{print array[3]}') 
pud=$(echo "$trr" | awk '{split($0,array,".")} END{print array[2]}') 
mkdir $pud 
echo -e "123" > file 
mv file $pud/ #(briefly, I want to use this "file" in the script "yy.sh" for some other analysis. This is a webserver script snippet, based on my program, there could be many instances running at one time, hence "pud" has to be unique, and the "file" is unique for every instance too) 

echo "<html>" 
echo "<head>" 
echo "</head>" 
echo "<body>" 
echo "<form action=\"yy.sh\" method=\"GET\">" 
echo "<input type=\"submit\" value=\"Click me to get the results page.\">" 
echo "</form>" 
echo "</body>" 
echo "</html>" 

我想要做的就是这些行在第二个bash脚本“yy.sh”中获取变量“pud”的值(“yy.sh”与“xx.sh”位于同一父目录中)。有人可以帮助我知道如何做到这一点?

非常感谢!

+0

什么是你想要的输出? – batMan

+0

我已经编辑了脚本,请看一下,让我知道如何做到这一点。谢谢!如果事情不清楚,请告诉我。 – Dipak

回答

0

我明白了,你需要一个唯一标志将文件保存在一个独特的目录为每个实例,以便您可以yy.sh遍历所有这些目录。

你可以试试这个:

dir="${pud}_`date +%s%N`" `date +%s%N` : will give you time in seconds and NanoSeconds since the epoch. 
mkdir "${dir}" 
echo -e "123" > file 
mv file $dir/ 

# Pass this file path to yy.sh as following 
sh yy.sh "${dir}/file" 

在你yy.sh 使用$1指由xx.sh传递的文件路径。 $1表示第一个命令行参数,$2表示第二个等等..

例如,

#yy.sh 
echo "Hello World" >> "$1" #This will append "hello world" to file whose path was passed by xx.sh 
+0

谢谢!但为了清楚起见,每个实例都会调用“yy.sh”,因此,必须在每个实例中查找在一个实例中创建的唯一目录中的“文件”。这就是为什么,我需要以某种方式使“yy.sh”寻找特定的“$ {dir}”。你能告诉我如何将“$ {dir}”的值传递给“yy.sh”?或者如果您有任何其他方式来做到这一点,请让我知道。由于 – Dipak

+0

校正在以前的评论 - “所以,在一个实例所做的唯一的目录,必须寻找了‘’在那** **例如” – Dipak

+0

感谢:-)文件,但是,做“'SH yy.sh” $ {DIR}/file'”执行‘’中‘xx.sh yy.sh’我想用HTML形式做在我上面的脚本,即单击该按钮时,则‘yy.sh’和执行的“'$ {DIR}/file'”的值出现在“yy.sh”。有没有办法做到这一点,而不在“xx.sh”执行“yy.sh”请让我知道,如果我不清楚。非常感谢! – Dipak