Results 1 to 8 of 8

Thread: bash, toggel variable value?

  1. #1
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    bash, toggel variable value?

    Hay all,

    Somehow I can not get an answer from google for this. I wan to do a simple if, then, to toggle the value of a variable between 1 and 0:

    example:
    Code:
    var="0"
    selection="a"
    
    if [ $selection = "a" ]; then toggle $var; fi
    
    echo $var
    The result should be "1".

    I want to use this in a little interactive script for selecting and deselecting certain options. All the variables values are either zero or one.

    Thanks in advance

    EDIT:
    Just be clear I want $var to toggle between 1 and 0:
    Code:
    echo $var
    0
    echo $var
    1
    echo $var
    0
    echo $var
    1
    Last edited by zero2xiii; May 6th, 2013 at 03:01 PM.
    Switched away from windows XP to Ubuntu 9.04. Never turned around to look back.

  2. #2
    Join Date
    May 2010
    Location
    uk
    Beans
    9,249
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: bash, toggel variable value?

    Hi

    Something along the lines of...
    Code:
    selection="a";
    var=;
    
    [ "$selection" = "a" ] && var="1" || var="0";
    
    echo $var;
    Is that the kind of thing you want ?

    Or is it more like this ?

    Code:
    selection="a";
    var=0;
    
    [ "$selection" = "a" ] && { [ "$var" = "0" ] && var="1" || var="0"; } 
    
    echo "$var"
    Kind regards
    Last edited by matt_symes; May 6th, 2013 at 02:30 PM. Reason: Reread the original post
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  3. #3
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash, toggel variable value?

    Hay,

    The second option works the way I intend it:

    Code:
    ~/:~$ [ "$selection" = "a" ] && var="1" || var="0";
    ~/:~$ echo $var
    1
    ~/:~$ [ "$selection" = "a" ] && { [ $var = "0" ] && var="1" || var="0"; }
    ~/:~$ echo $var
    0
    ~/:~$ [ "$selection" = "a" ] && { [ $var = "0" ] && var="1" || var="0"; }
    ~/:~$ echo $var
    1
    ~/:~$ [ "$selection" = "a" ] && { [ $var = "0" ] && var="1" || var="0"; }
    ~/:~$ echo $var
    0
    I will try this in the script and see if it works there .

    Thanks for the quick response!

    EDIT: It works in the script and I will credit you for the help . The script will be posted in my signature's link "help us help you" thread when I am done.
    Thanks
    Last edited by zero2xiii; May 6th, 2013 at 03:02 PM.
    Switched away from windows XP to Ubuntu 9.04. Never turned around to look back.

  4. #4
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash, toggel variable value?

    you might also try the following code
    Code:
    var=0
    selection="a"
    
    if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    
    echo $var

  5. #5
    Join Date
    Jul 2011
    Location
    South-Africa
    Beans
    678
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash, toggel variable value?

    Hay,

    That does not do what I want, it simply counts up:

    Code:
    ~/Scripts/DebugTool/:~$ var=0
    ~/Scripts/DebugTool/:~$ selection="a"
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    1
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    2
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    3
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    4
    ~/Scripts/DebugTool/:~$ 
    ]
    The above post's is a fix so thanks. It also works perfectly in the script so I will use that. Thanks
    Last edited by matt_symes; May 6th, 2013 at 03:01 PM. Reason: Fixed code tags :)
    Switched away from windows XP to Ubuntu 9.04. Never turned around to look back.

  6. #6
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash, toggel variable value?

    Quote Originally Posted by zero2xiii View Post
    Hay,

    That does not do what I want, it simply counts up:

    Code:
    ~/Scripts/DebugTool/:~$ var=0
    ~/Scripts/DebugTool/:~$ selection="a"
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    1
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    2
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    3
    ~/Scripts/DebugTool/:~$ if [ "$selection" = "a" ]; then let "var=-(var=~var)"; fi
    ~/Scripts/DebugTool/:~$ echo $var
    4
    ~/Scripts/DebugTool/:~$ 
    ]
    The above post's is a fix so thanks. It also works perfectly in the script so I will use that. Thanks
    Sorry, in my previous post I wrongly used the "~" (bitwise not) operator instead of "^" (bitwise xor) .
    Hopefully the following code should do what you wish:
    Code:
    var=0
    selection="a"
    
    if [ "$selection" = "a" ]; then let "var=1^var"; fi
    
    echo $var
    
    let "var=1^var"
    echo $var
    Last edited by alan9800; May 6th, 2013 at 10:02 PM.

  7. #7
    Join Date
    Jun 2006
    Location
    Brisbane Australia
    Beans
    713

    Re: bash, toggel variable value?

    To toggle var:
    Code:
    var=$((var==0))

  8. #8
    Join Date
    Apr 2013
    Location
    43.49°N 7.46°E
    Beans
    117
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash, toggel variable value?

    Quote Originally Posted by markbl View Post
    To toggle var:
    Code:
    var=$((var==0))
    or also
    Code:
    var=$((1-var))
    which avoids confusion between boolean values and arithmetic ones.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •