使用 Bash 自动创建测试输出

发布于 2024-09-13 05:12:59 字数 1264 浏览 1 评论 0原文

我有一个 java 程序,我试图为其生成 3 个输出,然后根据输入文件最初的名称重命名它们。

问题是仅移动输入文件。我认为这可能是有关相对文件命令的问题。

这是我的脚本。 (另外,我愿意接受有关使这个脚本看起来更好的建议。我是 bash 新手。)

#!/bin/bash
########################################################
#This script compiles Main, then attempts to run each  #
#test case. After running a test case, it renames the  #
#testcase and moves it to a new directory.             #
########################################################
#echo `pwd`    <---- This was used for testing. pwd is correct

#Gets contents of "tests" directory, stores them into the array
#without the file extension.
list=(`ls tests| sed 's/\.txt$//g'`)

#Compiles Main.java
cd ./src
javac Main.java
cd '../'
mv -f src/*.class bin #*/ (formatting fix)

#Runs Main for each test case, then renames and moves the test cases.
for filename in ${list[@]}
do
echo 1 > input.txt
echo tests/$filename.txt >> input.txt
cd ./bin # Why do I need to cd to make this work?
java bin/Main < input.txt
cd ../
mv -f input.txt "scriptout/'$filename'_input.txt"
mv -f "tests/output.txt" "scriptout/'$filename'_output.txt"
mv -f "tests/listing.txt" "scriptout/'$filename'_listing.txt"
mv -f "src/intermediate.txt" "scriptout/'$filename'_intermediate.txt"
done

I have a java program that I am trying to generate 3 outputs for, and then rename them depending on what the input file was originally called.

The problem is that only the input file is being moved. I think this might be an issue regarding relative file commands.

Here is my script. (Also, I'm open to suggestions on making this script better looking. I'm a bash newbie.)

#!/bin/bash
########################################################
#This script compiles Main, then attempts to run each  #
#test case. After running a test case, it renames the  #
#testcase and moves it to a new directory.             #
########################################################
#echo `pwd`    <---- This was used for testing. pwd is correct

#Gets contents of "tests" directory, stores them into the array
#without the file extension.
list=(`ls tests| sed 's/\.txt$//g'`)

#Compiles Main.java
cd ./src
javac Main.java
cd '../'
mv -f src/*.class bin #*/ (formatting fix)

#Runs Main for each test case, then renames and moves the test cases.
for filename in ${list[@]}
do
echo 1 > input.txt
echo tests/$filename.txt >> input.txt
cd ./bin # Why do I need to cd to make this work?
java bin/Main < input.txt
cd ../
mv -f input.txt "scriptout/'$filename'_input.txt"
mv -f "tests/output.txt" "scriptout/'$filename'_output.txt"
mv -f "tests/listing.txt" "scriptout/'$filename'_listing.txt"
mv -f "src/intermediate.txt" "scriptout/'$filename'_intermediate.txt"
done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-09-20 05:13:01

变量名称两边有单引号。由于它们位于双引号内,因此变量将被扩展,但单引号将包含在文件名中。试试这个:

mv -f input.txt "scriptout/${filename}_input.txt"

大括号将保护变量名称不与以下字符组合。

您可以这样做:

list=(tests/*)
list=("${list[@]##*/}")    # strip dirname
list=("${list[@]%.*}")     # strip extension

如果任何文件名中有空格,这将防止出现错误。

使用缩进使脚本更具可读性:

for ...
do
    command
done

通常,您应该避免使用相对目录并使用变量作为绝对目录的基础:

basedir=/path/to/base
do_something $basedir/test/filename
mv $basedir/subdir1/file1 $basedir/subdir2

这也可以使您不必使用 cd 一样多。

您收到任何错误消息吗?尝试使用 set -x 打开跟踪,并使用 set +x 将其关闭。将它们放在特定代码部分之前和之后,以便您可以看到该区域发生了什么。

You have single quotes around your variable names. Since they're inside double quotes, the variables will be expanded but the single quotes will be included in the filenames. Try this:

mv -f input.txt "scriptout/${filename}_input.txt"

The braces will protect the variable name from being combined with following characters.

You can do this:

list=(tests/*)
list=("${list[@]##*/}")    # strip dirname
list=("${list[@]%.*}")     # strip extension

These will prevent errors if there are spaces in any of the filenames.

Use indenting to make your script more readable:

for ...
do
    command
done

In general, you should avoid using relative directories and use a variable as a base for an absolute directory:

basedir=/path/to/base
do_something $basedir/test/filename
mv $basedir/subdir1/file1 $basedir/subdir2

This can also make it so you don't have to use cd as much.

Are you getting any error messages? Try using set -x to turn on tracing and set +x to turn it off. Place those before and after particular sections of code so you can see what's happening in that area.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文