- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
That is because #2 is wrong way to do it. "javascript:" is used in href attribute, because its default type is URL, not javascript statement.
Admin
Alex,
Would it be possible to change the server software to something less retarded than this "community server".
Admin
Admin
The KSH example is pretty standart for shell scripts, especially if you don't spend all day writing them.
From an older expr(1) manual page (2002)
Admin
What exactly is so difficult about strcmp? There is a perfectly good reason for it to return the values that it does, even if its not used by 90% of the developers out there.
Its ANSI definition states that it returns the lexicographical relation between two strings in the form of an integer. It doesn't return random values above or below zero - although I guess it could seem that way sometimes.Its fiendishly simple to remember when you've read and understood the docs for strcmp, the return value being the lexicographical *difference* between the two strings. http://www.cplusplus.com/ref/cstring/strcmp.html if the strings arent different then 0 is returned.
The real WTF is still
#define false 1
#define true 0
Maybe there should be a site entitled www.thedailyevilgenius.com >:->
Admin
I tested it on my linux box, expr is 60x slower than $(()) or let. But in many cases, it doesn't matter.
Admin
Hmpf, I used to type if (!strcmp(str1, str2)) { } to check for same-ness of strings!
Admin
So did I. Anyway, if I cared that much about the readability of string comparison, I'd rather define
than this idiotic true=0 stuff
Admin
some shells also support "let", not sure if ksh does.
like: let foo+=1; or let foo++;
Admin
Probably because your are using an avanced shell. sh under Sun Solaris refuses this syntax. ksh and bash have no issues with it though.
In plain sh, "bc" and "dc" can be used as alternatives --external programs-- to "expr".
Admin
Of course /usr/xpg4/bin/sh has no troubles with $(())
Admin
Hmmm... Frankly I'm a bit disappointed with this WTF, but perhaps I've gotten a bit blunted from previous posts (WTFs AND replies...). Hope to see something REALLY WTF-ish later today (or in the morning perhaps) :)
Admin
Holy shit! You don't like the forum software?!
Well, since no one else has complained about it, why should Alex just change it just for you? Anyway if he did change it, it would cut the post count in half.
Admin
This is the definition of justifiable homicide.
Basically it's having to spawn a whole new process just to add 1 to SLEEP_COUNT, as opposed to just typing
or
which has the virtue of not incurring a context switch...
Admin
This is obviously security thru obscurity, he figured that nobody will ever figure out the 'real' password is, since he is 'transforming' it in the program. DOH!
</sarcasm>
Admin
Sure. If you really want to do that:
data Ternary = True | False | FileNotFound
But the FileNotFound thingie really should be more like an exception that an actual usefull return value. So you would abstract that shit away seperately:
data CouldThrowAnException a = WentOk a | WentWrong String
And the Boolean & Exception together would just be:
type MyFileOperation = CouldThrowAnException Bool
The function that should return your ternary state could read like:
return (WentOk theFileIsPornFlag)
Or, if it fails:
return (WentWrong "File Not Found. So the answer is 7")
Excuse the extreme names, but in examples like these they put some context to what we're doing. In Haskell, all these types are off course already standard, but called a bit different:
data Either a b = Left a | Right b
data Bool = True | False
So then our type would just be:
type MyFileOperation = Either Bool String
.. and we would create constructor functions ourselves:
wentOk :: Bool -> MyFileOperation
wentOk b = Left b
wentWrong :: String -> MyFileOperation
wentWrong errMsg = Right errMsg
.. which you could also also define as function aliases:
wentOk = Left
wentWrong = Right
Much cleaner than 'ternary' logic, if you ask me. And good usage of type and function aliases prevents the need for most low-level comments. Oh, and it makes any error message during compilation much much more readable.
"a FileOperation was expected here, but a DbOperator was given"
is much more readable than:
"a (Either Bool String) was expected here, but a (Either Query String) was given"
Hmm .. im getting off topic here. sorry. I suddenly recall that Ternary logic was actually a featured wtf...
Admin
The "SLEEP_COUNT=`expr $SLEEP_COUNT + 1`" is fairly normal Unixese, if you want to guarantee that the script will run on any Unix since the early 1980's. OTOH, if that were true, then neither "#!/bin/ksh" nor "export SLEEP_COUNT=..." would work.
Back then, you needed to run an external program to do math in a script, but you needed to run an external program to do everything except simple loops and if statements. Some old-timers might argue that even the simple loops and if statements should have been done with external programs too, but that argument has long since been lost.
Modern Unix-like systems (OK, really we're talking about Linux now) build the "expr" command into the shell itself, so that old code runs without actually spawning a separate program. Then you have to worry if your /usr/bin/expr is compatible with what you get when you just run 'expr' from the shell...
/bin/ksh is not a standard shell, so it's not portable (every Unix capable of understanding "#!" has a /bin/sh, but not necessarily anything else). There was once a time when "#!" wasn't supported either, back in the days when there was only one Unix shell.
Old Bourne shells need two separate commands to define a variable and export it. They can't combine the two into one command as above, you must write it like this:
SLEEP_COUNT=something
export SLEEP_COUNT
The thing that worries me is that in order for an exported SLEEP_COUNT variable to be useful, it would have to recursively call itself, since environment variables in Unix only affect the current process and its children--it's not like DOS/Windows where setting an environment variable affects every (or at least many) process(es) on the system. I suspect the "unimportant code" is in fact very important after all.
Admin
I don't know what world you live in, but just about every time I read through one of these I find at least 1, and often more people complaining about the forum software. You'll also see malformed posts (with <'s and >'s) all over the place because of the lack of preview ability in favor of this horrid thing they call WYSIWYG.
The post today about the messed up javascript link was particularly funny because it looks like exactly something I'd see on these forums. For example the smilies on the left just give me a javascript error and are completely useless. If I try to post, about have the time this text box screws up and dies on me (giving javascript errors all over the place)
On top of that there's there the smilies which look like they were done in MS Paint by the Teletubies ( see the post icon, because the simily thing on the left is broken ).
Admin
oh, wait I didn't read John's post properly.
Anyways, this forum is just one of the many reasons ASP.Net should never have existed.
Admin
"Web monkeys", LOL! That's why "web designers" and "web programmers" are swear-words over here in Germany.
Yeah, this isn't a WTF after all. It's like that web site saying "Don't visit me never ever again!". Developer, here's your sign (http://www.mistupid.com/people/page013.htm)!
Torsten
Admin
"whose", damn you, "whose".
Admin
"Whose," damn you, "whose."
Admin
Bzzzt: "Whose", damn you, "whose".
The , and . are punctuation for the whole sentence so are outside the quotation marks.
Admin
You're not using design patterns! You should replace your new Person() line with something more like this:
new PersonFactory(Locale.getDefault()).createPerson(PersonConstants.FIRST_PERSON_SUBJECTIVE);
Admin
In British English, yes.
In formal American English (whatever that is) the comma or period ALWAYS goes inside the quotes.
But the British English method is gaining popularity in the US.
http://en.wikipedia.org/wiki/Quotation_mark
(in the Typographical Considerations section)
Admin
Not under Solaris! This is a POSIX thing.
That is probably correct, but how do you know which is installed, and where it is located?
The former problem cannot really be solved runtime in the same script, but it can be solved by a wrapper script firing off the correct script.
The latter problem can be solved by our friend /usr/bin/env, which also happens to be installed on most modern Unixes -- even Solaris.
So, assuming that you have two script bases; one that's POSIX compatible and can run under both ksh and bash, and one that's Solaris sh compatible, you can do something like this in the wrapper script:
HTH, HAND.
Admin
IANAL, butt it seems a bit silly to explicity use ksh when writing a generic shell script.
BTW, I'm having some problems with a script of my own:
<FONT style="BACKGROUND-COLOR: #ffffff" face="Courier New" color=#000000 size=2>/home/sidiot/My\ Programs/$ cat WTF.sh
#!/bin/sh
SLEEP_COUNT=0
while [[ $1 != $SLEEP_COUNT ]]
do
wait 1
export SLEEP_COUNT=`expr $SLEEP_COUNT + 1`
done
exit 0
</FONT><FONT face="Courier New" size=2><FONT face="Courier New" size=2><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>/home/sidiot/My\ Programs/$ alias wait=sleep
</FONT><FONT face="Courier New" size=2><FONT face="Courier New" size=2><FONT style="BACKGROUND-COLOR: #ffffff" color=#000000>/home/sidiot/My\ Programs/$ chmod +x WTF.sh
</FONT><FONT face="Courier New" size=2><FONT style="BACKGROUND-COLOR: #ffffff" face="Courier New" color=#000000 size=2>/home/sidiot/My\ Programs/$ alias sleep="echo $( ./WTF.sh ) > /dev/null"
./WTF.sh: line 5: wait: pid 1 is not a child of this shell
./WTF.sh: line 5: wait: pid 1 is not a child of this shell
./WTF.sh: line 5: wait: pid 1 is not a child of this shell
./WTF.sh: line 5: wait: pid 1 is not a child of this shell
./WTF...</FONT></FONT></FONT></FONT></FONT></FONT>
Admin
1) First off, I don't think most Linux distributions symlink /bin to /usr/bin. So you may be technically correct, but why intentionally break it on other *nix systems, just to gain 1 filesystem traversal? Actually spawning the shell is FAR more intensive that finding it anyway, so I highly doubt you save anything noticable. (Note: On the FC4 box I'm on now, /bin/sh is a symlink to /bin/bash, but /bin is NOT a symlink to /usr/bin (or the reverse)... most things in /bin are not symlinks, and if they are, it's only to other things in /bin. /usr/bin/sh and /usr/bin/bash don't exist at all. ksh is in /bin. My point is, filesystem locations are not always a given.)
2) Agree completely. Backticks are the bane of debugging. If your system doesn't support $( ), you need a new system.
3) The $(( )) math syntax exists in bash, too. Dunno how good it is in ksh, but in bash it's somewhat limited. Works for ++-type operations, but not for floating-point stuff. If you need more than $(( )) supports, the normal solution is to invoke bc. From your description, I'd guess ksh behaves similarly.
4) Agreed- perhaps this script is actually sourced, rather than run? In which case, export affects the CURRENT shell, not just child process too.
IIRC, the reason for relying on /bin/sh and not something more advanced (like ksh, bash, or perl), is that it should work without dependencies (the shell itself... obviously the script might have its own dependencies). Thus, it exists in /bin, not in /usr/bin... because /usr might not be mounted. Furthermore, IIRC it's position is guaranteed- it always exists as /bin/sh. Where is ksh? Where is bash? Depends on the system... sometimes /bin, sometimes /usr/bin, sometimes /usr/local/bin, sometimes /opt... Finding it isn't aways easy, either... whence? which? env? They won't always give the same answer... and where are THOSE programs anyway? /bin? /usr/bin? Builtin functions?
Also, porting: ksh -> bash (or vice versa) is harder than sh -> bash/ksh. If you have a ksh script, you would have to spend some time getting it to run under something else. Sticking to a subset allows for more choices in the case that you later need more functionality. (This of course assumes you don't yet need the extra functionality. If you already do, well...). I mention this only because I've worked on this before... things like syntax and builtin functions are sometimes less obvious than one would think.
I HAVE seen systems that either don't have ksh/bash/csh, or have them in odd, unexpected places. Sometimes it's just that the admin never installed them, or did it by hand rather than a supplied package. The point is that one of them almost certainly exists, but I can't guarantee WHICH one, or WHERE it is. So if portability matters, it pretty much has to be /bin/sh.
Lastly, I can be pretty sure my successor will understand plain sh scripts. But maybe he won't immediately understand ksh ones because he always uses bash, or vice versa.
Thus ends my treatise on why one should use /bin/sh and not ksh or bash without good reason.
Admin
Hence why I said Unix vs Unixlike - Linux != Unix - it is a clone of the OS, following an old file system standard which most flavours of Unix have long abandoned (e.g. /usr being used for applications instead of /opt, having a lot of superfluous directories in /).
If you're following a standard (e.g. the SYSV standards or LSB) then you know where your executable is. If some systems break the standard, then they're wrong and should be fixed (look at Windows :-).
ksh88 only supports integers, so this won't make much difference. I don't know about ksh93.
Feh, /bin is not needed - /sbin on Unix is intended for anything less than single user mode (anything higher, you know that /usr will be mounted).
Again, the whole point of standards (our lovely POSIX standard). Fortunately bash nearly follows the standards, the only problem I've found 'twixt bash and ksh is due to the different ways that they handle scope. (Also bash's vi command line edit mode is very buggy.)
One phrase: man ksh. sh/ksh/bash share a common pedigree, so the differences are slight, and good coding practice can minimise these.
Using bourne shell is just crippling - why restrict yourself to bread when you can have venison? It's like insisting on using Windows 3.1 because you don't understand Windows 2003.
I wouldn't get so wound up about it except for the times involve, bourne shell is older than me, korn shell was written in 1988 and updated in 1993, bash has been around since the early 90's. So, why are we still insisting on using something that is over 30 years old? This is one thing that Linux got right - use of bourne shell is treated as legacy and should be avoided.
Admin
Better still:
, but this is arguably not as portable as the 'expr' version.Admin
The way strcmp() is meant to be used is to always compare its result explicitly against 0, i.e.
strcmp(a, b) == 0 strcmp(a, b) < 0 strcmp(a, b) >= 0
That way, when you see 'strcmp() op 0', just think 'a op b'...
Those who haven't got it at this point never will.