LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 08-26-2009, 01:05 AM   #1
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6
Thanked: 0
Question Shell script problem


[Log in to get rid of this advertisement]
Can anyone help me to find what the problem with the script

Code:
cd $CPFs
	k=0
	for i in *
	do
		if [ -d "$i" ]
		then
			ARRs[$k]="$i"
			k=$(($k+1))
			#echo "$i"
		fi
	done

for Ds in ${ARRs[@]}
	do
		echo "$Ds"
	done
When I tried this I got the following error
Code:
ARRs[0]=1: not found
Please help me.
linuxubuntu canishk is offline  
Tag This Post , , , ,
Reply With Quote
Old 08-26-2009, 01:30 AM   #2
GrapefruiTgirl
Moderator
 
Registered: Dec 2006
Location: N.S.
Distribution: Slackware64
Posts: 4,233
Thanked: 179
For one thing, when assigning an array element like that, you don't need the $ inside the [$k]. Remove that and try it. Should work.

Sasha
linuxslackware GrapefruiTgirl is online now     Reply With Quote
Thanked by:
Old 08-26-2009, 01:30 AM   #3
vishesh
Member
 
Registered: Feb 2008
Location: india
Distribution: Fedora,RHEL,Ubuntu
Posts: 267
Thanked: 24
Quote:
Originally Posted by canishk View Post
[code]

ARRs[$k]="$i"
Try following
${ARRs[$k]}="$i"

thnks
windows_xp_2003 vishesh is offline     Reply With Quote
Thanked by:
Old 08-26-2009, 01:55 AM   #4
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6
Thanked: 0

Original Poster
Quote:
Originally Posted by vishesh View Post
Try following
${ARRs[$k]}="$i"

thnks

I tried your solution, but got something like this


Code:
${ARRs[$k]}="$i"
	k=$(($k+1))
Code:
difference.sh: 42: Bad substitution
where line 42 is the last line of code(EOF).

Quote:
For one thing, when assigning an array element like that, you don't need the $ inside the [$k]. Remove that and try it. Should work.

Sasha
Sasha,

I tried Your suggestion too, I got following output.

Code:
difference.sh: 42: ARRs[k]=1: not found
difference.sh: 42: ARRs[k]=2: not found
difference.sh: 42: ARRs[k]=3: not found
Hope need some more help, Visesh and Sasha, Thank you for your kind help.
linuxubuntu canishk is offline     Reply With Quote
Old 08-26-2009, 02:52 AM   #5
konsolebox
Member
 
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware
Posts: 957
Thanked: 15
Quote:
Originally Posted by canishk View Post
Code:
difference.sh: 42: Bad substitution
difference.sh: 42: ARRs[k]=1: not found
difference.sh: 42: ARRs[k]=2: not found
difference.sh: 42: ARRs[k]=3: not found
what shell are you using? bash, ksh, zsh or just plain bourne shell?
windows_xp_2003 konsolebox is offline     Reply With Quote
Old 08-26-2009, 02:58 AM   #6
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6
Thanked: 0

Original Poster
Quote:
Originally Posted by konsolebox View Post
what shell are you using? bash, ksh, zsh or just plain bourne shell?

its bash

Code:
GNU bash, version 3.2.48(1)-release (i486-pc-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
linuxubuntu canishk is offline     Reply With Quote
Old 08-26-2009, 03:39 AM   #7
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,132
Thanked: 464
Please, try to run the script as
Code:
bash -x yourscript.sh
this will show you a trace of the actual commands executed by the shell after shell's substitutions. This is useful for debugging. And please post the result of the above here. You know... we are really curious! Also, what is the actual content of the directory $CPFs?
linuxcentos colucix is offline     Reply With Quote
Thanked by:
Old 08-26-2009, 03:44 AM   #8
Disillusionist
Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 825
Thanked: 48
I copied your code (from post #1) and it worked perfectly suggesting that there is a problem with the input that it's getting.

Can you provide the output from:
Code:
ls -lb|grep '^d'
linuxubuntu Disillusionist is offline     Reply With Quote
Thanked by:
Old 08-27-2009, 12:13 AM   #9
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6
Thanked: 0

Original Poster
Lightbulb

Quote:
Originally Posted by colucix View Post
Please, try to run the script as
Code:
bash -x yourscript.sh
this will show you a trace of the actual commands executed by the shell after shell's substitutions. This is useful for debugging. And please post the result of the above here. You know... we are really curious! Also, what is the actual content of the directory $CPFs?
Thanks,

the error only shows when I try using sh script.sh, It will not showing when I tried with bash script.sh or with ./script.sh

Problem Solved.
linuxubuntu canishk is offline     Reply With Quote
Old 08-27-2009, 03:20 AM   #10
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,852
Blog Entries: 6
Thanked: 226
Quote:
Originally Posted by vishesh View Post
Try following
${ARRs[$k]}="$i"

thnks
I don't know what that does but it is not necessary to assign the value of $i to ARRs[k]
linux catkin is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
problem with shell script john smith Linux - Newbie 4 03-14-2009 07:03 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM
Problem in shell script Kumar Programming 4 04-27-2004 09:48 AM
shell script problem steltner Linux - General 19 05-18-2003 03:42 PM


All times are GMT -5. The time now is 07:32 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration