Cond statements, BASHForum: Other Help Topics Topic: Cond statements, BASH started by: jpeters Posted by jpeters on Sep. 08 2007,19:00
Is the DSL version of BASH stripped down? I haven't been able to get conditional statements working (e.g., if/then).
Posted by ^thehatsrule^ on Sep. 08 2007,20:09
No, they work. The version of bash is 2.x if memory serves. Perhaps you weren't using that or there's some kind of scripting error?EDIT: packages section shows 2.05b Posted by jpeters on Sep. 09 2007,01:23
I'm using the same version, 2.05b. Here's an example out of O'Reilly, with the result. dsl@box:~$ if {23 == 23}; then echo "okay"; fi bash: {23: command not found Posted by mikshaw on Sep. 09 2007,02:43
I'm not sure why curly braces were used in that example. I can't recall ever seeing tests done that way.Any of these should work: if [ 23 == 23 ]; then echo "okay"; fi if [ 23 -eq 23 ]; then echo "okay"; fi if test 23 == 23 ; then echo "okay"; fi if test 23 -eq 23 ; then echo "okay"; fi test 23 == 23 && echo "okay" test 23 -eq 23 && echo "okay" [ 23 == 23 ] && echo "okay" [ 23 -eq 23 ] && echo "okay" == is used for string or math comparison -eq is apparently used for integers only Posted by jpeters on Sep. 09 2007,02:52
Actually, I tried all types of braces with the same result. In your listed examples, only the "if test ...." worked....seems to be looking for a command following "if".... With "if test" I didn't need any braces: if test 23 == 23; then echo "ok"; fi Posted by mikshaw on Sep. 09 2007,02:56
I think there's something screwy with your bash.Are you using #!/bin/sh as an interpreter instead of #!/bin/bash? I've read that Bash behaves differently if it's called as sh Posted by jpeters on Sep. 09 2007,03:18
Same result using /bin/sh (as /bin/bash). I've tried it on two computers with the same results. EDIT: I just compiled BASH 3.2, and got the same results! Now I'm really baffled. EDIT2: Okay; got it working. Problem was that there needs to be a space before and after the brackets: if [ 3 -eq 3 ] (I put in several, just to make sure...it's hard to see single spaces in print ) Posted by mikshaw on Sep. 09 2007,13:43
"[" is a command, just as "test" is a command. If you think about it that way it might not even occur to you to _not_ include the spaces. After all, you wouldn't use the test command this way: if test23 == 23
Posted by curaga on Sep. 09 2007,15:14
actually [ is an alias for test..
Posted by ^thehatsrule^ on Sep. 09 2007,15:39
If you really want to be technical, I think [ and test are separate binaries (different invokes I suppose - though I guess busybox would have all symlinks directing back to it)
Posted by curaga on Sep. 09 2007,15:50
Or even more tech: both [ and test are actually the same common shell builtin, atleast bash and ash have it. So when invoked just like that, [ something or test something, they are the same thing, but as binaries, /usr/bin/[ something, they would be different ;)
Posted by roberts on Sep. 09 2007,16:22
When struggling with bash syntax it is often helpful to preuse existing bash scripts, i.e., /etc/init.d/knoppix-autoconfig and/or /etc/init.d/dsl.config
Posted by jpeters on Sep. 09 2007,21:34
I'm always impressed by the level of expertise on this board. Although I'm not sure where someone learns about binaries related to "[" vs. "test" , I'm duly impressed and enlightened. I'm studying knoppix-autoconfig right now with vim, where spacing is clear. Posted by mikshaw on Sep. 09 2007,23:37
When it comes to "[" specifically, I just thought it was very odd to see that character listed as a filename when i did "ls /usr/bin", so I had to find out what was up. This is kind of an unusual situation because "man [" and "[ --help" don't work. However, "/usr/bin/[ --help" does work:
Posted by ^thehatsrule^ on Sep. 10 2007,00:39
In DSL, it looks like [ is a symlink to test. However, as curaga pointed out, it's also a bash/ash built-in, so removing it could save a couple KB -- unless something else requires it? (and put it in gnu-utils, if it isn't already in there?)
Posted by jpeters on Sep. 10 2007,04:02
The /usr/bin --help files are the same for both "[" and "test." Manual for test: NAME 2 test - check file types and compare values 3 SYNOPSIS 5 test EXPRESSION 6 [ EXPRESSION ] 7 test OPTION What seems to differ from other languages is that "if" requires a command to follow it; thus the use of brackets (generally reserved for commands) instead of curly braces (for grouping, as with TCLTK, C++, etc.) |