Bash - rewriting variable in bash script?

Discussion in 'Tech Discussion' started by He, Sep 24, 2017.

Tags:
  1. He

    He Stop using the net, you net addict.

    Joined:
    Sep 12, 2017
    Messages:
    206
    Likes Received:
    116
    Reading List:
    Link
    So, I want to toggle a few of my key to remap them to numeric. While this is trivial to accomplish using xmodmap, the one I am confused about is on assigning variable to a bash script so that they don't persist in the script.

    The algorithm would be:
    1. Assign a variable with binary value, with default equal to 1.
    2. If the variable value is equal to 1, remap to numeric keys, toggle the variable to 0. Else if the variable is equal to 0, remap to default key, toggle variable value to 1.

    The question is on how to do it so the toggle persists.

    While it is possible to fetch value to be assigned from a file, and rewriting the file later, is there no way to edit the script?
     
  2. InvincibleDespair

    InvincibleDespair Well-Known Member

    Joined:
    Jan 21, 2017
    Messages:
    797
    Likes Received:
    463
    Reading List:
    Link
    Do you know CS50?
     
  3. He

    He Stop using the net, you net addict.

    Joined:
    Sep 12, 2017
    Messages:
    206
    Likes Received:
    116
    Reading List:
    Link
    nope. I'm pure statistician.
    Edit:Oh... If it is CS101 equivalent at uni, I do take it.
     
  4. elengee

    elengee Daoist Ninefaps

    Joined:
    Mar 15, 2016
    Messages:
    13,488
    Likes Received:
    25,896
    Reading List:
    Link
    When i read 'Bash' i was expecting a Qidian related thread. :hmm:
     
  5. rsyh93

    rsyh93 Active Member

    Joined:
    Jan 22, 2016
    Messages:
    2
    Likes Received:
    1
    Reading List:
    Link
    If there's anyway to determine the value from the output of a command that would probably be the most self contained solution. From reading the Arch wiki, it looks like the output of xmodmap -pke might do what you want.

    EDIT: wrong option
     
    Last edited: Sep 24, 2017
  6. kryner

    kryner BYSTANDER BY NATURE

    Joined:
    Nov 14, 2015
    Messages:
    425
    Likes Received:
    399
    Reading List:
    Link
    Hard to say without data.
     
  7. He

    He Stop using the net, you net addict.

    Joined:
    Sep 12, 2017
    Messages:
    206
    Likes Received:
    116
    Reading List:
    Link
    hmmm... Xmodmap part is solved actually. I've bound two script (one to modify and one to return tl default) to two different keybinding. Now I want to make it into one script where the toggle function works.

    what kind of data do you want? The script? Sure.
     
  8. fjord

    fjord Active Member

    Joined:
    Jul 27, 2017
    Messages:
    1
    Likes Received:
    0
    Reading List:
    Link
    Two options I can think about:
    1) Instead of using a variable, you should be able to check if the current mapping is the original mapping or the changed mapping. The script would check which state the mappings are in and toggle to the other mapping instead, of course this assumes that the mapping persists.
    2) If you want the variable itself to be changed in the script after each toggle, then you should look up self-modifying bash scripts on stack overflow, as there are threads on this topic.

    Hopefully this helps answer your question.
     
  9. rsyh93

    rsyh93 Active Member

    Joined:
    Jan 22, 2016
    Messages:
    2
    Likes Received:
    1
    Reading List:
    Link
    What I mean is taking the output of xmodmap -pke, which prints out your current bindings, and using that as to determine your 0 or 1. So something like "xmodmap -pke | grep keycode"
     
    He likes this.
  10. He

    He Stop using the net, you net addict.

    Joined:
    Sep 12, 2017
    Messages:
    206
    Likes Received:
    116
    Reading List:
    Link
    thank you!!! I just need to map mode_switch keysym and map the others to those of 3rd and 4th column.

    No more RSI pain, I hope...