通过HTML表单方法导出bash变量

时间:2017-10-07 19:56:14

标签: html bash shell cgi

有人可以帮我知道如何做到这一点:

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”在同一个父目录中)。有人可以帮我知道怎么做吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我知道,你需要一个uniquifier来将文件保存在每个实例的唯一目录中,这样你的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表示第二个,依此类推。

EG。

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