Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 11-27-2006, 02:18 PM   #1
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
Command that works on BASH promp but not from a shell script!

Hi all, anyone feel like enlightening a beginner-shell-scripter?

The section of code I'm stuck on is supposed to take a (unspecifed) number of field titles - for a latex table. This is my code as it stands:


until [ "$field" = "done" ] ; do
read field
fieldnumber=$((fieldnumber + 1))

Here's the bit that really confuses me! When I run the scrip and enter, say, 'example' BASH returns:

field1="example": command not found

obviously if I type that command directly into the BASH prompt it works fine. I thought that a shell script should work in exactly the same way as if you type it straight into the prompt - so why the error?

Any ideas would be appreciated. Cheers, Chris.
Old 11-27-2006, 03:40 PM   #2
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 730

Rep: Reputation: 75
Hi, Chris.

Most interpreted languages provide 2 features that control scanning. One is some kind of escape mechanism that says "don't look at the enclosed text for anything special"; this is often a set of quote marks. The other, often eval, says "look at this chunk again".

For your code, I added the eval and an eval of an echo. The echo, being a print statement, is often your best debugging tool:

# @(#) s1       Demonstrate eval.


until [ "$field" = "done" ] ; do
        read field
        fieldnumber=$((fieldnumber + 1))

        eval field$fieldnumber="\"$field\""
        eval echo field$fieldnumber="\"$field\""
which, when run, produces:
% ./s1
If you are going to post code here often, please use the CODE tags -- highlight, then click "#".

For this problem, you might want to consider using an array, q.v.

Best wishes ... cheers, makyo

( edit 1: addition )

Last edited by makyo; 11-27-2006 at 03:43 PM.
Old 11-27-2006, 03:55 PM   #3
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Original Poster
Rep: Reputation: 15
Thanks Makyo. I hadn't noticed the CODE button - I'll use it in future!

Old 11-27-2006, 04:05 PM   #4
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
eval field$fieldnumber="\"$field\""
Old 11-27-2006, 07:11 PM   #5
Senior Member
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
FYI, to increment a variable, this works in bash.

$ AA=1;((AA++));echo $AA


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
works on command line but not in bash script tara Linux - General 7 02-09-2009 03:57 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 10:27 AM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 02:00 AM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM
useradd command needed from shelll promp tarak4u Linux - Networking 1 07-31-2002 05:09 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:02 PM.

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