Issue
I want to write a script which will take the output of a job submission script as a variable to be used for the same next for loop. I have written it as follows
temp= (40 50)
for i in "${!temp[@]}"; do
cd ${temp[i]}-$((temp[i]+10))ns;
((counter++))
var="ID$((counter-1))"
var1=ID$counter
if [[ "$i" -eq 0]]; then
var1=$(qsub jobsubmission.sh);
else
var1=$(qsub -W depend=afterok:${var} jobsubmission.sh);
fi
cd ..;
done
The script goes into a directory, submits a job then goes into the next directory and submits a job based on dependancy of the job created in the first loop. The problem in this script is that the variable "var" is not taking the value of variable assigned in the before loop. Please suggest a way.
Solution
In trying to simplify your script for troubleshooting, I identified the two "!" are extraneous, preventing the array values from being derived.
Also, it is not advisable to reuse the same variable for looping in multiple loops within the same code.
Lastly, to pass position-dependant values to the second loop for, again, position-dependant usage, you need to assign those values to another array.
The distilled logic flow and diagnostic coding is this:
#!/bin/bash
#lastVal=""
altVal=()
temp=( 50 60 )
action()
{
#for i in "${!temp[@]}"
for i in "${temp[@]}"
do
echo "i = ${i}"
#lastVal="ABC_${i}"
altVal[$i]="ABC_${i}"
#mkdir ${temp[i]}-$((temp[i]+10))ns
#cd ${temp[i]}-$((temp[i]+10))ns
#lastVal=$(qsub jobsubmission.sh)
#cd ..
done
}
#action lastVal
action
echo ""
temp=( 60 70)
#for j in "${!temp[@]}"
for j in "${temp[@]}"
do
echo "j = ${j}"
previous=$( expr ${j} - 10 )
echo "${altVal[ ${previous} ]}"
echo ""
#mkdir ${temp[j]}-$((temp[j]+10))ns
#cd ${temp[j]}-$((temp[j]+10))ns
#var1=$(qsub -W depend=afterok:${lastVal} jobsubmission.sh)
#cd ..
done
and the session log is this:
ericthered@OasisMega1:/WORKS$ ./test_41.sh
i = 50
i = 60
j = 60
ABC_50
j = 70
ABC_60
ericthered@OasisMega1:/WORKS$
Answered By - Eric Marceau Answer Checked By - Senaida (WPSolving Volunteer)