My scripts for startup, dmenu, and the command line
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

edit-ini 1.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh
  2. #The script inserts or replaces an ini field where $1 is the path of the target file and $2 is the value to add or replace. $2 has the form section:property=value. It only cares about the first instance of $2
  3. file=$2
  4. change=$3
  5. section=$(printf $change | cut -d ':' -f 1)
  6. property=$(printf $change | cut -d ':' -f 2)
  7. property_type=$(printf $property | cut -d '=' -f 1)
  8. ex_blank='^\s*$'
  9. ex_any_sec='^\[.*\]$'
  10. #ex_sec='/^\[$section\]/,/^\[.*\]$\|^\s*$/'
  11. check_args() {
  12. if [ $1 -ne $2 ]; then
  13. printf "Invalid number of arguments. Expected $2\n$@\n"
  14. exit;
  15. fi
  16. return 0;
  17. }
  18. replace() {
  19. sed -i "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {s/^$property_type=.*$/$property/ }" $1
  20. }
  21. add() {
  22. #Check if the section exists. If not, abort.
  23. if [ -z "$(sed "/^\[$section\]$/p" $1)" ]; then printf "section $section does not exist. Aborting.\n" exit; fi
  24. sed -i "/^\[$section\]/a $property" $1
  25. }
  26. insert() {
  27. #Check if the field is already there
  28. field=$(sed -n "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {/^$property_type=.*$/p}" $1)
  29. if [ -z $field ]; then add $1; else replace $1; fi
  30. }
  31. get() {
  32. #Prints a section of the ini file
  33. sed -n "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ p" $1
  34. }
  35. make() {
  36. sed -i "$ G; $ a [$section]" $1
  37. }
  38. sdelete() {
  39. sed -i "/^\[$section\]/,/$ex_any_sec\|$ex_blank/ {/$ex_any_sec/! d; /\[$section\]/d}" $1
  40. }
  41. check_args $# 3
  42. case $1 in
  43. insert) insert $2 $3;;
  44. get) get $2 $3;;
  45. make) make $2 $3;;
  46. sdelete) sdelete $2 $3;;
  47. *) printf "invalid command $1";;
  48. esac