1

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
2
  • 2
    Hard to find the done for each for without indentation. Commented Jul 26, 2016 at 1:31
  • en.wikipedia.org/wiki/Stray_Cat_Blues Commented Jul 26, 2016 at 1:57

2 Answers 2

3

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.

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

2 Comments

Wish that i could up-vote this a few more times. Absolutely the problem in this case. Nice catch.
Thank you so much! That seems to be the exact issue!
0

If d doesn't iterate to next value, possible issue is that you spawn a sub shell in the do part of the for loop. You can run the script in the debug mode ie

#!/bin/bash -x # x for the debug mode

to spot when you spawn a subshell

Sidenotes

In

for t in 0
do
#stuff
done

t can have only one value that is zero.

Also

if [ $t = 0 ]; then 

should have been

if [ $t -eq 0 ]; then # -eq for integers = for strings

1 Comment

Thanks for your response! The "t" for loop is not the one I'm having issues with, it's the "d" for loop. I just got rid of the extra t values to test something.

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.