Shell脚本 - C中的输入和输出文件

时间:2012-11-27 18:27:43

标签: shell input output

c_file=$( echo $2 | sed 's/\.c//g')
$c_file < $in_file > tempFile.out

参数$ 5是带我C程序的路径。我的C程序名称的“.c”。 例如,路径: /..../ A3Solution.c

那会让我:

A3Solution < input.in > output.out

我遇到了运行时错误:

  

A3Solution:未找到命令

我不知道为什么,但是当我在其他路径中运行其他C程序时它会起作用... 关于如何改变我的计划的任何想法?我真的没有看到问题。我已经尝试过:cat $5 and ls $5所以,我知道5美元的路径是正确的。

1 个答案:

答案 0 :(得分:0)

当前路径通常不在运行程序时搜索的路径中。 e.g。

$ ls -l bla
-rwxr-xr-x 1 me me 17036 13. Okt 2012  bla
$ bla
bash: bla: command not found
$

诀窍是告诉你的shell通过加./

作为前缀来查看当前目录
$ ./bla
hello world
$

因为你真的不知道给定的文件是在当前目录中还是在其他目录中(除非你解析'/'),你可以简单地在当前工作目录前加上(但是它不会起作用)如果指定绝对路径)或使用类似realpath实用程序的东西来规范化到绝对路径的任何相对路径。

你也可能想要使用bash的强大功能(如果你正在使用它,去掉尾随的.c)。 类似的东西:

exe_file=$(realpath ${2%.c})
if [ -e "${exe_file}" ]; then
   "${exe_file}" < "${in_file}" > tempFile.out
else
   echo "file '$2' not found or not executable" 1>&2
fi
相关问题