0

i am using WSL (Ubuntu) in Windows. i used bash script.sh for the script below:

    #! /bin/sh
#################LOAD FILES###################
lead_SNPs=`grep "lead_SNPs" ../prep/files.txt | cut -f2`
bfile=`grep -w "bfile" ../prep/files.txt | cut -f2`
bfile_list=`grep -w "bfile_list" ../prep/files.txt | cut -f2`
r2=`grep "r2" ../prep/parameters.txt | cut -f2`
###############LD###########################
if [ ${bfile} = "NA" ]; then
    cat ${bfile_list} | while read line; do 
    file=${line}
    file_n=`echo $file |awk -F '/' '{print $NF}'`

    echo 'Calculating LD'
    plink --bfile ${file} --r2  --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}

    done
else
    file=${bfile}
    file_n=`echo $file |awk -F '/' '{print $NF}'`
    echo ${file_n}
    plink --bfile ${file} --r2  --ld-window-kb 1000 --ld-window 999999 --ld-window-r2 ${r2} --ld-snp-list ${lead_SNPs} --out C:/Users/naghm/Desktop/FDSP-github/ld/${file_n}
fi

but i get this error

Syntax error near unexpected token `fi`

can you correct my code please? i can not understand where i made mistake.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Naghme Vahabi
  • 25
  • 1
  • 6
  • 4
    Please check whether your file has [Windows Line Endings](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – that other guy Jun 22 '20 at 23:33
  • It looks like the call to `plink` in both places is identical. Refactor so the reader doesn't have to guess if that is the case. (ie, write a function and call it in each location) – William Pursell Jun 22 '20 at 23:36
  • 3
    https://shellcheck.net – Jetchisel Jun 22 '20 at 23:37
  • 3
    Try running your script through [ShellCheck](https://www.shellcheck.net/). I just tried it and it made a lot of suggestions. – wjandrea Jun 22 '20 at 23:39
  • i can not undrestand how to add windows line ending in scripts – Naghme Vahabi Jun 22 '20 at 23:44
  • @NaghmeVahabi With editors like notepad3, notepad++, you can control the newline separator to use. – Febtober Jun 22 '20 at 23:46
  • 1
    @NaghmeVahabi You need to *remove* the Windows line endings -- that is, convert the script from DOS/Windows format to unix format. See [the question that other guy linked](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings), especially the top answer's "Solutions" section. – Gordon Davisson Jun 22 '20 at 23:51
  • Is your `#! /bin/sh` line really indented like that? The `#!` characters have to be at the beginning of the line. (If they're not, then `/bin/sh` is (probably) the default anyway.) – Keith Thompson Jun 23 '20 at 00:07

2 Answers2

0

Try to change:

if [ ${bfile} = "NA" ]; then

to

if [ "${bfile}" = "NA" ]; then

I suspect ${bfile} is empty which expanded to:

if [  = "NA" ]; then

in your original line.

Febtober
  • 110
  • 6
  • 1
    I suggest enclosing all other variables in double quotes as well. (ShellCheck suggested by wjandrea seems like a good tool to look for potential problems.) – Febtober Jun 22 '20 at 23:51
0

The first line in your script doesn't look right. It should be

#!/bin/sh

or if using bash shell:

#!/bin/bash
LeadingEdger
  • 604
  • 4
  • 7