bash Script: Issue with iterating Directory and store into array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash Script: Issue with iterating Directory and store into array
# 15  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Ok, then try following:
Code:
echo $SHELL
which $SHELL

both are /bin/bash
Code:
/bin/bash
/bin/bash

# 16  
Old 08-26-2009
Hm I have no idea currently why it does work on command line but not in a script. No clue, sorry.
# 17  
Old 08-26-2009
Quote:
Originally Posted by zaxxon
Hm I have no idea currently why it does work on command line but not in a script. No clue, sorry.

No worries, anyway thank you for your great help.

I will update if I found something.

Regards,

Anish

---------- Post updated at 04:40 PM ---------- Previous update was at 12:58 PM ----------

When I changed the mode using chmod and I tried with ./ not sh the it worked fine.

Whenever I tried with sh script.sh, I got that error, but not with ./script.sh.

Thanks for the help.
# 18  
Old 08-26-2009
i copy exxactly the code you put i change the valor of $CPFs
for a directory of my server, and the script work OK (HP-UX, with sh)....
the error is in line 42 but the code you put si more short than 42 lines, can you put all the code of you scritp
# 19  
Old 08-26-2009
Quote:
Whenever I tried with sh script.sh, I got that error, but not with ./script.sh.
Tbh I never call any script with a shell in front of it. Glad you found it though Smilie
# 20  
Old 08-26-2009
when i write the my last time i don't see all you response but now i read...
I'm probing, now i'm in linux enviorement with bash and i has the same problem that you say.
Code:
$ sh new.sh
new.sh: 11: ARRs[0]=dir1: not found
new.sh: 11: ARRs[1]=dir2: not found

As i now in sh (i don't now version) you must declare as array de variable, for this porpuse is
Code:
man sh
" sh is the standard command interpreter for the system.  The current ver‐
     sion of sh is in the process of being changed to conform with the POSIX
     1003.2 and 1003.2a specifications for the shell.  This version has many
     features which make it appear similar in some respects to the Korn shell,
     but it is not a Korn shell clone (see ksh(1)). "

Code:
man ksh

" To assign values to an
      array, use set -A name value ....  The value of all subscripts must be
      in the range of 0 through 1023.  Arrays need not be declared.  Any
"

if you execute ./new.sh you execute under bash because is your shell by defect...and shell works Ok, you can optain the same result if you execute:
Code:
bash new.sh

I'm think that when you execute whith sh something happens and the error is the same that if yo make this shell in old unix version ( o may be better said old sh ) that they need you declare de arrays with
set -a ARRs valor1 valor2 .......
i'm not suere what is the problem but think this thinks can be relation...
(sorry i'm spanish and i don't explain so good in english)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need a Bash script for iterating thru an array and running a command

Hi , I am a total beginner so bear with me. I have the below code which works . I need to extend it by iterating thru the array arr and executing a command in each loop. some thing on the lines of below. I need to run this in a Jenkins script , so I would need below bash script to run... (6 Replies)
Discussion started by: SVRao19056
6 Replies

2. Shell Programming and Scripting

Shell script to loop and store in array

I'm trying to achieve the follwoinig with no luck. Find the directories that are greater than 50GB in size and pick the owner of the directory as I would like to send an alert notification. du -sh * | sort -rh 139G Dir_1 84G Dir_2 15G Dir_3 ls -l Dir_1 drwx------ 2... (3 Replies)
Discussion started by: 308002184
3 Replies

3. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Iterating awk over a directory of files?

I need to run the same awk function over an entire directly of files. This is the awk: awk '{$(NF+1)=1}1' Is there a way that I can run this command once over all of the files, along the lines of: awk '{$(NF+1)=1}1' * so that I do not have to run this several times? My main concern is... (2 Replies)
Discussion started by: owwow14
2 Replies

5. Shell Programming and Scripting

Bash Script Iterating Question

I am trying to look through one of my directories to remove certain files. I am pretty new to Unix and bash so I just need a little help in starting this. I know I would have to write two loops one to iterate the directories and one to iterate the files. How would I write the loops? (3 Replies)
Discussion started by: totoro125
3 Replies

6. Shell Programming and Scripting

Array Issue In Bash

Hi, I have the following code which is giving error mentioned below. Please can you support on this. Also suggest how can we access all the items against single vasservicename in circlename array,i.e, vasservicename say MTSTV will be available to all circles which are mentioned in the array... (2 Replies)
Discussion started by: siramitsharma
2 Replies

7. Shell Programming and Scripting

Script to store the variable in a table or array.

Hi, I have few variable say 10 ex:- l_pc_291334_01_0_01_00.cmp l_pc_441133_50_0_02_00.cmp l_pc_441133_55_0_02_00.cmp Each variable value is coming via loop on a table. I want to create a script that stores these value to a table or array ( But one by one not all at one time as... (4 Replies)
Discussion started by: amitkumar.b2
4 Replies

8. Shell Programming and Scripting

How to store files names from a directory to an array

Hi I want to store the file names into an array. I have written like this but I am getting error. declare -A arr_Filenames ls -l *.log | set -A arr_Filenames $(awk '{print $9}') index=0 while (( $index < ${#arr_Filenames })); do Current_Filename=${arr_Filenames} ... (5 Replies)
Discussion started by: dgmm
5 Replies

9. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

10. Shell Programming and Scripting

How to store contents of a command in array of variables in shell script?

I want to store contents of command dir in array of variables For eg: dir contents are command d2 demovi~ file inven java new untitled folder d1 demovi er1 filename inven~ myfiles ubuntu desktop xmms ----------------------------------- I... (3 Replies)
Discussion started by: netresearch
3 Replies
Login or Register to Ask a Question