【问题标题】:Script and Java: How can i say to Java to put the output file into a new folder?脚本和 Java:我如何对 Java 说将输出文件放入新文件夹?
【发布时间】:2016-09-28 19:00:26
【问题描述】:

我有这段代码,它获取一个 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();
        }

    }
}

运行后的控制台显示:

测试 SatoriTest1 将输出写入 /SatoriTest1-output

现在格式化输入文件/inputfile

执行 curl -k -v -i -X POST -d --output /SatoriTest1-output --header "Content-Type: application/json" -H "Accept:application/json" /satori-service/validate

结束 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}"

有什么帮助吗?

【问题讨论】:

    标签: java json shell unix cygwin


    【解决方案1】:

    您的输入和输出文件路径由脚本中未定义的 OUTPATH 构建。因此,OUTPUTFILENAME="${OUTPATH}/SatoriTest1-output" 变成了 "/SatoriTest1-output"。

    检查根目录下的输出文件,即 / 或初始化 OUTPATH 并试一试。

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多