linux - C 程序在将其输出从 bash 脚本重定向到另一个文件时卡在终端上

标签 linux bash shell ubuntu

我正在尝试从 Linux 中的 bash 文件运行 C 程序,然后将其输出写入另一个文件(位于另一个目录中)。我使用的命令是:

gcc myfile.c -o test 
./test > /home/"$user"/Documents/"$name"/"$file" 

每当我尝试运行此命令时,程序都不会运行,而是卡在加载中。即使我写了一个文件名(来自程序所在的同一目录),程序也不会运行,直到我删除整个重定向命令并只写简单的 ./test 命令。我不知道为什么会这样。

这是 C 程序:

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;
  printf("Enter number of elements\n");
  scanf("%d", &n);
  printf("Enter %d integers\n", n);
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
  for (c = 0 ; c < n - 1; c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) 
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c < n; c++)
     printf("%d\n", array[c]);
  return 0;
}

即使我这样写:

./test | tee text.txt

它没有打印任何东西。

最佳答案

您可以使用命令 tee 来收集 printw 信息。喜欢 ./test |三通文件 或者您尝试在其中添加 2>&1。

关于linux - C 程序在将其输出从 bash 脚本重定向到另一个文件时卡在终端上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62497547/

相关文章:

linux - 使用 Gksudo 运行 sudo 等脚本

Linux:在命令行中打开最新文件

linux - 如何将文件名作为变量从 shell 脚本传递给 awk 命令

linux - 为什么 "yes yes | ssh hostname"不能自动对主机 key 身份验证回答是?

c++ - 尝试通过 C++ 程序连接到 postgresql

linux - 将源添加到 bash

bash - 以非交互方式将参数传递给交互式程序

bash - 将 'test' 的结果赋给一个变量

shell - 如何找出哪些 shell 命令在 POSIX 兼容的 Linux 系统中可用,哪些不可用?

php - 从 PHP 启动交互式 SSH bash session