bash – 从交互式命令到更少的管道输出

前端之家收集整理的这篇文章主要介绍了bash – 从交互式命令到更少的管道输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想做点什么
  1. openssl enc -d -aes256 -in somefile | less

openssl需要来自stdin的密码.当涉及的较少时,这会搞砸.

有没有办法从交互式命令(如openssl要求输入密码)获取输出并将输出管道输入更少?

或者使用bash脚本有更好的技术吗?

也许让shell脚本请求密钥,然后将密钥存储在临时文件中并使用openssl的-kfile选项来查找它.希望你的openssl版本支持-kfile.

我担心这会带来安全感,但有点小心,安全漏洞可能比你想象的要小. (但你相信你的系统管理员和sudoers ……?)

  1. #!/bin/bash
  2.  
  3. INFILE=somefile
  4.  
  5. read -s -p "Enter key for $INFILE: " key
  6. echo
  7.  
  8. # write the key to a temp file; use mktemp(1)
  9. # for safer creation of a privately-owned file.
  10. #
  11. TMPKEY=$(mktemp -t) || exit 1
  12. echo "$key" > "$TMPKEY"
  13.  
  14. # will remove the temp file on script exit
  15. trap 'rm -f "$TMPKEY"' EXIT
  16.  
  17. # remove the key file a couple seconds after openssl runs
  18. (sleep 2; rm -f "$TMPKEY") &
  19.  
  20. openssl enc -d -aes256 -in "$INFILE" -kfile "$TMPKEY" | less
  21.  
  22. [ -f "$TMPKEY" ] && rm -f "$TMPKEY"
  23. trap - EXIT
  24.  
  25. # rest of script...
  26.  
  27. exit 0

猜你在找的Bash相关文章