0

i am trying to write a bash script to quick change the $mod key of i3 i moved the "set $mod Mod4" to the end of my conf

#!/bin/bash $1=key $alt="alt" $win="win" sed '$d' /home/fabian/.config/i3/config if [ "$key" = "$win" ]; then echo "set \$mod Mod4" >> /home/fabian/.config/i3/config echo echo "Changed successfully to win" else if [ "$key" = "$alt" ]; then echo "set \$mod Mod1" >> /home/fabian/.config/i3/config echo echo "Changed successfully to alt" else echo "No Flag valid flag set, set alt or win" fi fi 

two problems: the sed command is not working even if i run with sudo

if i run the script with "changeMod.sh alt" it still goes into the win condition and echos "Changed successfully to win"

if it's possible i would like the script to insert old $mod+shift+r to reload the i3 conf

Do you know how this is possible?

0

    1 Answer 1

    1

    The main problem is your sed is missing the -i flag which would allow it to edit the file in place, but your method is unsafe — if you ran the script without a valid command multiple times you’d remove a line from your config every time. You also have a lot of duplication of effort.

    This should do the trick in a safer, more succinct fashion. Creates a backup copy of the config, but you could prevent this by removing .backup from the sed line (subject to the available features of your sed).

    #!/bin/bash conf="/home/fabian/.config/i3/config" case "${1}" in (alt) key=Mod1 ;; (win) key=Mod4 ;; (*) printf "Invalid key: %s\n" "${1}" ; exit 1 ;; esac sed -i.backup 's/^\(set $mod\) .*/\1 '"${key}/" "${conf}" || exit printf "Changed successfully to %s/%s\n" "${1}" "${key}" 
    3
    • great solution! do you also know if it's possible to insert a shortcut?CommentedApr 22, 2022 at 9:10
    • i found that it is possible to use xdotool for inserting shortcuts but i don't know how to check the current $mod key. I would need to find out the current mod key which is the last word of my i3 config file is that possible with sed?CommentedApr 22, 2022 at 15:52
    • sed is not the best tool for checking a current setting, I would probably use awk — but that’s a different question, so ask a new question.
      – bxm
      CommentedApr 24, 2022 at 7:04

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.