Java Scanner Linux test run: dollar sign command line arguments
The tutor suggests an explanation for the surprise during the Linux test run.
In my previous article I reported a surprising finding of how my Java Scanner program reads dollar sign input from the bash command line.
Pursuant to that issue, I’ll show another command line call and its result:
java TryScanner $0.45 $125.27 $$1.28 \$56.39
The result:
Not a readable number: bash.45
Readable number: 25.27
Readable number:22821.28
Not a readable number: $56.39
(Possible) explanation of first result:
In bash, $0 is the command to tell what shell you are using (ie, bash). Bash substitutes bash for $0 in $0.45, giving bash.45. TryScanner then reads bash.45 and deems it not a readable number, since it contains non-numeric characters.
(Possible) explanation of second result:
$1, in bash, means the first command line argument given to a bash script. Since java TryScanner isn’t a bash script, $1 points to a blank value. Therefore, in place of $1, bash substitutes a blank. The blank is just whitespace, so it doesn’t pollute the remaining 25.27, which TryScanner finds and reports as a readable number.
(Possible) explanation of third result:
$$, in bash, is the command to get the process id (pid) of the terminal. In this case, it’s obviously 2282, which is substituted (by bash) in front of 1.28. The resulting number 22821.28 is read and reported by TryScanner as a readable number.
(Possible) explanation of the fourth result:
In Perl, Java, and (it appears) bash as well, the backspace \ can be used to “escape” other characters. When you escape a character, you change its meaning. We know that the dollar sign $ has special meaning in bash; apparently the \ in front changes it to just a regular character. Bash hands $56.39 to TryScanner, which, seeing the dollar sign in front, deems $56.39 not a readable number.
While, for the time being, I’d say we’re finished talking about the Java Scanner example, this post has opened up other topics for the future. HIH:)
I used many helpful sources for this post:
Kurniawan, Budi. Java 5: A Beginner’s Tutorial. 2006: Brainy Software Corp.
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.