lesspipe.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. #
  3. # To use this filter with less, define LESSOPEN:
  4. # export LESSOPEN="|/usr/bin/lesspipe.sh %s"
  5. #
  6. # The script should return zero if the output was valid and non-zero
  7. # otherwise, so less could detect even a valid empty output
  8. # (for example while uncompressing gzipped empty file).
  9. # For backward-compatibility, this is not required by default. To turn
  10. # this functionality there should be another vertical bar (|) straight
  11. # after the first one in the LESSOPEN environment variable:
  12. # export LESSOPEN="||/usr/bin/lesspipe.sh %s"
  13. if [ ! -e "$1" ] ; then
  14. exit 1
  15. fi
  16. if [ -d "$1" ] ; then
  17. ls -alF -- "$1"
  18. exit $?
  19. fi
  20. exec 2>/dev/null
  21. case "$1" in
  22. *.[1-9n].bz2|*.[1-9]x.bz2|*.man.bz2|*.[1-9n].[gx]z|*.[1-9]x.[gx]z|*.man.[gx]z|*.[1-9n].lzma|*.[1-9]x.lzma|*.man.lzma)
  23. case "$1" in
  24. *.gz) DECOMPRESSOR="gzip -dc" ;;
  25. *.bz2) DECOMPRESSOR="bzip2 -dc" ;;
  26. *.xz|*.lzma) DECOMPRESSOR="xz -dc" ;;
  27. esac
  28. if [ -n "$DECOMPRESSOR" ] && $DECOMPRESSOR -- "$1" | file - | grep -q troff; then
  29. $DECOMPRESSOR -- "$1" | groff -Tascii -mandoc -
  30. exit $?
  31. fi ;;&
  32. *.[1-9n]|*.[1-9]x|*.man)
  33. if file "$1" | grep -q troff; then
  34. man -l "$1" | cat -s
  35. exit $?
  36. fi ;;&
  37. *.tar) tar tvvf "$1" ;;
  38. *.tgz|*.tar.gz|*.tar.[zZ]) tar tzvvf "$1" ;;
  39. *.tar.xz) tar Jtvvf "$1" ;;
  40. *.xz|*.lzma) xz -dc -- "$1" ;;
  41. *.tar.bz2|*.tbz2) bzip2 -dc -- "$1" | tar tvvf - ;;
  42. *.[zZ]|*.gz) gzip -dc -- "$1" ;;
  43. *.bz2) bzip2 -dc -- "$1" ;;
  44. *.zip|*.jar|*.nbm) zipinfo -- "$1" ;;
  45. *.rpm) rpm -qpivl --changelog -- "$1" ;;
  46. *.cpi|*.cpio) cpio -itv < "$1" ;;
  47. *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
  48. if [ -x /usr/bin/identify ]; then
  49. identify "$1"
  50. elif [ -x /usr/bin/gm ]; then
  51. gm identify "$1"
  52. else
  53. echo "No identify available"
  54. echo "Install ImageMagick or GraphicsMagick to browse images"
  55. exit 1
  56. fi ;;
  57. *)
  58. if [ -x /usr/bin/file ] && [ -x /usr/bin/iconv ] && [ -x /usr/bin/cut ]; then
  59. case `file -b "$1"` in
  60. *UTF-16*) conv='UTF-16' ;;
  61. *UTF-32*) conv='UTF-32' ;;
  62. esac
  63. if [ -n "$conv" ]; then
  64. env=`echo $LANG | cut -d. -f2`
  65. if [ -n "$env" -a "$conv" != "$env" ]; then
  66. iconv -f $conv -t $env "$1"
  67. exit $?
  68. fi
  69. fi
  70. fi
  71. exit 1
  72. esac
  73. exit $?