LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Closed Thread
 
Search this Thread
Old 04-08-2007, 01:50 AM   #1
mohtech
Member
 
Registered: Dec 2004
Location: Denver Colorado
Distribution: Kubuntu/Debian
Posts: 120

Rep: Reputation: 15
I feel stupid: declare not found in bash scripting?


I was anxious to get my feet wet, and I'm only up to my toes before I'm stuck...this seems very very easy but I'm not sure what I've done wrong. Below is the script and its output. What the heck am I missing?

______________________________________________________
#!/bin/bash
declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${ PROD[*]}"
_______________________________________________________

products.sh: 6: declare: not found
products.sh: 8: Syntax error: Bad substitution


mohtech
 
Old 04-08-2007, 02:17 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I ran what you posted (but at the command line, not in a script, though that should make no significant difference), and got this:

Code:
-bash: ${ PROD[*]}: bad substitution
In other words, I couldn't reproduce your first problem, the "declare: not found" error. Try the declare command by itself, on the command line.

And I got rid of the "bad substitution" problem when I removed the space which is between the ${ and the PROD on the printf line.

Hope this helps.
 
Old 04-08-2007, 02:26 AM   #3
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
The previous poster identified your second problem.

As far as your first problem goes ... I am not a bash guru although I have written a number of bash scripts. So far I have found no need for declare statements. I suspect that you might not need it either. But if you do want to use it, the following does work:

Code:
#!/bin/bash

declare -a PROD
PROD[0]="computers"
PROD[1]="HomeAutomation"
printf "${PROD[*]}\n"
EDIT: My original post was based on an older version of bash. When I tried the declare statement you posted I got an error message, but one that was different from yours. I just tried it on a newer version of bash, and your declare statement worked fine. So it might depend on the version of bash you are running. What I posted above runs fine on both versions.

Last edited by blackhole54; 04-08-2007 at 02:35 AM.
 
Old 04-08-2007, 07:37 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

As wjevans_7d1 wrote, removing the space when using the array name allows it to run. Here is a sample:
Code:
#!/bin/bash

# @(#) s1       Demonstrate declare and printf.

bash --version
declare -a PROD[0]="computers" PROD[1]="HomeAutomation"
printf "${PROD[*]}"
printf "\n"
Producing:
Code:
% ./s1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
computers HomeAutomation
Best wishes ... cheers, makyo

Last edited by makyo; 04-08-2007 at 08:04 AM.
 
Old 04-29-2010, 07:00 AM   #5
brianhickey18
LQ Newbie
 
Registered: Apr 2010
Posts: 3

Rep: Reputation: 0
Unhappy

Im having the similar problem as mohtech
It doesnt seem to recognise declare.
I'm having the same problem with let and basic math code like incrementing a variable.
Even creating an simple array gives me an error
example

names=( john thomas )
syntax error: `names=' unexpected

I reckon I must have something disabled somewhere on the system.
Im working on a beta server at work so I wasn't involved in the initial installation/setup.

any ideas how I could remedy this?
 
Old 04-29-2010, 07:19 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
@brianhickey18: please start your own thread and give details of the script before the names= line. Software changes so it's seldom useful to revive a 3+ year old thread.
 
Old 04-29-2010, 09:09 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
And, with that sage advice, old fossil thread now closed....
 
  


Closed Thread

Tags
bash, scripting


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
Stupid Scripting Question steve_f60 Linux - General 2 03-10-2006 04:21 PM
God do I feel stupid, please read, laugh and help! jmpmjmpm Linux - General 12 02-22-2005 05:18 PM
question about bash look and feel Hockeyfan Suse/Novell 4 12-28-2004 05:29 PM
I feel stupid asking this... mjakob Linux - General 6 01-21-2002 04:31 AM
I Feel Stupid:) Insanepyro69 Linux - Newbie 21 11-11-2001 01:19 AM


All times are GMT -5. The time now is 01:20 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration