英文:
Bash script command output error when splitting up and convert to int
问题
我创建了一个bash脚本来读取另一个命令的输出(java -jar ...
)。
命令的结果如下所示:
MIN,MAX
6165350,6228865
我使用 head -2 | tail -1
去掉了第一行,所以输出变成了:
6165350,6228865
然后,我将它保存到名为 output
的变量中:
output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)
最后,我创建了一个Java文件来拆分输出结果:
import java.util.*;
public class Demo {
public static void main(String[] args) {
String[] values = args[0].split(",");
System.out.println(Integer.parseInt(values[0]));
System.out.println(Integer.parseInt(values[1]));
}
}
以下是最终的bash脚本:
#!/bin/bash
output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)
java Demo $output
当我运行bash脚本时,为什么会出现错误:
6165350
Exception in thread "main" java.lang.NumberFormatException: For input string: "6228865
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Demo.main(Demo.java:8)
当我尝试更改 output
时,我得到了正确的结果:
output="6165350,6228865"
谢谢。
英文:
I create a bash script to read the output from another command (java -jar ...
).
The result from the command is like below:
MIN,MAX
6165350,6228865
I omit the first line using head -2 | tail -1
so the output will become
6165350,6228865
And i save it to the variable called output
output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)
Last, I create java file to split the output results
import java.util.*;
public class Demo {
public static void main(String[] args) {
String[] values = args[0].split(",");
System.out.println(Integer.parseInt(values[0]));
System.out.println(Integer.parseInt(values[1]));
}
}
And below is the final bash script
#!bin/bash
output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)
java Demo $output
When i run the bash, why i'm getting the error
6165350
"xception in thread "main" java.lang.NumberFormatException: For input string: "6228865
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at Demo.main(Demo.java:8)
I get the correct results when i try to change the output
output="6165350,6228865"
Thank you
答案1
得分: 2
I suspect that there is a \r
at the end of the last argument, which explains why Exception
is spelled "xception
.
You can confirm that it is the case by pipe the output to a hex dump.
我怀疑最后一个参数末尾有一个`\r`,这就解释了为什么`Exception`拼写为`"xception`。
您可以通过将输出导向十六进制转储来确认这一点。
You can trim the \r
of with tr -d '\r'
in the bash part or strip the argument in the Java part.
您可以在bash部分使用`tr -d '\r'`来去掉`\r`,或者在Java部分剥离参数。
英文:
I suspect that there is a \r
at the end of the last argument, which explains why Exception
is spelled "xception
.
You can confirm that it is the case by pipe the output to a hex dump.
java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1 | xxd
You can trim the \r
of with tr -d '\r'
in the bash part or strip the argument in the Java part.
String[] values = args[0].strip().split(",");
答案2
得分: 1
如果存在_不可打印字符,通常String#strip_方法会将其移除。
_String#trim_也可能有效。
尝试以下代码。
public static void main(String[] args) {
String[] values = args[0].split(",");
System.out.println(Integer.parseInt(values[0].strip()));
System.out.println(Integer.parseInt(values[1].strip()));
}
英文:
If there is a non-printing character there, typically the String#strip method will remove it.
String#trim may work, also.
Try the following.
public static void main(String[] args) {
String[] values = args[0].split(",");
System.out.println(Integer.parseInt(values[0].strip()));
System.out.println(Integer.parseInt(values[1].strip()));
}
答案3
得分: 1
自从您提到您的 bundletool.jar
产生回车作为行尾时,我会建议(如果您无法更改Java程序以生成合理的行尾),只需执行以下操作:
nums=( $(java .... bundletool.jar ... | dos2unix | awk -F , '{print $1, $2}') )
然后您可以在 ${nums[0]}
中找到第一个数字,在 ${nums[1]}
中找到第二个数字。
英文:
Since you mentioned that your bundletool.jar
produces a carriage return as line ending, I would (if you can not change the Java program to produce reasonable line endings) simply do a
nums=( $(java .... bundletool.jar ... |dos2unix|awk -F , '{print $1, $2}') )
You then have the first number in ${nums[0]}
and the second one in ${nums[1]}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论