Move âifâ conditions to variables
Clash 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
bash
add a comment |Â
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
bash
add a comment |Â
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
bash
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
bash
edited Aug 7 at 10:11
asked Aug 7 at 0:25
user3529850
1084
1084
add a comment |Â
add a comment |Â
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/*
.
thanks, good points. I updated my question with the final version. I also had to remove"
and leave stringhotfix/
in variable to make it work.
â user3529850
Aug 7 at 10:09
add a comment |Â
up vote
0
down vote
case $(git branch |sed -nes/* //p
)$( git -reflog 1|cut -d -f4
) in release:develop
| master:hotfix/*
) : hooray!
esac
add a comment |Â
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
.
No, you'd have to useif [[ $var = 0 ]]; then...
. Tryvar=$(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 theif
.
â ilkkachu
Aug 7 at 12:12
add a comment |Â
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/*
.
thanks, good points. I updated my question with the final version. I also had to remove"
and leave stringhotfix/
in variable to make it work.
â user3529850
Aug 7 at 10:09
add a comment |Â
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/*
.
thanks, good points. I updated my question with the final version. I also had to remove"
and leave stringhotfix/
in variable to make it work.
â user3529850
Aug 7 at 10:09
add a comment |Â
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/*
.
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/*
.
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 stringhotfix/
in variable to make it work.
â user3529850
Aug 7 at 10:09
add a comment |Â
thanks, good points. I updated my question with the final version. I also had to remove"
and leave stringhotfix/
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
add a comment |Â
up vote
0
down vote
case $(git branch |sed -nes/* //p
)$( git -reflog 1|cut -d -f4
) in release:develop
| master:hotfix/*
) : hooray!
esac
add a comment |Â
up vote
0
down vote
case $(git branch |sed -nes/* //p
)$( git -reflog 1|cut -d -f4
) in release:develop
| master:hotfix/*
) : hooray!
esac
add a comment |Â
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
case $(git branch |sed -nes/* //p
)$( git -reflog 1|cut -d -f4
) in release:develop
| master:hotfix/*
) : hooray!
esac
answered Aug 7 at 0:45
mikeserv
44.3k564150
44.3k564150
add a comment |Â
add a comment |Â
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
.
No, you'd have to useif [[ $var = 0 ]]; then...
. Tryvar=$(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 theif
.
â ilkkachu
Aug 7 at 12:12
add a comment |Â
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
.
No, you'd have to useif [[ $var = 0 ]]; then...
. Tryvar=$(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 theif
.
â ilkkachu
Aug 7 at 12:12
add a comment |Â
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
.
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
.
answered Aug 7 at 5:45
RalfFriedl
3,5601522
3,5601522
No, you'd have to useif [[ $var = 0 ]]; then...
. Tryvar=$(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 theif
.
â ilkkachu
Aug 7 at 12:12
add a comment |Â
No, you'd have to useif [[ $var = 0 ]]; then...
. Tryvar=$(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 theif
.
â 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password