LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 06-01-2009, 07:34 AM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Random


Why do I always get the following error?

randomprogram: 3: Syntax error: "(" unxexpected

when I run the following script:

Code:
#!/bin/bash
echo $[($RANDOM % 10)]
 
Old 06-01-2009, 08:01 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Because there is a syntax error!!

It should be:
echo $(($RANDOM % 10))
 
Old 06-01-2009, 08:12 AM   #3
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by pixellany View Post
Because there is a syntax error!!

It should be:
echo $(($RANDOM % 10))
Thanks Pixel, however that yields:

randomprogram: 3: arithmetic expression: expecting primary: " % 10"

grrr why wont this work for me?
 
Old 06-01-2009, 08:21 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What is "randomprogram"? (the name of your script? If so, then where is line 3?)

Does "echo $RANDOM" work?

Does the whole expression work correctly if simply typed in a terminal?
 
Old 06-01-2009, 08:27 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,603
Blog Entries: 25

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Hi,

Works for me from the cli;
Code:
~# echo $(($RANDOM % 10))
3
Start a new session and try again.
 
Old 06-01-2009, 08:39 AM   #6
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by onebuck View Post
Hi,

Works for me from the cli;
Code:
~# echo $(($RANDOM % 10))
3
Start a new session and try again.
Yes echo $(($RANDOM % 10)) works if I type it straight from the CLI.

But it won't work from my very short, 3 line script program.

The script program is called 'randomprogram'. The echo command is on line 3.
 
Old 06-01-2009, 08:45 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
You showed us a two-line script.....??
 
Old 06-01-2009, 08:46 AM   #8
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
The script works perfect from me, experiment time, why does it work if you chmod +x it, then run as ./randomprogram.sh but not as sh randomprogram.sh?
 
Old 06-01-2009, 08:53 AM   #9
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by pixellany View Post
You showed us a two-line script.....??
Sorry Pix, my script now has a carriage return on line two, so it looks like this:

Code:
#!/bin/sh

echo $(($RANDOM % 10))
 
Old 06-01-2009, 08:58 AM   #10
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by jamescondron View Post
The script works perfect from me, experiment time, why does it work if you chmod +x it, then run as ./randomprogram.sh but not as sh randomprogram.sh?
Well i tried sudo sh randomprogram.sh and got:

randomprogram.sh: 6: arithmetic expression: expecting primary: " % 1000"

then i tried sudo ./randomprogram.sh and got:

randomprogram.sh: 6: arithmetic expression: expecting primary: " % 1000"

then I tried ./randomprogram.sh and got:

randomprogram.sh: 6: arithmetic expression: expecting primary: " % 1000"

I'm stooomped!
 
Old 06-01-2009, 09:02 AM   #11
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Weird, see the shabang is ignored when you tell it on the CLI what interpretter to use (ie: sh) which is why you generally have to make it executable so that uses the shabang to pick the interpretter.

Line 6 now? Can you give us an exact copy of the code?
 
Old 06-01-2009, 09:09 AM   #12
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by jamescondron View Post
Weird, see the shabang is ignored when you tell it on the CLI what interpretter to use (ie: sh) which is why you generally have to make it executable so that uses the shabang to pick the interpretter.

Line 6 now? Can you give us an exact copy of the code?
edit: sudo chmod 777 randomscript.sh should make it executable?

Also, maybe my user account cannot run bash or sh scripts or something? how can I check this?

Don't worry about Line 6 there's more carriage returns (empty lines) now.

Last edited by greengrocer; 06-01-2009 at 09:14 AM.
 
Old 06-01-2009, 09:13 AM   #13
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Yes, that line is the shabang, that tells it what interpretter to use when you execute it. When you run 'sh' or 'bash' prior to the this, you're executing the shell and passing the code as an arg. When you do it this way, it ignores the shabang as a comment instead.
 
Old 06-01-2009, 03:17 PM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Also.....Check to see what "/bin/sh" points to. If it is something besides BASH, that could be a problem.

Thanks to james* for the tip on how and when the "shabang" is needed.
 
Old 06-04-2009, 05:43 AM   #15
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Original Poster
Rep: Reputation: 37
/bin/sh is pointing to /bin/dash

and I have tried changing the interpreter to /bin/rbash or /bin/bash

and I still get the error:

randomprogram.sh: 3: arithmetic expression: expecting primary: " % 1000"


whereis sh yields the following:

Code:
sh: /bin/sh /bin/sh.distrib /usr/share/man/man1/sh.1.gz

Last edited by greengrocer; 06-04-2009 at 05:48 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
Box is attempting to scan and ssh into random machines using random usernames gianh Linux - Security 2 11-09-2008 06:42 PM
using /dev/random to output random numbers on a text file guguma Programming 4 04-02-2007 02:42 PM
KDE Random wallpaper or script to create symbolic links to random files cvelasquez Linux - Software 2 02-26-2007 07:48 PM
random no.s cranium2004 Programming 9 04-10-2005 06:37 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM


All times are GMT -5. The time now is 09:08 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