2

我正在尝试打开一个终端,该终端将执行代码并将输出文件复制到目标文件夹,但由于某种原因,只有可执行文件正在运行并且复制命令不起作用,但是当我单独执行每个命令时它可以工作......

谁能帮我检测我的语法错误?

命令行是:

gnome-terminal --working-directory=/home/syntax_error/Desktop/uni_work/ --tab -e "./a.out './exec_me 500' ; cp output.txt /home/syntax_error/FILES/first_output.txt"

其中 ./exec_me 是 a.out 的参数,500 是 exec_me 的参数

谢谢 =)

4

3 回答 3

4

看起来gnome-terminal不使用 shell 来执行这些命令。如果要使用;,则需要通过 shell 显式调用它。

尝试:

gnome-terminal -e "bash -c 'command1 ; command2'"

或者:

echo "command1 ; command2" > tmp.sh
gnome-terminal -e "bash tmp.sh"
于 2012-01-04T01:00:52.850 回答
4

我自己进行了类似的测试:

$ strace -o /tmp/gnome.out -f gnome-terminal --working-directory=/var/log --tab -e "cat *.log ; echo hello"
$ grep --color=no execve /tmp/gnome.out 
28561 execve("/usr/bin/gnome-terminal", ["gnome-terminal", "--working-directory=/var/log", "--tab", "-e", "cat *.log ; echo hello"], [/* 39 vars */]) = 0
28564 execve("/usr/lib/libvte9/gnome-pty-helper", ["gnome-pty-helper"], [/* 40 vars */]) = 0
28565 execve("/home/sarnold/bin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/usr/local/sbin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/usr/local/bin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/usr/sbin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/usr/bin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/sbin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */]) = -1 ENOENT (No such file or directory)
28565 execve("/bin/cat", ["cat", "*.log", ";", "echo", "hello"], [/* 40 vars */] <unfinished ...>
28565 <... execve resumed> )            = 0

这表明整个命令行正在传递给在字符串中找到的第一个可执行文件。(这是一种……独特的……执行内容的方式。)

我建议编写一个小的 shell 脚本,它完全符合您的要求,并gnome-terminal -e从命令行选项运行该 shell 脚本。像这样的东西:

~/bin/cp_first_output.sh

#!/bin/sh
cd /home/syntax_error/Desktop/uni_work/
./a.out './exec_me 500'
cp output.txt /home/syntax_error/FILES/first_output.txt

chmod 755该文件,然后运行:

gnome-terminal --tab -e /home/syntax_error/bin/cp_first_output.sh
于 2012-01-04T01:01:33.600 回答
0

尝试使用“\”(不带引号)分隔两个命令

于 2012-01-04T00:37:03.000 回答