ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I want to verify that time is legal.
For example 23:00 = legal
25:00 = not legal.
When running the below code, of course I don't want to get the error message when time is not legal. I tried to use the below but need you help since it's failed.
Code:
if (`/bin/date -d "$arg" >& /dev/null` == "") then
echo "\nError: Time $argv[1] not legal time"
exit
endif
You've been a member here for ELEVEN YEARS, and have been asking about scripting for many years; you were also asked to use CODE tags and not QUOTE tags on scripts as well. And saying "since it's failed", tells us nothing. What is the message you're getting, and where is the rest of the script?
You're missing a backslash to close a double-quote, you have $arg AND $argv[1] in use. Show us the rest of the script, and give us a real error message. Also, if you really want to validate a user-input time ONLY (that is, just "18:44" or "23:58", vs. "2019-09-29:06:00:00"), you need to split both parts apart and validate them.
I want to add this section to long script and it independence section from the rest of the script.
While running the below code I always get error message: "/bin/date: invalid date `25:00'"
I tried to send it to /dev/null but the variable don't set with that option.
Code:
set arg = $argv[1]
set check_time_exist = `/bin/date -d $arg`
if ( "$check_time_exist" == "" ) then
echo "\nError: Time $argv[1] not legal time"
exit
endif
Sorry,
I want to add this section to long script and it independence section from the rest of the script.
Then you need to post it when it can run by itself, stand-alone. We can't guess as to the rest of your script, what it feeds in/out, etc., and based on what you posted, it won't work AT ALL.
Quote:
While running the below code I always get error message: "/bin/date: invalid date `25:00'" I tried to send it to /dev/null but the variable don't set with that option.
Code:
set arg = $argv[1]
set check_time_exist = `/bin/date -d $arg`
if ( "$check_time_exist" == "" ) then
echo "\nError: Time $argv[1] not legal time"
exit
endif
Again, what you posted won't work on its own, and doesn't show us how the FIRST piece of code ties in to this one. And what shell are you writing this in? Bash? KSH? CSH???
I'm not a tsch/csh user, but a quick Google search for "tsch if statement" quickly revealed the correct syntax:
Code:
if ! ( { /bin/date -d "$argv[1]" >& /dev/null } ) then
echo fatal: $argv[1]: invalid time
exit
endif
Here, the if statement acts on the exit code of date.
You could also simply run date -d first, and then check the $status variable (similar to $? in bash) for the exit code.
(BTW, this isn't really a good way to check whether a string contains a valid time, since date -d will happily accept strings containing a combination of dates and time.)
I'm not a tsch/csh user, but a quick Google search for "tsch if statement" quickly revealed the correct syntax:
Code:
if ! ( { /bin/date -d "$argv[1]" >& /dev/null } ) then
echo fatal: $argv[1]: invalid time
exit
endif
Here, the if statement acts on the exit code of date.
You could also simply run date -d first, and then check the $status variable (similar to $? in bash) for the exit code.
(BTW, this isn't really a good way to check whether a string contains a valid time, since date -d will happily accept strings containing a combination of dates and time.)
Thanks for your help...
While running this if I get error when trying to use 25:00
Thanks for your help...
While running this if I get error when trying to use 25:00
Code:
/bin/date: invalid date `25:00'
For bash:
Code:
#!/bin/bash
TIME="$1"
if
[[ $TIME != "" ]] && \
[ ${TIME%:*} -le 23 -a ${TIME%:*} -ge 0 -a \
${TIME#*:} -le 59 -a ${TIME#*:} -ge 0 ]; then
echo ok
else
echo NOT ok
fi
Validates HH:MM time using regex. Can't have more than 23 for the first two digits, and more than 59 for the second two. Should be easy to follow.
I wrote it in tcsh as mention in the subject. can you guide me how to do it in tcsh?
We are trying to; again, you have been here for ELEVEN YEARS, and have been asking about such things for at least five. Certainly after all this time a simple if/then shouldn't be hard.
The example given in bash, can be easily modified using what michaelk provided, and be used for what you're after. His example should easily be enough to 'guide you'.
Well, that is a 100% valid *EXAMPLE* of how to do this, which should 'guide' the OP. Only needs to modify the test-parameter regex. That's why I split my regex, and test the first two digits separately from the second. Anything less than 23 for the first two is valid...less than 59 for the second is valid. Either test fails, and you have a bad time.
Should be able to split based on the colon, break it into HRS and MIN variables, and do a simple less-than to test.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.