zsh-users
 help / color / mirror / code / Atom feed
* Style question: Can this be written in a more elegant way?
@ 2006-04-24  7:26 Com MN PG P E B Consultant 3
  2006-04-24  8:27 ` Marc Chantreux
  0 siblings, 1 reply; 10+ messages in thread
From: Com MN PG P E B Consultant 3 @ 2006-04-24  7:26 UTC (permalink / raw)
  To: zsh-users Mailinglist

Problem: In a directory, perform a certain operation PROG_SUCCESS on the
newest file
matching the glob pattern X*. If no file exists with this glob pattern,
execute PROG_FAIL.

Here is my solution:

file=$(ls -1t X* 2>/dev/null|head -n 1)
if [[ ${#file} -gt 0 ]]
then
  PROG_SUCCESS $file
else
  PROG_FAIL
fi

This certainly works, and I think it's not that bad either. Just
wondering whether
this can be written less clumsy. Any suggestions?

Ronald
-- 
Ronald Fischer (phone +49-89-63676431)
mailto:mn-pg-p-e-b-consultant-3.com@siemens.com


^ permalink raw reply	[flat|nested] 10+ messages in thread
* RE: Style question: Can this be written in a more elegant way?
@ 2006-04-25  6:37 Com MN PG P E B Consultant 3
  2006-04-25  8:01 ` Marc Chantreux
  0 siblings, 1 reply; 10+ messages in thread
From: Com MN PG P E B Consultant 3 @ 2006-04-25  6:37 UTC (permalink / raw)
  To: Bart Schaefer, zsh-users

> I think you mean:
> 
>     file=( X*(Nom[1]) )
>     (( $+file )) && PROG_SUCCESS $file || PROG_FAIL
> 
> However, that will run PROG_FAIL if PROG_SUCCESS fails, as well as if
> there are no matching files.  I don't think you can avoid if/else.
> 
>     if (( $+file )); then PROG_SUCCESS $file; else PROG_FAIL; fi
> 
> Note that in the event that there are two or more files with the same
> time stamp, there's no guarantee which one of them is chosen, and in
> fact X*(om[1]) and X*(Om[1]) might both choose the same file.

Thank you. This ambiguity would not be a problem in my case. Just out of
curiosity:

[OT] How exact are time stamps on files? If, basically, a time_t value
is choosen
for a time stamp, then in practice the only possibility for two files to
come
up with the same time stamp, is when the time stamp is explicitly
supplied
via "touch", isn't it?

Ronald
-- 
Ronald Fischer (phone +49-89-63676431)
mailto:mn-pg-p-e-b-consultant-3.com@siemens.com


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2006-04-25 17:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-24  7:26 Style question: Can this be written in a more elegant way? Com MN PG P E B Consultant 3
2006-04-24  8:27 ` Marc Chantreux
2006-04-24 15:21   ` Bart Schaefer
2006-04-25  7:55     ` Marc Chantreux
2006-04-25 14:41       ` Bart Schaefer
2006-04-25 14:58         ` Marc Chantreux
2006-04-25 17:03           ` Bart Schaefer
2006-04-25  6:37 Com MN PG P E B Consultant 3
2006-04-25  8:01 ` Marc Chantreux
2006-04-25 15:06   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).