脚本未正确执行
我有一个创建一个包含从我的相机的当前设置一个文本文件中的shell脚本:脚本未正确执行
#!/bin/sh
file="test.txt"
[[ -f "$file" ]] && rm -f "$file"
var=$(gphoto2 --summary)
echo "$var" >> "test.txt"
if [ $? -eq 0 ]
then
echo "Successfully created file"
exit 0
else
echo "Could not create file" >&2
exit 1
fi
该脚本,因为我认为它应该当我运行它从终端,但是当我运行下面的处理程序是创建的文本文件,但不包含任何来自相机的信息:
import java.util.*;
import java.io.*;
void setup() {
size(480, 120);
camSummary();
}
void draw() {
}
void camSummary() {
String commandToRun = "./ex2.sh";
File workingDir = new File("/Users/loren/Documents/RC/CamSoft/");
String returnedValues; // value to return any results
try {
println("in try");
Process p = Runtime.getRuntime().exec(commandToRun, null, workingDir);
int i = p.waitFor();
if (i==0) {
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((returnedValues = stdInput.readLine()) != null) {
println(returnedValues);
}
} else{
println("i is: " + i);
}
}
catch(Throwable t) {
println(t);
}
}
最后,我想直接从剧本到读取一些数据变量,然后在处理中使用这些变量。
有人可以帮我解决这个问题吗?
谢谢
罗兰
备用脚本:
#!/bin/sh
set -x
exec 2>&1
file="test.txt"
[ -f "$file" ] && rm -f "$file"
# you want to store the output of gphoto2 in a variable
# var=$(gphoto2 --summary)
# problem 1: what if PATH environment variable is wrong (i.e. gphoto2 not accessible)?
# problem 2: what if gphoto2 outputs to stderr?
# it's better first to:
echo first if
if ! type gphoto2 > /dev/null 2>&1; then
echo "gphoto2 not found!" >&2
exit 1
fi
echo second if
# Why using var?...
gphoto2 --summary > "$file" 2>&1
# if you insert any echo here, you will alter $?
if [ $? -eq 0 ]; then
echo "Successfully created file"
exit 0
else
echo "Could not create file" >&2
exit 1
fi
有你的shell脚本的几个问题。让我们一起纠正并改进。
#!/bin/sh
file="test.txt"
[ -f "$file" ] && rm -f "$file"
# you want to store the output of gphoto2 in a variable
# var=$(gphoto2 --summary)
# problem 1: what if PATH environment variable is wrong (i.e. gphoto2 not accessible)?
# problem 2: what if gphoto2 outputs to stderr?
# it's better first to:
if ! type gphoto2 > /dev/null 2>&1; then
echo "gphoto2 not found!" >&2
exit 1
fi
# Why using var?...
gphoto2 --summary > "$file" 2>&1
# if you insert any echo here, you will alter $?
if [ $? -eq 0 ]; then
echo "Successfully created file"
exit 0
else
echo "Could not create file" >&2
exit 1
fi
感谢您的建议。我需要做一点挖掘才能完全理解,但你推荐的东西似乎有意义。当它从终端运行时,它的工作方式应该如此。当我从处理中运行它退出不返回它只是返回“完成”....奇怪 –
我站在纠正。 “完成”来自于我今天上午添加的其他内容以进一步排除故障。这也不起作用 –
如果更正的shell脚本不起作用,则必须在java域中调试问题。 –
也许'/ bin/sh'与'/ bin/bash'不一样,'sh'不知道怎么做'$()'。试试'#!/ bin/bash'。 – meuh
我试过sh和bash ...没有变化 –
我不知道这个问题是否重要,但gphoto2是一个命令行应用程序。争论 - 总结让我看到了一大堆的价值观。 –