LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2007, 07:29 AM   #1
gentech
LQ Newbie
 
Registered: Jul 2007
Posts: 2

Rep: Reputation: 0
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

Last edited by gentech; 07-11-2007 at 07:41 AM.
 
Old 07-11-2007, 07:41 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
You need a quotation mark before test3.

Hope this helps.
 
Old 07-11-2007, 07:42 AM   #3
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Probably
echo test3"
is missing a "
It should be:
echo "test3"
 
Old 07-11-2007, 07:42 AM   #4
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
change line 12 from
echo test3"
to
echo "test3"
 
Old 07-12-2007, 01:20 AM   #5
gentech
LQ Newbie
 
Registered: Jul 2007
Posts: 2

Original Poster
Rep: Reputation: 0
i tried it.. but still it gives me that error..
 
Old 07-12-2007, 01:55 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
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...
 
Old 07-12-2007, 04:14 AM   #7
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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).
 
Old 07-12-2007, 05:21 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
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.
 
Old 07-12-2007, 11:42 AM   #9
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Unfortunatly your 2 posts and mine are not clear enough..
Better:
It depends on which shell calls this script
 
Old 07-13-2007, 01:23 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
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
 
Old 07-13-2007, 02:10 AM   #11
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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.
 
Old 07-13-2007, 05:58 AM   #12
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
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.
 
Old 07-13-2007, 06:02 AM   #13
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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

Last edited by nx5000; 07-13-2007 at 06:05 AM.
 
Old 07-13-2007, 07:20 AM   #14
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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.
 
Old 07-13-2007, 07:33 AM   #15
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
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

Last edited by nx5000; 07-13-2007 at 07:38 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Asterisk box to connect VoIP lines with normal telephone lines. sraju Linux - Software 1 11-08-2006 12:38 AM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 02:03 PM
Ugly Lines Adrohak Linux - Software 8 07-09-2004 11:50 PM
How many lines? WindozBytes Linux - General 17 03-30-2002 07:10 PM


All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration