Advertisement
miamondo

sed_substitute.sh

Mar 3rd, 2022 (edited)
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.73 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Copyright © 2022 Benoît Boudaud <https://miamondo.org/contact/>
  4. # This program is free software: you can redistribute it and/or modify it
  5. # under the terms of the GNU General Public License as published by the
  6. # Free Software Foundation, either version 3 of the License, or any later version. This
  7. # program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  8. # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. # See the GNU General Public License for more details. You should have received a copy of the GNU
  10. # General Public License along with this program. If not, see <http://www.gnu.org/licenses/>
  11.  
  12. # This code replaces a string by using the sed command. In the example below, I browse all launcher
  13. # files in the /usr/share/applications directory looking for the string "Exec=firefox http"
  14. # which I replace with "Exec=vivaldi-stable http."
  15.  
  16. directory=/usr/share/applications
  17.  
  18. for app in `ls "$directory"`
  19. do
  20.     if ! [[ -d "$app" ]]
  21.     then
  22.         sed -i 's/Exec=firefox http/Exec=vivaldi-stable http/g' $directory/$app
  23.     fi
  24. done
  25.  
  26. # -------------------------------------------------------------------------------------------------
  27.  
  28. # it is possible to pass three parameters to the script (Directory, replaced text, new text) So the
  29. # command would be:
  30.  
  31. #    sed_substitute.sh /usr/share/applications Exec=firefox http Exec=vivaldi-stable http
  32.  
  33. # And the code should be:
  34.  
  35. # for app in `ls $1`
  36. # do
  37. #    if ! [[ -d "$app" ]]
  38. #    then
  39. #        sed -i "s/$2/$3/g" $1/$app
  40. #    fi
  41. # done
  42.  
  43. # line 39 : Take care of double quoting the sed expression. You have to do so, if this one contains
  44. # variables. Otherwise, it doesn't work.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement