Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #TextInFile! A cute, little utility that allows you to search for the text inside directories from the command-line.
  2. #Author : - Shashank Srivastava
  3. #Date : - 16 August 2017
  4. #set -x
  5. #!/bin/bash
  6. echo ""
  7. if [ $# -eq 0 ]
  8. then
  9. echo "Error!! Please enter absolute path of the directory inside which you want to perform the search. Run this utility as ./textinfile /path/to/directory. Prefix sudo if needed."
  10. echo ""
  11. exit
  12. fi
  13. DIRNAME=$1
  14. if [ -d $DIRNAME ]
  15. then
  16. echo "Detecting OS of your machine..."
  17. echo ""
  18. OS=`uname -v | awk '{print$1}' | sed 's:.*-::'`
  19. if [ "$OS" == "Darwin" ]
  20. then
  21. echo "Your OS is : - Mac OS X."
  22. elif [ "$OS" == "Ubuntu" ]
  23. then
  24. echo "Your OS is : - Ubuntu."
  25. else
  26. echo "Your OS is : - RHEL."
  27. fi
  28. echo ""
  29. echo "Checking figlet insallation..."
  30. echo ""
  31. which figlet
  32. if [ $? -ne 0 ]
  33. then
  34. echo "This utility requires a package called figlet in order to display a cool ASCII art ;-) Do you want to install it? Its a one time process only. You can safely ignore this insallation. Press y to install or n to cancel."
  35. read ANSWER
  36. if [ $ANSWER == "y" ] && [ $OS == "Darwin" ]
  37. then
  38. echo "You chose to install figlet. Installing it for your OS now :-)"
  39. brew install figlet
  40. elif [ $ANSWER == "y" ] && [ $OS == "Ubuntu" ]
  41. then
  42. echo "You chose to install figlet. Installing it for your OS now :-)"
  43. sudo apt-get install figlet -y
  44. elif [ $ANSWER == "n" ]
  45. then
  46. echo "You chose not to install figlet :-( No cool ASCII art will be diplayed!"
  47. fi
  48. else
  49. echo "figlet already installed! Lets get started :-)"
  50. fi
  51. echo "TextInFile" | figlet 2>/dev/null
  52. echo "Welcome to TextInFile! A cute, little utility that allows you to search for the text inside directories from the command-line :-)"
  53. echo ""
  54. echo -n "Enter the string that you want to search inside $DIRNAME..."
  55. read TEXT_STRING
  56. echo ""
  57. echo "Searching for $TEXT_STRING in $DIRNAME..."
  58. echo ""
  59. NUM=`grep -r -i "$TEXT_STRING" $DIRNAME | wc -l`
  60. if [ $NUM -eq 0 ]
  61. then
  62. echo "Sorry, $TEXT_STRING wasn't found anywhere inside $DIRNAME."
  63. else
  64. echo "$TEXT_STRING was found $NUM time(s) inside $DIRNAME."
  65. echo ""
  66. echo "Below is your Search Result :-)"
  67. fi
  68. echo ""
  69. grep -r -i "$TEXT_STRING" -n $DIRNAME
  70. echo ""
  71. echo "This utility took $SECONDS seconds to run(including the time you took to type/paste your string)."
  72. echo ""
  73. echo "That's all folks! Hope you liked TextInFile ;-)"
  74. else
  75. echo "The directory $DIRNAME that you specified does't exist. Please choose the correct directory."
  76. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement