LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2019, 07:00 AM   #1
DoME69
Member
 
Registered: Jan 2008
Posts: 181

Rep: Reputation: 16
Check if time legal in tcsh....


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.


Quote:
if (`/bin/date -d "$arg" >& /dev/null` == "") then
echo "\nError: Time $argv[1] not legal time"
exit
endif
 
Old 10-07-2019, 07:33 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,944

Rep: Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810
Quote:
Originally Posted by DoME69 View Post
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.

Last edited by TB0ne; 10-07-2019 at 07:51 AM.
 
Old 10-07-2019, 07:53 AM   #3
DoME69
Member
 
Registered: Jan 2008
Posts: 181

Original Poster
Rep: Reputation: 16
Sorry,

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
 
Old 10-07-2019, 08:00 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,944

Rep: Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810
Quote:
Originally Posted by DoME69 View Post
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???
 
Old 10-07-2019, 08:02 AM   #5
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,652

Rep: Reputation: Disabled
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.)
 
2 members found this post helpful.
Old 10-07-2019, 09:41 AM   #6
DoME69
Member
 
Registered: Jan 2008
Posts: 181

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Ser Olmy View Post
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
Code:
/bin/date: invalid date `25:00'
 
Old 10-07-2019, 09:51 AM   #7
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,652

Rep: Reputation: Disabled
Quote:
Originally Posted by DoME69 View Post
While running this if I get error when trying to use 25:00
Code:
/bin/date: invalid date `25:00'
Seems there's an issue with using redirection inside an if ( { } ) statement.

It does work if you run the command on its own, so just do that and check $status afterwards.
 
Old 10-07-2019, 09:57 AM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,944

Rep: Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810
Quote:
Originally Posted by DoME69 View Post
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.
 
Old 10-07-2019, 10:04 AM   #9
DoME69
Member
 
Registered: Jan 2008
Posts: 181

Original Poster
Rep: Reputation: 16
I wrote it in tcsh as mention in the subject.
can you guide me how to do it in tcsh?
 
Old 10-07-2019, 10:13 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Code:
#!/bin/tcsh

set number=$argv[1]
echo $number
if ( $number =~ [0-2][0-3]:[0-5][0-9] ) then
  echo "time is valid"
else
  echo "not valid"
endif
Lots of ways, it just depends on what you are actually trying to accomplish. In this example hours must be a two digit value.

Last edited by michaelk; 10-07-2019 at 10:16 AM.
 
Old 10-07-2019, 10:18 AM   #11
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,652

Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
Code:
#!/bin/tcsh

set number=$argv[1]
echo $number
if ( $number =~ [0-2][0-3]:[0-5][0-9] ) then
  echo "time is valid"
else
  echo "not valid"
endif
Not quite:
Code:
~$ ./test 12:00
12:00
time is valid
~$ ./test 23:00
23:00
time is valid
~$ ./test 14:00
not valid
~$
 
Old 10-07-2019, 10:32 AM   #12
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,944

Rep: Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810
Quote:
Originally Posted by DoME69 View Post
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'.
 
Old 10-07-2019, 10:39 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Quote:
Originally Posted by Ser Olmy View Post
Not quite:
Code:
~$ ./test 12:00
12:00
time is valid
~$ ./test 23:00
23:00
time is valid
~$ ./test 14:00
not valid
~$
Ouch, nevermind that method...
 
1 members found this post helpful.
Old 10-07-2019, 10:44 AM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,944

Rep: Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810Reputation: 5810
Quote:
Originally Posted by michaelk View Post
Ouch, nevermind that method...
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.

Last edited by TB0ne; 10-07-2019 at 10:49 AM.
 
Old 10-07-2019, 10:49 AM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,028

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Quote:
Originally Posted by TB0ne View Post
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.
True, although your example is probably easier to understand...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Patents, Legal Collaboration and our Legal Summit LXer Syndicated Linux News 0 03-15-2012 07:11 PM
LXer: Patents, Legal Collaboration and our Legal Summit LXer Syndicated Linux News 0 03-15-2012 08:50 AM
tcsh script: can you time parts of the script? BrianK Programming 7 02-02-2010 07:20 PM
Partition check, check double check Vincentius Linux - General 0 12-25-2004 05:47 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration