LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   randomized lines (https://www.linuxquestions.org/questions/linux-newbie-8/randomized-lines-568389/)

gentech 07-11-2007 07:29 AM

randomized lines
 
hi
i am trying to createa a randomized number
and then when number is picked by the randomizer
it shows a line..


when you do !test in the game..
it should give a randomized line.
it recognizes !test command

but it says script is wrong..

script :

Code:

#!/bin/sh
rand=$(($RANDOM % 3))
if test $rand -eq 0
then echo "test1"
fi
if test $rand -eq 1
then
echo "test2"
fi
if test $rand -eq 2
then
echo "test3"
fi

it gives me this error :
Quote:

addons/test.sh: line 12: syntax error: unexpected end of file

anyone an idea or solution for my problem :(

wjevans_7d1@yahoo.co 07-11-2007 07:41 AM

You need a quotation mark before test3.

Hope this helps.

muha 07-11-2007 07:42 AM

Probably
echo test3"
is missing a "
It should be:
echo "test3"

berbae 07-11-2007 07:42 AM

change line 12 from
echo test3"
to
echo "test3"

gentech 07-12-2007 01:20 AM

i tried it.. but still it gives me that error.. :(

chrism01 07-12-2007 01:55 AM

Ok guys, look at this:

Code:

#!/bin/sh
rand=$(( $RANDOM % 3 ))
if test $rand -eq 0
then
    echo "test1"
fi
if test $rand -eq 1
then
    echo "test2"
fi
if test $rand -eq 2
then
    echo "test3"
fi

will not work.
But replace /bin/sh with /bin/bash or /bin/ksh and it does.
You have to know the syntax of the shell you specified... :)

nx5000 07-12-2007 04:14 AM

Quote:

Originally Posted by chrism01
will not work

It depends on what your shell is and how it's configured.
If you are running bash, it doesn't matter because it doesn't really look at the first line (one of bash oddities). So this works on a lot of default install. It works on my machine.

If you are running another one like "dash" then it will look at the first line (shebang).

chrism01 07-12-2007 05:21 AM

that's what my next line says ..... depends on which shell you use.
Note that (for those who don't know) on Linux sh is often symlinked to bash, but that won't always happen.
In production environments, you should ALWAYS specify the shell to be used, just in case the SW is installed under a user whose default shell turns out to be different.

nx5000 07-12-2007 11:42 AM

Unfortunatly your 2 posts and mine are not clear enough..
Better:
It depends on which shell calls this script
:)

chrism01 07-13-2007 01:23 AM

I could be wrong, but I think that's only true if a cmd shell is NOT specifed as the first line.
Otherwise, i think the parent shell (or kernel?) will try to use the shell specified ...
Actually, that happened today at work.
Tried to run a script that specified #!/bin/ksh, but it turned out that the local version was in /usr/bin/ksh

theYinYeti 07-13-2007 02:10 AM

You can explicitely tell what shell to use, as in:
source ./script.sh
or
/bin/ksh ./script.sh

Else the shell used will be the one told on the first line. As said above, on Linux, /bin/sh is often an alias for /bin/bash; however /bin/bash, when called as sh, changes its inner workings to be closer to the behaviour of the old original sh.

In your case, $((...)) is a bash-only feature, hence the problem you had. However this line can be "standardized" with the expr command.

Yves.

wjevans_7d1@yahoo.co 07-13-2007 05:58 AM

Quoth theYinYeti:

Quote:

on Linux, /bin/sh is often an alias for /bin/bash; however /bin/bash, when called as sh, changes its inner workings to be closer to the behaviour of the old original sh.

In your case, $((...)) is a bash-only feature, hence the problem you had.
That can't be it. I run this:

Code:

#!/bin/sh

echo $((3+5))

and get 8.

nx5000 07-13-2007 06:02 AM

That's because of what I said, you are running bash.

This script is a "bashism". It will only work for people who have /bin/sh pointing to /bin/bash, I have /bin/sh linked to /bin/dash so it doesn't work.

Whereas the "source", it's a bit different: it executes in the current shell, so it does not spawn a new shell, see this example. Take care ;)
Code:

#!/bin/sh
echo "hi!"
exit

chmod +x script
Try this:
./script
And then this:
source ./script.sh

:)

theYinYeti 07-13-2007 07:20 AM

You're right wjevans! The man page changed since I read about this issue back in the early '90. Now it reads like this:
<<When invoked as sh, bash enters posix mode after the startup files are read.>>
and I suppose $((...)) is posix. I stand corrected.

Yves.

nx5000 07-13-2007 07:33 AM

True $(( works on dash too
I should have checked maybe :)

These one only work (as expected) in bash:

echo "test" &> /tmp/u
echo d{a,b,c}
echo $(( $RANDOM % 3 ))

...etc


All times are GMT -5. The time now is 05:27 PM.