0

I have a bash script as below:

day=(58 34 107 91 43 39 41 76 37 47 70 74 56 19 95 38 48 96 50 76 89 79 46 105 26 88 69 87 23 82 99 77 114 52 87 63 33 52 57 45 48 49 55 60 34 107 48 40 25 20 16)

year=(1952 1953 1954 1955 1956 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004)

for dom in $day; do 
   for yrs in $year; do
          ncks -O -d time,$dom imdJJAS$yrs.nc ac_$yrs.nc
   done
done 

Basically i am trying to extract the time dimension for each year using the NCO ncks command, the scripts run but the outputs are incorrect. For year 1951, it succesfully extracted the 58th time value, but from 1952 onwards, it extracts the last value in the day array (16), which is incorrect.

ive tried setting {$day[a]} since its an array, but if i used this, for all years in extracts the last value in the array instead.

I am not too sure what im doing wrong, ive looked through quite a few post regarding this, but it doest seem to be working.

Id appreciate any help.

Cheers!

2 Answers 2

1

$array by itself will expand to the first element in the array. To expand to the full array you should use ${array[@]}:

for dom in "${day[@]}"; do 
   for yrs in "${year[@]}"; do
          ncks -O -d "time,${dom}" "imdJJAS${yrs}.nc" "ac_${yrs}.nc"
   done
done 

I also quoted your variable expansions and changed $dom and $yrs to ${dom} and ${yrs}. The later is done to prevent mistakenly referring to an undefined variable $dom_abc is not the same as ${dom}_abc

Sign up to request clarification or add additional context in comments.

6 Comments

Hi andlrc, Thank you for your assistance, i really appreciate it. I've tried your code as above, but it seems to only use the last element in the day array for ${dom}. The year array appears to be selected correctly, and i feel like the problem lies with the for dom in "$day[@]}"; do command. I just dont know what it is, ive looked through my day array and there is nothing wrong with it, that I can see
I did found an error in my years however, ive forgot to add 1966. The length of each array are correct now, but it still looks like the last value in the day array is overwriting all the other day values.
@KarlChan Where are you referring the dom variable? Outside the iteration?
Yes, the dom arrays are outside the iteration, similar to the ones in my first post. Everything else is the same except ive used the code you've provided.
@KarlChan dom is not an array but used to store one value from the day array. After the iteration dom will contain the last value from day
|
0

If I understand your intention correctly, you are trying to use corresponding values from both arrays. In that case you need a numerical index. for VAR in ARRAY iterates over all values of the array.

2 Comments

wow, someone downvoted you with no comment. What a petty move by them.
yeah, that unfair @MichealVehrs To answer your question, yes that is indeed my intention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.