Advertisement
Spirit_of_Stallman

word replacer new

Feb 13th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.02 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. help_message="
  4. Script have 2 modes: interactive and noninteractive.
  5. For interactive mode use args:
  6. -i  insensitive search for replacement
  7. -v  verbose entered args
  8. -I  start interactive mode
  9. Exemple:
  10. scriptname -I [-i]
  11.  
  12. For noninteractive use args:
  13. -i  insensitive search for replacement
  14. -v  verbose entered args
  15. -d  path to target directory
  16. -t  a word or expression that must be replaced
  17. -r  word or expression to be replaced by
  18. Exemple:
  19. scriptname [-i] -d /path/to/dir -t 'exemple_1' -r 'i love exemples'
  20. or with regexp:
  21. scriptname [-i] -d /path/to/dir -t 'ex..ple_1$' -r 'i love exemples'
  22. "
  23.  
  24. function main_work(){
  25.     for arg in $script_args; do case $arg in "-i") insensitive_trigger="1";; "-v") dbg="1";; esac; done
  26.    
  27.     [ "$interactive_trigger" == "1" ] && {
  28.         [[ "$dir_path" || "$old_expression" || "$new_expression" ]] && echo -e "why you use args in interactive mode?\n"
  29.  
  30.         read -r -p "path to work dir > " dir_path
  31.         IFS= read -r -p "target word      > " old_expression
  32.         IFS= read -r -p "replacement word > " new_expression
  33.     }
  34.     [ ! -d "$dir_path" ] && { echo -e "\nDirectory \"$dir_path\" does not exist\nMake sure that the path exists"; error_exit_trigger="1"; }
  35.     [ -z "$old_expression" ] && { echo -e "\nNot found the target word"; error_exit_trigger="1"; }
  36.     [ -z "$new_expression" ] && { echo -e "\nNot found the word for replacement"; error_exit_trigger="1"; }
  37.     [ "$error_exit_trigger" == "1" ] && exit
  38.  
  39.     for file in $(find $dir_path -type f); do
  40.         if [ "$insensitive_trigger" == "1" ]; then
  41.             sed -E -i -e "s/$old_expression/$new_expression/ig" $file
  42.         else
  43.             sed -E -i -e "s/$old_expression/$new_expression/g" $file
  44.         fi
  45.     done
  46.    
  47.     [ "$dbg" == "1" ] && echo -e "\ndir path   : $dir_path\ntarget word: $old_expression\nreplaced by: $new_expression\ninsensitive: $insensitive_trigger\n"
  48.  
  49.     echo -e "Done\nHave a nice day"
  50.     exit
  51. }
  52. function error_handler(){
  53.     error_value="$OPTARG"
  54.     [ "$error_value" == "d" ] && {
  55.         echo -e "Err: -d must contain folder path argument"
  56.         error_exit_trigger="1"
  57.     }
  58.     [ "$error_value" == "t" ] && {
  59.         echo -e "Err: -t must contain target word or expression"
  60.         error_exit_trigger="1"
  61.     }
  62.     [ "$error_value" == "r" ] && {
  63.         echo -e "Err: -r must contain target word or expression for replacement"
  64.         error_exit_trigger="1"
  65.     }
  66.     [ "$error_exit_trigger" == "1" ] && {
  67.         echo "For help use -h"
  68.         exit
  69.     }
  70. }
  71.  
  72. script_args="$@"
  73. insensitive_trigger="0"
  74. error_exit_trigger="0"
  75. dbg="0"
  76.  
  77. for arg in $script_args;do
  78.     case $arg in
  79.         "-h"|"--help") echo "$help_message" && exit ;;
  80.         "-I") interactive_trigger="1"; main_work;   ;;
  81.     esac
  82. done
  83.  
  84. while getopts ":d:t:r:" arg;do
  85.     case $arg in
  86.         "d") dir_path="$OPTARG"       ;;
  87.         "t") old_expression="$OPTARG" ;;
  88.         "r") new_expression="$OPTARG" ;;
  89.         :)   error_handler            ;;
  90.     esac
  91. done
  92.  
  93. main_work
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement