Problem with using time check with if statments - BASHGet the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashCheck existence of input argument in a Bash shell scriptEcho newline in Bash prints literal n
Multi tool use
If Trump gets impeached, how long would Pence be president?
May a man marry the women with whom he committed adultery?
Where to find an interactive PDF or HTML version of the tex.web documentation?
If a 2019 UA artificer has the Repeating Shot infusion on two hand crossbows, can they use two-weapon fighting?
Aftermath of nuclear disaster at Three Mile Island
What do you call a flexible diving platform?
Request for a Latin phrase as motto "God is highest/supreme"
Vertical tennis ball into fancy new enumerate
Converting 8V AC to 8V DC - bridge rectifier gets very hot while idling
How do I explain an exponentially complex intuitively?
What does "see" in "the Holy See" mean?
What are the different qualities of the intervals?
Old French song lyrics with the word "baiser."
Word for showing a small part of something briefly to hint to its existence or beauty without fully uncovering it
Japanese reading of an integer
How did Mysterio have these drones?
Finding minimum time for vehicle to reach to its destination
Does a Rogue's Evasion work for spells?
Checking if an integer is a member of an integer list
Is it legal to use cash pulled from a credit card to pay the monthly payment on that credit card?
How to check what is edible on an alien world?
TSA asking to see cell phone
Suggestions for protecting jeans from saddle clamp bolt
Why does Canada require mandatory bilingualism in all government posts?
Problem with using time check with if statments - BASH
Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?How do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to check if a variable is set in Bash?How to concatenate string variables in BashCheck existence of input argument in a Bash shell scriptEcho newline in Bash prints literal n
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am currently checking a .sh script just to do some basic things.
chmod +x catbash.sh
echo 'Hello, Please Enter your User Name'
echo
read VarUserName
currenttime=$(date +%H:%M)
if [[ $currenttime > 11:59 ]] || [[ $currenttime < 12:00 ]];
then echo 'Good Morning' $VarUserName'.'
fi
if [[ $currenttime > 12:00 ]] || [[ $currenttime < 16:59 ]];
then echo 'Good Afternoon' $VarUserName'.'
fi
if [[ $currenttime > 17:00 ]] || [[ $currenttime < 19:59 ]];
then echo 'Good Evening' $VarUserName'.'
fi
if [[ $currenttime > 20:00 ]] || [[ $currenttime < 23:59 ]];
then echo 'Good Night' $VarUserName'.'
fi
My issue is that i am trying to use the systems current time to be used in a if statement and depending on the time for a different output.
right now the script outputs all all of the "good...." echos and does not output a single echo depending on the time.
thank you for the help.
bash
add a comment |
I am currently checking a .sh script just to do some basic things.
chmod +x catbash.sh
echo 'Hello, Please Enter your User Name'
echo
read VarUserName
currenttime=$(date +%H:%M)
if [[ $currenttime > 11:59 ]] || [[ $currenttime < 12:00 ]];
then echo 'Good Morning' $VarUserName'.'
fi
if [[ $currenttime > 12:00 ]] || [[ $currenttime < 16:59 ]];
then echo 'Good Afternoon' $VarUserName'.'
fi
if [[ $currenttime > 17:00 ]] || [[ $currenttime < 19:59 ]];
then echo 'Good Evening' $VarUserName'.'
fi
if [[ $currenttime > 20:00 ]] || [[ $currenttime < 23:59 ]];
then echo 'Good Night' $VarUserName'.'
fi
My issue is that i am trying to use the systems current time to be used in a if statement and depending on the time for a different output.
right now the script outputs all all of the "good...." echos and does not output a single echo depending on the time.
thank you for the help.
bash
2
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needsAND
instead ofOR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use ofelif
so that no more than one technically can print out ..
– Zak
Mar 26 at 18:59
add a comment |
I am currently checking a .sh script just to do some basic things.
chmod +x catbash.sh
echo 'Hello, Please Enter your User Name'
echo
read VarUserName
currenttime=$(date +%H:%M)
if [[ $currenttime > 11:59 ]] || [[ $currenttime < 12:00 ]];
then echo 'Good Morning' $VarUserName'.'
fi
if [[ $currenttime > 12:00 ]] || [[ $currenttime < 16:59 ]];
then echo 'Good Afternoon' $VarUserName'.'
fi
if [[ $currenttime > 17:00 ]] || [[ $currenttime < 19:59 ]];
then echo 'Good Evening' $VarUserName'.'
fi
if [[ $currenttime > 20:00 ]] || [[ $currenttime < 23:59 ]];
then echo 'Good Night' $VarUserName'.'
fi
My issue is that i am trying to use the systems current time to be used in a if statement and depending on the time for a different output.
right now the script outputs all all of the "good...." echos and does not output a single echo depending on the time.
thank you for the help.
bash
I am currently checking a .sh script just to do some basic things.
chmod +x catbash.sh
echo 'Hello, Please Enter your User Name'
echo
read VarUserName
currenttime=$(date +%H:%M)
if [[ $currenttime > 11:59 ]] || [[ $currenttime < 12:00 ]];
then echo 'Good Morning' $VarUserName'.'
fi
if [[ $currenttime > 12:00 ]] || [[ $currenttime < 16:59 ]];
then echo 'Good Afternoon' $VarUserName'.'
fi
if [[ $currenttime > 17:00 ]] || [[ $currenttime < 19:59 ]];
then echo 'Good Evening' $VarUserName'.'
fi
if [[ $currenttime > 20:00 ]] || [[ $currenttime < 23:59 ]];
then echo 'Good Night' $VarUserName'.'
fi
My issue is that i am trying to use the systems current time to be used in a if statement and depending on the time for a different output.
right now the script outputs all all of the "good...." echos and does not output a single echo depending on the time.
thank you for the help.
bash
bash
edited Mar 26 at 20:26
marc_s
598k135 gold badges1148 silver badges1284 bronze badges
598k135 gold badges1148 silver badges1284 bronze badges
asked Mar 26 at 18:47
dooddood
31 bronze badge
31 bronze badge
2
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needsAND
instead ofOR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use ofelif
so that no more than one technically can print out ..
– Zak
Mar 26 at 18:59
add a comment |
2
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needsAND
instead ofOR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use ofelif
so that no more than one technically can print out ..
– Zak
Mar 26 at 18:59
2
2
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needs
AND
instead of OR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use of elif
so that no more than one technically can print out ..– Zak
Mar 26 at 18:59
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needs
AND
instead of OR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use of elif
so that no more than one technically can print out ..– Zak
Mar 26 at 18:59
add a comment |
2 Answers
2
active
oldest
votes
The problem is with your conditions, all of them permit any given value, did you maybe want to use &&
instead of ||
?
1
Another stylistic improvement would be to useelif
instead of always closing the previousif
and starting a new one.
– tripleee
Mar 26 at 19:03
add a comment |
It would be much simpler if you would write it using the elif
construction and start from the later times and work your way down.
chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night $USER"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening $USER"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon $USER"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning $USER"
else echo "Good Night $USER"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364290%2fproblem-with-using-time-check-with-if-statments-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is with your conditions, all of them permit any given value, did you maybe want to use &&
instead of ||
?
1
Another stylistic improvement would be to useelif
instead of always closing the previousif
and starting a new one.
– tripleee
Mar 26 at 19:03
add a comment |
The problem is with your conditions, all of them permit any given value, did you maybe want to use &&
instead of ||
?
1
Another stylistic improvement would be to useelif
instead of always closing the previousif
and starting a new one.
– tripleee
Mar 26 at 19:03
add a comment |
The problem is with your conditions, all of them permit any given value, did you maybe want to use &&
instead of ||
?
The problem is with your conditions, all of them permit any given value, did you maybe want to use &&
instead of ||
?
answered Mar 26 at 18:55
joshbooksjoshbooks
3491 silver badge7 bronze badges
3491 silver badge7 bronze badges
1
Another stylistic improvement would be to useelif
instead of always closing the previousif
and starting a new one.
– tripleee
Mar 26 at 19:03
add a comment |
1
Another stylistic improvement would be to useelif
instead of always closing the previousif
and starting a new one.
– tripleee
Mar 26 at 19:03
1
1
Another stylistic improvement would be to use
elif
instead of always closing the previous if
and starting a new one.– tripleee
Mar 26 at 19:03
Another stylistic improvement would be to use
elif
instead of always closing the previous if
and starting a new one.– tripleee
Mar 26 at 19:03
add a comment |
It would be much simpler if you would write it using the elif
construction and start from the later times and work your way down.
chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night $USER"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening $USER"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon $USER"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning $USER"
else echo "Good Night $USER"
add a comment |
It would be much simpler if you would write it using the elif
construction and start from the later times and work your way down.
chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night $USER"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening $USER"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon $USER"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning $USER"
else echo "Good Night $USER"
add a comment |
It would be much simpler if you would write it using the elif
construction and start from the later times and work your way down.
chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night $USER"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening $USER"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon $USER"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning $USER"
else echo "Good Night $USER"
It would be much simpler if you would write it using the elif
construction and start from the later times and work your way down.
chmod +x catbash.sh
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "19:59" ]]; then echo "Good Night $USER"
elif [[ "$currenttime" > "16:59" ]]; then echo "Good Evening $USER"
elif [[ "$currenttime" > "11:59" ]]; then echo "Good Afternoon $USER"
elif [[ "$currenttime" > "05:59" ]]; then echo "Good Morning $USER"
else echo "Good Night $USER"
answered Mar 26 at 22:44
kvantourkvantour
12.3k4 gold badges19 silver badges35 bronze badges
12.3k4 gold badges19 silver badges35 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55364290%2fproblem-with-using-time-check-with-if-statments-bash%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
m3t5W Lo3rDA2zl,BGf2tqZh
2
@el-teedee this is NOT a parsing issue .. This is straight up logic -- He needs
AND
instead ofOR
.. All if statement are being executed and are technically true .. So they all echo .. Another way to make this more correct would be the use ofelif
so that no more than one technically can print out ..– Zak
Mar 26 at 18:59