bash script that reads user input and uses “cal” command to validate dates
Clash Royale CLAN TAG#URR8PPP
I want to write a script that reads my input (for example if my script is called "check", then I would type "check 9 4 1993" and that input would go through the cal command and will check through the calendar whether it is a valid date or not).
My idea below is that if the input that goes through the cal command gives an error it will mean that it's not a valid date, and vice-versa if there's no error than the date is valid. I do realize there's something terribly wrong with this draft (I can't figure out how to make it so that my input will go through the cal command), but I will appreciate some suggestions. Here's the draft anyways:
#!/bin/bash
day=$1; month=$2; year=$3
day=$(echo "$day" | bc)
month=$(echo "$month" | bc)
year=$(echo "$year" | bc)
cal $day $ month $year 2> /dev/null
if [[$? -eq 0 ]]; then
echo "This is a valid date"
else
echo "This is an invalid date"
fi
bash shell-script scripting cal
add a comment |
I want to write a script that reads my input (for example if my script is called "check", then I would type "check 9 4 1993" and that input would go through the cal command and will check through the calendar whether it is a valid date or not).
My idea below is that if the input that goes through the cal command gives an error it will mean that it's not a valid date, and vice-versa if there's no error than the date is valid. I do realize there's something terribly wrong with this draft (I can't figure out how to make it so that my input will go through the cal command), but I will appreciate some suggestions. Here's the draft anyways:
#!/bin/bash
day=$1; month=$2; year=$3
day=$(echo "$day" | bc)
month=$(echo "$month" | bc)
year=$(echo "$year" | bc)
cal $day $ month $year 2> /dev/null
if [[$? -eq 0 ]]; then
echo "This is a valid date"
else
echo "This is an invalid date"
fi
bash shell-script scripting cal
1
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
What is the purpose behindday=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also doday=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking abc
process.
– Digital Trauma
Oct 8 '14 at 16:28
1
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33
add a comment |
I want to write a script that reads my input (for example if my script is called "check", then I would type "check 9 4 1993" and that input would go through the cal command and will check through the calendar whether it is a valid date or not).
My idea below is that if the input that goes through the cal command gives an error it will mean that it's not a valid date, and vice-versa if there's no error than the date is valid. I do realize there's something terribly wrong with this draft (I can't figure out how to make it so that my input will go through the cal command), but I will appreciate some suggestions. Here's the draft anyways:
#!/bin/bash
day=$1; month=$2; year=$3
day=$(echo "$day" | bc)
month=$(echo "$month" | bc)
year=$(echo "$year" | bc)
cal $day $ month $year 2> /dev/null
if [[$? -eq 0 ]]; then
echo "This is a valid date"
else
echo "This is an invalid date"
fi
bash shell-script scripting cal
I want to write a script that reads my input (for example if my script is called "check", then I would type "check 9 4 1993" and that input would go through the cal command and will check through the calendar whether it is a valid date or not).
My idea below is that if the input that goes through the cal command gives an error it will mean that it's not a valid date, and vice-versa if there's no error than the date is valid. I do realize there's something terribly wrong with this draft (I can't figure out how to make it so that my input will go through the cal command), but I will appreciate some suggestions. Here's the draft anyways:
#!/bin/bash
day=$1; month=$2; year=$3
day=$(echo "$day" | bc)
month=$(echo "$month" | bc)
year=$(echo "$year" | bc)
cal $day $ month $year 2> /dev/null
if [[$? -eq 0 ]]; then
echo "This is a valid date"
else
echo "This is an invalid date"
fi
bash shell-script scripting cal
bash shell-script scripting cal
edited Jan 13 at 21:51
Rui F Ribeiro
39.7k1479132
39.7k1479132
asked Oct 8 '14 at 15:32
grinkegrinke
163
163
1
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
What is the purpose behindday=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also doday=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking abc
process.
– Digital Trauma
Oct 8 '14 at 16:28
1
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33
add a comment |
1
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
What is the purpose behindday=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also doday=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking abc
process.
– Digital Trauma
Oct 8 '14 at 16:28
1
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33
1
1
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
What is the purpose behind
day=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also do day=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking a bc
process.– Digital Trauma
Oct 8 '14 at 16:28
What is the purpose behind
day=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also do day=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking a bc
process.– Digital Trauma
Oct 8 '14 at 16:28
1
1
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33
add a comment |
3 Answers
3
active
oldest
votes
In some Linux distros that I have checked (e,g, Ubuntu 14.04), the packaged cal
comes from BSD and not GNU Coreutils. The BSD version does not seem to accept days as a parameter; only months and years. The Ubuntu version does have a -H YYYY-MM-DD option, but that doesn't seem to help.
Instead I would use the date
utility. Assuming the GNU Coreutils under Linux I think I would rewrite your script something like:
#!/bin/bash
day=$((10#$1))
month=$((10#$2))
year=$((10#$3))
if date -d $year-$month-$day > /dev/null 2>&1; then
# cal $month $year
echo "This is a valid date"
else
echo "This is an invalid date"
fi
Notes:
- I am using the shell's own arithmetic expansion to validate input/remove leading zeros, instead of forking a new
bc
process for each parameter - I am using GNU date to parse the input date
- The date command may be used directly as the
if
conditional expression.if
works by checking process exit codes. Commonly the[
or[[
executables are used instead, but there is no reason other programs can be used if they exit with useful exit codes - I'm not sure if you actually wanted the
cal
output for correct dates or not. If you do, simply uncomment thecal
line.
add a comment |
[[
is actually a command, and like other commands you need whitespace to separate its arguments:
if [[ $? -eq 0 ]]; then
# ...^
add a comment |
There is a space between your dollar $
and your month
variable:
cal $day $ month $year 2> /dev/null
It should be:
cal $day $month $year 2> /dev/null
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f160064%2fbash-script-that-reads-user-input-and-uses-cal-command-to-validate-dates%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In some Linux distros that I have checked (e,g, Ubuntu 14.04), the packaged cal
comes from BSD and not GNU Coreutils. The BSD version does not seem to accept days as a parameter; only months and years. The Ubuntu version does have a -H YYYY-MM-DD option, but that doesn't seem to help.
Instead I would use the date
utility. Assuming the GNU Coreutils under Linux I think I would rewrite your script something like:
#!/bin/bash
day=$((10#$1))
month=$((10#$2))
year=$((10#$3))
if date -d $year-$month-$day > /dev/null 2>&1; then
# cal $month $year
echo "This is a valid date"
else
echo "This is an invalid date"
fi
Notes:
- I am using the shell's own arithmetic expansion to validate input/remove leading zeros, instead of forking a new
bc
process for each parameter - I am using GNU date to parse the input date
- The date command may be used directly as the
if
conditional expression.if
works by checking process exit codes. Commonly the[
or[[
executables are used instead, but there is no reason other programs can be used if they exit with useful exit codes - I'm not sure if you actually wanted the
cal
output for correct dates or not. If you do, simply uncomment thecal
line.
add a comment |
In some Linux distros that I have checked (e,g, Ubuntu 14.04), the packaged cal
comes from BSD and not GNU Coreutils. The BSD version does not seem to accept days as a parameter; only months and years. The Ubuntu version does have a -H YYYY-MM-DD option, but that doesn't seem to help.
Instead I would use the date
utility. Assuming the GNU Coreutils under Linux I think I would rewrite your script something like:
#!/bin/bash
day=$((10#$1))
month=$((10#$2))
year=$((10#$3))
if date -d $year-$month-$day > /dev/null 2>&1; then
# cal $month $year
echo "This is a valid date"
else
echo "This is an invalid date"
fi
Notes:
- I am using the shell's own arithmetic expansion to validate input/remove leading zeros, instead of forking a new
bc
process for each parameter - I am using GNU date to parse the input date
- The date command may be used directly as the
if
conditional expression.if
works by checking process exit codes. Commonly the[
or[[
executables are used instead, but there is no reason other programs can be used if they exit with useful exit codes - I'm not sure if you actually wanted the
cal
output for correct dates or not. If you do, simply uncomment thecal
line.
add a comment |
In some Linux distros that I have checked (e,g, Ubuntu 14.04), the packaged cal
comes from BSD and not GNU Coreutils. The BSD version does not seem to accept days as a parameter; only months and years. The Ubuntu version does have a -H YYYY-MM-DD option, but that doesn't seem to help.
Instead I would use the date
utility. Assuming the GNU Coreutils under Linux I think I would rewrite your script something like:
#!/bin/bash
day=$((10#$1))
month=$((10#$2))
year=$((10#$3))
if date -d $year-$month-$day > /dev/null 2>&1; then
# cal $month $year
echo "This is a valid date"
else
echo "This is an invalid date"
fi
Notes:
- I am using the shell's own arithmetic expansion to validate input/remove leading zeros, instead of forking a new
bc
process for each parameter - I am using GNU date to parse the input date
- The date command may be used directly as the
if
conditional expression.if
works by checking process exit codes. Commonly the[
or[[
executables are used instead, but there is no reason other programs can be used if they exit with useful exit codes - I'm not sure if you actually wanted the
cal
output for correct dates or not. If you do, simply uncomment thecal
line.
In some Linux distros that I have checked (e,g, Ubuntu 14.04), the packaged cal
comes from BSD and not GNU Coreutils. The BSD version does not seem to accept days as a parameter; only months and years. The Ubuntu version does have a -H YYYY-MM-DD option, but that doesn't seem to help.
Instead I would use the date
utility. Assuming the GNU Coreutils under Linux I think I would rewrite your script something like:
#!/bin/bash
day=$((10#$1))
month=$((10#$2))
year=$((10#$3))
if date -d $year-$month-$day > /dev/null 2>&1; then
# cal $month $year
echo "This is a valid date"
else
echo "This is an invalid date"
fi
Notes:
- I am using the shell's own arithmetic expansion to validate input/remove leading zeros, instead of forking a new
bc
process for each parameter - I am using GNU date to parse the input date
- The date command may be used directly as the
if
conditional expression.if
works by checking process exit codes. Commonly the[
or[[
executables are used instead, but there is no reason other programs can be used if they exit with useful exit codes - I'm not sure if you actually wanted the
cal
output for correct dates or not. If you do, simply uncomment thecal
line.
answered Oct 8 '14 at 17:24
Digital TraumaDigital Trauma
5,87211528
5,87211528
add a comment |
add a comment |
[[
is actually a command, and like other commands you need whitespace to separate its arguments:
if [[ $? -eq 0 ]]; then
# ...^
add a comment |
[[
is actually a command, and like other commands you need whitespace to separate its arguments:
if [[ $? -eq 0 ]]; then
# ...^
add a comment |
[[
is actually a command, and like other commands you need whitespace to separate its arguments:
if [[ $? -eq 0 ]]; then
# ...^
[[
is actually a command, and like other commands you need whitespace to separate its arguments:
if [[ $? -eq 0 ]]; then
# ...^
answered Oct 8 '14 at 15:45
glenn jackmanglenn jackman
51.2k571110
51.2k571110
add a comment |
add a comment |
There is a space between your dollar $
and your month
variable:
cal $day $ month $year 2> /dev/null
It should be:
cal $day $month $year 2> /dev/null
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
add a comment |
There is a space between your dollar $
and your month
variable:
cal $day $ month $year 2> /dev/null
It should be:
cal $day $month $year 2> /dev/null
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
add a comment |
There is a space between your dollar $
and your month
variable:
cal $day $ month $year 2> /dev/null
It should be:
cal $day $month $year 2> /dev/null
There is a space between your dollar $
and your month
variable:
cal $day $ month $year 2> /dev/null
It should be:
cal $day $month $year 2> /dev/null
edited Oct 9 '14 at 7:55
answered Oct 8 '14 at 15:43
geedoubleyageedoubleya
3,0431118
3,0431118
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
add a comment |
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
Damn, what a rookie mistake by me, thanks for noticing :) The script works as intended now, i have the right output.
– grinke
Oct 8 '14 at 17:16
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f160064%2fbash-script-that-reads-user-input-and-uses-cal-command-to-validate-dates%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
1
similar question, not dup: just wanted to know what am i doing wrong in the end of this script that doesnt put my checkdate in to cal command
– glenn jackman
Oct 8 '14 at 15:48
What is the purpose behind
day=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also doday=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking abc
process.– Digital Trauma
Oct 8 '14 at 16:28
1
Almost exactly the same problem as in bash question about if and then; and very similar to how to write this if command?
– G-Man
Oct 8 '14 at 16:33