LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-27-2006, 02:18 PM   #1
ChrisScott
Member
 
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:

field=""
fieldnumber=0

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

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
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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:
Code:
#!/bin/sh

# @(#) s1       Demonstrate eval.

field=""
fieldnumber=0

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

        eval field$fieldnumber="\"$field\""
        eval echo field$fieldnumber="\"$field\""
done
which, when run, produces:
Code:
% ./s1
1
field1=1
2
field2=2
done
field3=done
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
ChrisScott
Member
 
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!

C
 
Old 11-27-2006, 04:05 PM   #4
berbae
Member
 
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
/bin/bash
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.
((fieldnumber++))

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:21 PM.

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
Open Source Consulting | Domain Registration