Shell: while loop wont work in my freeBSD, any idea?

vaclinux

Member

Reaction score: 1
Messages: 61

Dear,
Hope you can try
Code:
  opt="x"
        while [[ $opt != "y" && $opt != "n" ]]
        do
         echo "Do you want to continoue(y/n)"
         read opt 
        done
and help why i got
[[: not found
after executing the shell script in my freebsd 6.2, it seem the "[[" assume as a command.

Btw, this script is working in my linux os(fedora10)
 

vermaden

Son of Beastie

Reaction score: 1,125
Messages: 2,729

You use strict bash(1) syntax, which is not POSIX compatible (works only @ bash), use strict POSIX sh(1) syntax, even on Linux, then you will have much less problems porting your scripts to other operating systems, here is a code that does the same and works the same, even under POSIX sh(1):

Code:
OPT="x"
while [ ${OPT} != "y" -a ${OPT} != "n" ]
do
  echo -n "Do you want to continue (y/n): "
  read OPT
done
 
OP
OP
vaclinux

vaclinux

Member

Reaction score: 1
Messages: 61

It is working now,
I need to understand more about POSIX standard,
Thanks,
 

vermaden

Son of Beastie

Reaction score: 1,125
Messages: 2,729

vaclinux said:
It is working now,
I need to understand more about POSIX standard,
Thanks,
You are welcome, generally if it works under #!/bin/sh under FreeBSD/OpenBSD/NetBSD then its POSIX standart, that do not applies to #!/bin/sh at Linux, since there 99% of the times /bin/sh --> /bin/bash (is just a symlink).
 

Brandybuck

Active Member

Reaction score: 11
Messages: 147

Bash is not the Bourne (POSIX) shell. It add a whole bunch of really nice features, and is mostly compatible with sh. But not quite. That wouldn't be a problem, except that a few Linux distros decided to symlink bash to sh. That works fine under those Linux distros, but the scripts can break elsewhere if they have bash-isms in them.
 

graudeejs

Son of Beastie

Reaction score: 692
Messages: 4,615

why do they use bash, when there is mksh ?
I love mksh, so far i can do in console anything that i could do in #!/bin/sh.
 

vermaden

Son of Beastie

Reaction score: 1,125
Messages: 2,729

killasmurf86 said:
why do they use bash, when there is mksh ?
I love mksh, so far i can do in console anything that i could do in #!/bin/sh.
mksh is designed for INTERACTIVE use, while /bin/sh only to be POSIX compatible without ANY interactive enhaecements like completion and so, thus /bin/sh is a lot better then mksh as system shell for scripts.
 
Top