run-plus-calc.sh 1.1 KB

1234567891011121314151617181920212223242526272829
  1. #!/bin/bash
  2. # Shows a run menu with calculator support.
  3. # Deps:
  4. # - rofi (can easily be converted to dmenu)
  5. # - python (2 or 3) for calculations
  6. # - notification system setup (`notify-send` should work properly)
  7. # Running:
  8. # - Save the file to anything.sh
  9. # - chmod +x anything.sh
  10. # - ./anything.sh
  11. # - It should show a menu with executables. Input app name or math. Enjoy!
  12. # If you enter math, it should show the result as a notification.
  13. # Otherwise should run the program you selected.
  14. # Example input: geany, 2+2, 2-1, 10/3, 2*2=, (3^2)*10
  15. input=$( find ${PATH//:/ } -maxdepth 1 -executable -printf '%f\n' | rofi -dmenu -i )
  16. if grep -q '+\|-[[:digit:]]\|\*\|\/\|\^' <<<"$input"; then
  17. # If there is an equal sign (=) at the end, get rid of it.
  18. # Replace "^" with "**" so that it can calculate powers.
  19. input=$(echo "$input" | sed 's/\=//g;s/\^/**/g')
  20. # Show the result in a notification.
  21. # The `float(1)*` part is so that Python 2 returns decimal places.
  22. notify-send "The result is: "$( python -c "print( float(1)*$input )" )
  23. else
  24. # Not a calculation, so run the command.
  25. $($input)
  26. fi