如何向不同的终端执行命令?

如何向不同的终端执行命令?

用到的c语言代码:run.c

#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char **argv)
{
  char tty[16] = {0};
  char cmd[64] = {0};
  char tmp[2] = {0};
  int len, i;
  int fd;

  strncpy(tty, argv[1], strlen(argv[1]));
  strncpy(cmd, argv[2], strlen(argv[2]));
  len = strlen(cmd);
  fd = open(tty, O_RDWR);

  for (i = 0; i < len; i++) {
    sprintf(tmp, "%c ", cmd[i]);
    ioctl(fd, TIOCSTI, tmp);
  }
  ioctl(fd, TIOCSTI, "\n ");
}

编译c语言代码:

gcc run.c -o a.out

在pts0执行想pts1终端执行命令

[root@pts0 ~]# tty
/dev/pts/0  #这是当前的终端
[root@pts0 ~]# ./a.out /dev/pts/1  "hostname -I"  #向pts1终端执行命令
[root@pts0 ~]#

在pts1终端查看执行结果:

[root@pts1 ~]# hostname -I
176.16.0.51
[root@pts1 ~]# tty
/dev/pts/1
[root@pts1 ~]#
#!/bin/bash
#tty_o=\$(tty)
tty_o="/dev/pts/1"
pushd /tmp/bash_test
    ./a.out \$tty_o "sleep 25s &"
    sleep 8
    [ "\$1" == "jobs" ]  && \
        ./a.out \$tty_o "jobs  > /tmp/bash_test/result"
    [ "\$1" == "jobsl" ] && \
        ./a.out \$tty_o "jobs -l  > /tmp/bash_test/result"
    [ "\$1" == "jobsn" ] && \
        ./a.out \$tty_o "jobs -n  > /tmp/bash_test/result"
    [ "\$1" == "jobsp" ] && \
        ./a.out \$tty_o "jobs -p  > /tmp/bash_test/result"
    [ "\$1" == "jobsr" ] && \
        ./a.out \$tty_o "jobs -r  > /tmp/bash_test/result"
    [ "\$1" == "jobss" ] && {
        ./a.out \$tty_o "killall -STOP sleep"
        ./a.out \$tty_o "jobs -s  > /tmp/bash_test/result"
        }
popd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值