LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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

Reply
 
Search this Thread
Old 08-26-2009, 12:05 AM   #1
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6

Rep: Reputation: 0
Question Shell script problem


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.
 
Old 08-26-2009, 12:30 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 08-26-2009, 12:30 AM   #3
vishesh
Member
 
Registered: Feb 2008
Distribution: Fedora,RHEL,Ubuntu
Posts: 660

Rep: Reputation: 66
Quote:
Originally Posted by canishk View Post
[CODE]

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

thnks
 
Old 08-26-2009, 12:55 AM   #4
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 08-26-2009, 01:52 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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?
 
Old 08-26-2009, 01:58 AM   #6
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 08-26-2009, 02:39 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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?
 
Old 08-26-2009, 02:44 AM   #8
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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'
 
Old 08-26-2009, 11:13 PM   #9
canishk
LQ Newbie
 
Registered: Aug 2009
Location: India
Distribution: Ubuntu 9.04
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 08-27-2009, 02:20 AM   #10
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,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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]
 
  


Reply

Tags
bash, iterate, script, shell


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


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