【问题标题】:Linux a long shell command with pipe operationLinux 一个带有管道操作的长 shell 命令
【发布时间】:2019-05-29 14:00:13
【问题描述】:

这是我的命令。

ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | tar xpf - -C /usr

问题是后面的 tar 命令需要sudo,所以如果我改变我的代码像 ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | sudo tar xpf - -C /usr。但是我想自动运行,这种情况不是我想要的。

如果我将命令更改为

ssh username@hostname "cd /usr ; echo \"password\" | sudo -S tar cpf - . --ignore-failed-read" | echo "password" | sudo -S tar xpf - -C /usr。这种情况下无法获取到之前的tar数据流。

那么我怎样才能同时获得前一个 tar 数据流并将sudo 提供给我的后一个 tar 命令?

PS:我不想创建实际的 tar 文件。我希望它即时压缩和解压缩。

有人可以帮助我吗?非常感谢~!

【问题讨论】:

  • 听起来你在问如何配置 sudo 使其不需要密码。
  • 但实际上我无权配置 sudo,因此它不需要密码
  • 对于Super UserUnix & Linux Stack Exchange 来说可能是一个更好的问题。在没有sudo 的情况下解压,然后执行sudo cp -r 将其放置在需要的位置。
  • 听起来不错,但是如果文件太多,磁盘太小而无法放置新文件怎么办。我想要的是覆盖现有文件。

标签: linux shell pipe tar


【解决方案1】:

将数据传送到fifo,从stdin读取密码:

trap 'rm -f /tmp/fifo' 0
mkfifo /tmp/fifo
ssh username@hostname 'cd /usr ; echo "password" |
     sudo -S tar cpf - . --ignore-failed-read' > /tmp/fifo & 
echo "${password?}" | sudo -S sh -c 'tar xpf - -C /usr < /tmp/fifo'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多