我有这段代码,它获取 Bash 文件并运行它,然后在控制台中打印 Bash 的过程:
package main.java.com.reachforce.java;
import java.io.IOException;
import java.io.InputStream;
public class RunScript {
public static void main(String[] args) throws InterruptedException {
try {
// Run the process
Runtime run = Runtime.getRuntime();
Process p = run.exec("C:/cygwin64/bin/bash -c \\'/bin/tests/SatoriTest1.sh 04.10 C:\\cygwin64\\bin\\tests");
p.waitFor();
// Get the input stream
InputStream is = p.getInputStream();
// Read script execution results
int i = 0;
StringBuffer sb = new StringBuffer();
while ( (i = is.read()) != -1)
sb.append((char)i);
System.out.println(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行后控制台向我显示:
Test SatoriTest1 writing output to /SatoriTest1-output
Now formatting imput file /inputfile
Executing curl -k -v -i -X POST -d --output /SatoriTest1-output --header "Content-Type: application/json" -H "Accept:application/json" /satori-service/validate
End SatoriTest1.sh
我试图找出这个“SatoriTest1-output”是在哪里创建的,但是......我找不到它。
这是 .sh 文件:
#!/bin/bash
#
# First need to compute directory for sourcing additonal files
DIR="$( dirname "${BASH_SOURCE}" )/.."
CURLDIR=`dirname "$(readlink -f "$BASH_SOURCE")"`
SCRIPTNAME="SatoriTest1"
# This script relies on the getconfig settings so calling now .
"${CURLDIR}/getmicroserviceconfig.sh"
# Calling the getmicroserviceconfig.sh function to get the configuration
getmicroserviceconfig
# Also the formatSatori.sh FORMATFILE="${OUTPATH}/formatSatori.sh"
if [ -f "${FORMATFILE}" ] then
. "${FORMATFILE}"
fi
#
# Usage ./SatoriTest1.sh
#
# Testing /satori-service: POSTs a new address for validation
#
# Note: using -k to avoid certificate foof
#
# Depends on: 001-login test (to get a successful login w/security token)
# SatoriTest1: /satori-service/validate: Satori address validation test
#Expect this to submit an address to Satori address validation service
#
THISTEST="SatoriTest1.sh"
APIPATH="satori-service/validate" FULLURL="${WEBSERVICEURL}/${APIPATH}" OUTPUTFILENAME="${OUTPATH}/SatoriTest1-output"
echo "Test $SCRIPTNAME writing output to ${OUTPUTFILENAME}"
NEWSATORIFILE="${OUTPATH}/inputfile"
echo "Now formatting imput file ${NEWSATORIFILE}"
formatSatori "${NEWSATORIFILE}"
NEWSATORIREQ=$newsatorireq
echo ${NEWSATORIREQ} > "${OUTPATH}/${OUTPUTFILENAME}.json"
#
# Now make the API calls using curl
#
# Calling POST with inputfile
echo "Executing curl -k -v -i -X POST -d ${NEWSATORIREQ} --output ${OUTPUTFILENAME} --header \"Content-Type: application/json\" -H \"Accept:application/json\" ${FULLURL}" curl -k
-v -i -X POST -d "${NEWSATORIREQ}" --output "${OUTPUTFILENAME}" --header "Content-Type: application/json" -H "Accept:application/json" "${FULLURL}"
echo "End ${THISTEST}"
有什么帮助吗?
最佳答案
您的输入和输出文件路径是根据脚本中未定义的 OUTPATH 构建的。因此,OUTPUTFILENAME="${OUTPATH}/SatoriTest1-output"已变为“/SatoriTest1-output”。
检查根目录下的输出文件,即/或初始化 OUTPATH 并尝试一下。
关于java - 脚本和Java : How can i say to Java to put the output file into a new folder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39755637/