Move “if” conditions to variables

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am writting git hook file. I have following condition:



current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
release_branch_name="release"
develop_branch_name="develop"
master_branch_name="master"
hotfix_branch_name="hotfix/*"

if [[ ($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name)
|| ($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name) ]] ; then
#do something
fi


I would like to move conditions from if statement into a variable. I've done:



current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
release_branch_name="release"
develop_branch_name="develop"
master_branch_name="master"
hotfix_branch_name="hotfix/*"
is_merge_from_develop_to_release=$(($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name))
is_merge_from_hotfix_to_master=$(($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name))

if [[ $is_merge_from_develop_to_release || $is_merge_from_hotfix_to_master ]] ; then
#do something
fi


It gives me an error for hotfix/*, but it works if the entire condition is stuffed in the if statement.
How can properly decouple conditions from if ?



EDITED (final version):



function checkBranches sed "s;:;;")
hotfix_branch_name="hotfix/*"

[[ $current_branch == "release" &&
$merged_branch == "develop" ]] && return 0
[[ $current_branch == "master" &&
$merged_branch == $hotfix_branch_name ]] && return 0
return 1


if checkBranches ; then
#do something
fi









share|improve this question



























    up vote
    1
    down vote

    favorite












    I am writting git hook file. I have following condition:



    current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
    merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
    release_branch_name="release"
    develop_branch_name="develop"
    master_branch_name="master"
    hotfix_branch_name="hotfix/*"

    if [[ ($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name)
    || ($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name) ]] ; then
    #do something
    fi


    I would like to move conditions from if statement into a variable. I've done:



    current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
    merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
    release_branch_name="release"
    develop_branch_name="develop"
    master_branch_name="master"
    hotfix_branch_name="hotfix/*"
    is_merge_from_develop_to_release=$(($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name))
    is_merge_from_hotfix_to_master=$(($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name))

    if [[ $is_merge_from_develop_to_release || $is_merge_from_hotfix_to_master ]] ; then
    #do something
    fi


    It gives me an error for hotfix/*, but it works if the entire condition is stuffed in the if statement.
    How can properly decouple conditions from if ?



    EDITED (final version):



    function checkBranches sed "s;:;;")
    hotfix_branch_name="hotfix/*"

    [[ $current_branch == "release" &&
    $merged_branch == "develop" ]] && return 0
    [[ $current_branch == "master" &&
    $merged_branch == $hotfix_branch_name ]] && return 0
    return 1


    if checkBranches ; then
    #do something
    fi









    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am writting git hook file. I have following condition:



      current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
      merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
      release_branch_name="release"
      develop_branch_name="develop"
      master_branch_name="master"
      hotfix_branch_name="hotfix/*"

      if [[ ($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name)
      || ($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name) ]] ; then
      #do something
      fi


      I would like to move conditions from if statement into a variable. I've done:



      current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
      merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
      release_branch_name="release"
      develop_branch_name="develop"
      master_branch_name="master"
      hotfix_branch_name="hotfix/*"
      is_merge_from_develop_to_release=$(($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name))
      is_merge_from_hotfix_to_master=$(($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name))

      if [[ $is_merge_from_develop_to_release || $is_merge_from_hotfix_to_master ]] ; then
      #do something
      fi


      It gives me an error for hotfix/*, but it works if the entire condition is stuffed in the if statement.
      How can properly decouple conditions from if ?



      EDITED (final version):



      function checkBranches sed "s;:;;")
      hotfix_branch_name="hotfix/*"

      [[ $current_branch == "release" &&
      $merged_branch == "develop" ]] && return 0
      [[ $current_branch == "master" &&
      $merged_branch == $hotfix_branch_name ]] && return 0
      return 1


      if checkBranches ; then
      #do something
      fi









      share|improve this question















      I am writting git hook file. I have following condition:



      current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
      merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
      release_branch_name="release"
      develop_branch_name="develop"
      master_branch_name="master"
      hotfix_branch_name="hotfix/*"

      if [[ ($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name)
      || ($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name) ]] ; then
      #do something
      fi


      I would like to move conditions from if statement into a variable. I've done:



      current_branch_name=$(echo $(git branch | grep "*" | sed "s;* ;;"))
      merged_branch_name=$(echo $(git reflog -1) | cut -d" " -f 4 | sed "s;:;;")
      release_branch_name="release"
      develop_branch_name="develop"
      master_branch_name="master"
      hotfix_branch_name="hotfix/*"
      is_merge_from_develop_to_release=$(($current_branch_name == $release_branch_name && $merged_branch_name == $develop_branch_name))
      is_merge_from_hotfix_to_master=$(($current_branch_name == $master_branch_name && $merged_branch_name == $hotfix_branch_name))

      if [[ $is_merge_from_develop_to_release || $is_merge_from_hotfix_to_master ]] ; then
      #do something
      fi


      It gives me an error for hotfix/*, but it works if the entire condition is stuffed in the if statement.
      How can properly decouple conditions from if ?



      EDITED (final version):



      function checkBranches sed "s;:;;")
      hotfix_branch_name="hotfix/*"

      [[ $current_branch == "release" &&
      $merged_branch == "develop" ]] && return 0
      [[ $current_branch == "master" &&
      $merged_branch == $hotfix_branch_name ]] && return 0
      return 1


      if checkBranches ; then
      #do something
      fi






      bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 7 at 10:11

























      asked Aug 7 at 0:25









      user3529850

      1084




      1084




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Not really saving you lines but you could use a function:



          check_branch () sed "s;* ;;"))
          local merged_branch=$(echo $(git reflog -1)

          if check_branch; then
          #something
          fi


          Will your branch names change frequently? If not it would make more sense just to compare the variables against the strings: release, develop, master, hotfix/*.






          share|improve this answer




















          • thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
            – user3529850
            Aug 7 at 10:09


















          up vote
          0
          down vote













          case $(git branch |sed -nes/* //p
          )$( git -reflog 1|cut -d -f4
          ) in release:develop
          | master:hotfix/*
          ) : hooray!
          esac





          share|improve this answer



























            up vote
            0
            down vote













            With $(( )) you can do arithmetic, it is not for string comparisons.



            In general, you can convert



            if cmd; then ...


            to



            var=$(cmd; echo $?)
            if [[ $var ]]; then


            This will execute cmd and then echo the return status of cmd, assigning it to var.






            share|improve this answer




















            • No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
              – ilkkachu
              Aug 7 at 12:12










            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460927%2fmove-if-conditions-to-variables%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Not really saving you lines but you could use a function:



            check_branch () sed "s;* ;;"))
            local merged_branch=$(echo $(git reflog -1)

            if check_branch; then
            #something
            fi


            Will your branch names change frequently? If not it would make more sense just to compare the variables against the strings: release, develop, master, hotfix/*.






            share|improve this answer




















            • thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
              – user3529850
              Aug 7 at 10:09















            up vote
            3
            down vote



            accepted










            Not really saving you lines but you could use a function:



            check_branch () sed "s;* ;;"))
            local merged_branch=$(echo $(git reflog -1)

            if check_branch; then
            #something
            fi


            Will your branch names change frequently? If not it would make more sense just to compare the variables against the strings: release, develop, master, hotfix/*.






            share|improve this answer




















            • thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
              – user3529850
              Aug 7 at 10:09













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Not really saving you lines but you could use a function:



            check_branch () sed "s;* ;;"))
            local merged_branch=$(echo $(git reflog -1)

            if check_branch; then
            #something
            fi


            Will your branch names change frequently? If not it would make more sense just to compare the variables against the strings: release, develop, master, hotfix/*.






            share|improve this answer












            Not really saving you lines but you could use a function:



            check_branch () sed "s;* ;;"))
            local merged_branch=$(echo $(git reflog -1)

            if check_branch; then
            #something
            fi


            Will your branch names change frequently? If not it would make more sense just to compare the variables against the strings: release, develop, master, hotfix/*.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 7 at 1:23









            Jesse_b

            10.5k22659




            10.5k22659











            • thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
              – user3529850
              Aug 7 at 10:09

















            • thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
              – user3529850
              Aug 7 at 10:09
















            thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
            – user3529850
            Aug 7 at 10:09





            thanks, good points. I updated my question with the final version. I also had to remove " and leave string hotfix/ in variable to make it work.
            – user3529850
            Aug 7 at 10:09













            up vote
            0
            down vote













            case $(git branch |sed -nes/* //p
            )$( git -reflog 1|cut -d -f4
            ) in release:develop
            | master:hotfix/*
            ) : hooray!
            esac





            share|improve this answer
























              up vote
              0
              down vote













              case $(git branch |sed -nes/* //p
              )$( git -reflog 1|cut -d -f4
              ) in release:develop
              | master:hotfix/*
              ) : hooray!
              esac





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                case $(git branch |sed -nes/* //p
                )$( git -reflog 1|cut -d -f4
                ) in release:develop
                | master:hotfix/*
                ) : hooray!
                esac





                share|improve this answer












                case $(git branch |sed -nes/* //p
                )$( git -reflog 1|cut -d -f4
                ) in release:develop
                | master:hotfix/*
                ) : hooray!
                esac






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 7 at 0:45









                mikeserv

                44.3k564150




                44.3k564150




















                    up vote
                    0
                    down vote













                    With $(( )) you can do arithmetic, it is not for string comparisons.



                    In general, you can convert



                    if cmd; then ...


                    to



                    var=$(cmd; echo $?)
                    if [[ $var ]]; then


                    This will execute cmd and then echo the return status of cmd, assigning it to var.






                    share|improve this answer




















                    • No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                      – ilkkachu
                      Aug 7 at 12:12














                    up vote
                    0
                    down vote













                    With $(( )) you can do arithmetic, it is not for string comparisons.



                    In general, you can convert



                    if cmd; then ...


                    to



                    var=$(cmd; echo $?)
                    if [[ $var ]]; then


                    This will execute cmd and then echo the return status of cmd, assigning it to var.






                    share|improve this answer




















                    • No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                      – ilkkachu
                      Aug 7 at 12:12












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    With $(( )) you can do arithmetic, it is not for string comparisons.



                    In general, you can convert



                    if cmd; then ...


                    to



                    var=$(cmd; echo $?)
                    if [[ $var ]]; then


                    This will execute cmd and then echo the return status of cmd, assigning it to var.






                    share|improve this answer












                    With $(( )) you can do arithmetic, it is not for string comparisons.



                    In general, you can convert



                    if cmd; then ...


                    to



                    var=$(cmd; echo $?)
                    if [[ $var ]]; then


                    This will execute cmd and then echo the return status of cmd, assigning it to var.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 7 at 5:45









                    RalfFriedl

                    3,5601522




                    3,5601522











                    • No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                      – ilkkachu
                      Aug 7 at 12:12
















                    • No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                      – ilkkachu
                      Aug 7 at 12:12















                    No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                    – ilkkachu
                    Aug 7 at 12:12




                    No, you'd have to use if [[ $var = 0 ]]; then.... Try var=$(false; echo $?); if [[ $var ]]; then echo hello; fi And unless you need to tell the difference between different falsy values (like 2, which often indicates an error), it's not really useful to store the exit value. Just put the code in a function and call it from the if.
                    – ilkkachu
                    Aug 7 at 12:12

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460927%2fmove-if-conditions-to-variables%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?