user6407765 user6407765 - 4 months ago 6
Bash Question

for loop in shell script not iterating

I'm having a recurring issue with the first for loop (d) not iterating to its next indicated value (1). Any ideas as to what I'm doing wrong? I've tried using the continue command to "manually" force the for loop to iterate to its next value, but that hasn't worked. Essentially, every time I run the code it completes the (0) iteration of the for loop but then stops in terminal without displaying any error messages. I've simplified the second nested for loop so that it only has one value (0) in order to condense the code. Thanks in advance!

#!/bin/bash
# PRSice Commands

dates=$(date +"%m_%d_%Y")
cd /media/sf_Completed_PRS/
mkdir PRS_$dates

echo "Name of input (target) data set: "
read targetname

cd /media/sf_VMShare/
mv $targetname.bim /home/brainlab/
mv $targetname.fam /home/brainlab/
mv $targetname.bed /home/brainlab/

for d in 0 1 2
do
if [ $d = 0 ]; then
gwas=mddgwas.assoc
gwasn=MDD
fi

if [ $d = 1 ]; then
gwas=scz2.assoc
gwasn=SCZ2
fi

if [ $d = 2 ]; then
gwas=BIP.assoc
gwasn=BIP
fi

cd /media/sf_Completed_PRS/PRS_$dates/
directory=$gwasn
mkdir $directory

for t in 0
do

if [ $t = 0 ]; then
lower=.0001
inc=.0009
upper=.001
fi

if [ $t = 1 ]; then
break
fi

cd /home/brainlab
R --file=PRSice_v1.25.R -q --args \
base $gwas \
target $targetname \
slower $lower \
supper $upper \
sinc $inc \
clump.p1 1 \
clump.p2 1 \
clump.r2 0.1 \
clumb.kb 500 \
no.regression T \
plink ./plink_1.9_linux_160914 \
figname compare_1\

cd /home/brainlab

if [ $t = 0 ]; then
mv PROFILES.0.0001.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.0010.profile /media/sf_Completed_PRS/PRS_$dates/$directory
fi

if [ $t = 1 ]; then
mv PROFILES.0.01.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.05.profile /media/sf_Completed_PRS/PRS_$dates/$directory
fi

if [ $t = 2 ]; then
mv PROFILES.0.1.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.2.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.3.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.4.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.0.5.profile /media/sf_Completed_PRS/PRS_$dates/$directory
fi

if [ $t = 3 ]; then
mv PROFILES.1.profile /media/sf_Completed_PRS/PRS_$dates/$directory
mv PROFILES.2.profile /media/sf_Completed_PRS/PRS_$dates/$directory
fi
done

cd /media/sf_Completed_PRS/PRS_$dates/$directory
sed 's/SCORE/SCORE-0.0001/' PROFILES.0.0001.profile > new_file && mv new_file PROFILES.0.0001.profile
sed 's/SCORE/SCORE-0.0010/' PROFILES.0.0010.profile > new_file && mv new_file PROFILES.0.0010.profile
sed 's/SCORE/SCORE-0.01/' PROFILES.0.01.profile > new_file && mv new_file PROFILES.0.01.profile
sed 's/SCORE/SCORE-0.05/' PROFILES.0.05.profile > new_file && mv new_file PROFILES.0.05.profile
sed 's/SCORE/SCORE-0.1/' PROFILES.0.1.profile > new_file && mv new_file PROFILES.0.1.profile
sed 's/SCORE/SCORE-0.2/' PROFILES.0.2.profile > new_file && mv new_file PROFILES.0.2.profile
sed 's/SCORE/SCORE-0.3/' PROFILES.0.3.profile > new_file && mv new_file PROFILES.0.3.profile
sed 's/SCORE/SCORE-0.4/' PROFILES.0.4.profile > new_file && mv new_file PROFILES.0.4.profile
sed 's/SCORE/SCORE-0.5/' PROFILES.0.5.profile > new_file && mv new_file PROFILES.0.5.profile
sed 's/SCORE/SCORE-1/' PROFILES.1.profile > new_file && mv new_file PROFILES.1.profile
sed 's/SCORE/SCORE-2/' PROFILES.2.profile > new_file && mv new_file PROFILES.2.profile

cat > compiledScores$gwasn

awk '{print $1,$2,$6}' PROFILES.0.0001.profile > compiledScores$gwasn
awk '{print $6}' PROFILES.0.0010.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.01.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.05.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.1.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.2.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.3.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.4.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.0.5.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn
awk '{print $6}' PROFILES.1.profile > tempfile.txt
pr -m -t -J compiledScores$gwasn tempfile.txt > new_file && mv new_file compiledScores$gwasn

column -t compiledScores$gwasn > new_file && mv new_file compiledScores$gwasn

done

Kaz Kaz
Answer

Could this command be the problem?

cat > compiledScores$gwasn

This will make your entire script stop, waiting for input into cat.

If a script hangs, use a utility like pstree to see the process lineage (from another terminal, or after suspending the script with Ctrl-Z, if possible).

What is in my hang.sh script?

$ cat hang.sh 
#!/bin/sh

cat

Just that. Let's run it, and suspend:

$ ./hang.sh 
^Z
[1]+  Stopped                 ./hang.sh

Show snippet of the pstree output:

$ pstree | grep hang
     |                |      |-hang.sh---cat

See: hang.sh is running, and has a child: cat.

Another tool in finding where a script is hanging is to turn on its trace mode with set -x. What is in my hang.sh now?

$ cat hang.sh 
#!/bin/sh

set -x

grep -s foo /dev/null # silent command
cat
grep -s bar /dev/null # silent command

cat surrounded by two silent commands that don't produce any output. But we have a set -x at the top, to enable command tracing:

$ ./hang.sh 
+ grep -s foo /dev/null
+ cat

Aha! It executed the first grep, then got stuck in cat, and didn't reach the command after that.