if loop not working in BASH shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if loop not working in BASH shell
# 1  
Old 11-09-2008
if loop not working in BASH shell

i have this code for a simple if loop:

#!/bin/bash
array="1 2 3 4 5"
array2="5 6 7 8 9"
if [ ${array2[0]} -gt ${array[0]} ]; then
echo "${array2[0]} is greater than ${array[0]}!!"
fi

the error is

./script8: line 9: [: too many arguments

./script8: line 9: [: too many arguments

./script8: line 9: [: too many arguments

./script8: line 9: [: too many arguments

./script8: line 9: [: too many arguments
# 2  
Old 11-09-2008
You are assigning the values as a common string to the variables, try this:

Code:
array=(1 2 3 4 5)
array2=(5 6 7 8 9)

instead of:

Code:
array="1 2 3 4 5"
array2="5 6 7 8 9"

# 3  
Old 11-09-2008
Use:
array=(1 2 3 4 5)
array2=(5 6 7 8 9)

to set up arrays in bash. With the quotes you're putting everything into element zero.
# 4  
Old 11-09-2008
ok thanks that worked..

now i am trying to get some numbers from a .txt file and put them into an array but when i try to echo it i get blank lines...

#!/bin/bash
array=(20 20 20 20 20)
j=0
while [ $j -lt 5 ]
do
awk '/%/' /cygdrive/c/unix/bin.txt | awk '{print $5}'> $[collectbininfo[j]]
echo ${collectbininfo[j]}
echo "$j"
j=`expr $j + 1`
done

bin.txt is the file from which i am picking out the numbers and the line which has the numbers has a % sign.. the file looks like this:

[1] Continuity 32 56%
[2] Continuity 32 56%
[3] Continuity 32 56%
[4] Continuity 32 56%
[5] Continuity 32 56%

i want to put all the 32's in an array

i already used the tr command on the bin.txt file to remove the CR's
thanks for your help guys.. i am still learning unix shell programming..
# 5  
Old 11-09-2008
The 32's are in the 3th field of your file IMO, adjust the field in the awk code if I'm wrong. This is an example how to fill and print an array:

Code:
#!/bin/bash

i=0

awk '/%/{print $3}' /cygdrive/c/unix/bin.txt |
while read s
do
  arr[$i]="$s"
  let i=i+1
done

len=${#arr[*]}
i=0

while [ $i -lt $len ]
do
  echo "${arr[$i]}"
  let i=i+1
done

# 6  
Old 11-09-2008
what is "s"?
# 7  
Old 11-10-2008
The read command assigns the output of the awk command to the shell variable s.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bourne shell is not working

I have a loop with cases I am working on Bourne shell for file in *.${Today}*.csv *.${Today}*.txt\ do case ${file} in sun_detail) do something ;; sum) do something ;; mod) do something ;; *) do something ;; (5 Replies)
Discussion started by: digioleg54
5 Replies

2. Shell Programming and Scripting

Aliases NOT working inside bash shell script

i have defined a function ln_s() for customizing the ln command in script1.sh. more script1.sh echo "Starting Execution" ./script2.sh echo "End of Execution" ln_s(){ ] && return ln -s "$1" "$2" } My script1.sh executes another script2.sh which has the following entry more script2.sh... (12 Replies)
Discussion started by: mohtashims
12 Replies

3. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

4. Shell Programming and Scripting

Shell script loop not working

Hi I'm using this script to transcode videos in an Ubuntu 12.04 machine. #! /bin/bash MOVDIR="/media/topaz_1/media/transcodes/transcode_mov/" MOVDESTDIR="/media/topaz_1/media/transcodes/final_mov/" DONEFILESDIR="/media/topaz_1/media/transcodes/dv_cache/"... (1 Reply)
Discussion started by: oscarfelson
1 Replies

5. UNIX for Dummies Questions & Answers

For loop in bash shell

Hi, I am using a for loop to manipulate files data_1.txt through data_100.txt. The for-loop is set up like this: for i in {1..100}; do cut -f1 data_$i.txt > output$i.txt I get the following error message when I run the code: cannot open `data.txt' for reading: No such file or directory... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. Shell Programming and Scripting

BASH: Factorial using FOR loop not working

Hi, I am trying to run the factorial script but it’s not working. The code is mentioned below: ------------------------------------------------------------------ /home/gc> cat fact.sh #!/bin/bash # using one command line parameter factorial=1 for (( number = 1; number <= $1 ;... (3 Replies)
Discussion started by: Technext
3 Replies

7. Shell Programming and Scripting

export not working in Bash shell

Hi Friends, I am presently migrating shell scripts writter in KSH to SH.I am stuck at this place and i am not able to find a work around:- Let the script name is x.sh Below are some of the codes in it... export abc=hello export abc=hi export abc=how When i am trying to compile the script ... (6 Replies)
Discussion started by: amit.behera
6 Replies

8. Shell Programming and Scripting

Shell Script not working using for loop and a funtion

Hi- Here is the shell script that for some reason is not returning results: #! /bin/ksh - avg() { AVG=0 typeset SUM=0 if then echo "You must have at least two numbers" else for NUM in "$*" do ... (2 Replies)
Discussion started by: koomba
2 Replies

9. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

10. UNIX for Dummies Questions & Answers

korn shell to bash - statement not working

Everything else seems to be working, but this isn't. Is it the "cat..." that is wrong of the condition? Thanks. cat tc_result.txt | while read LINE do if then let "files_run += 1"; echo "inside the if loop" # save current filetype case $LINE... (5 Replies)
Discussion started by: brdholman
5 Replies
Login or Register to Ask a Question