Bash function using variable in it syntax error

The below bash function uses multiple variables CODING , SAMPLE , SURVEY , and variant
in it. The user selects the cap function and details are displayed on the screen using the $SURVEY variable, the directory is changed to $SAMPLE and the samples.txt is opened so the user can select the sample to use. The numeric id is entered along with some $variant (could be multiple) and the $CODING:$variant are written to an out.txt file and stored /home/cmccabe/cap/TGS . I am getting a syntax error on the last line looking for the matching ' and am not sure if I am using the variables correctly in the function. Thank you :).

samples.txt in $SAMPLE

test-01
test-02
test-03
test-04

$SURVEY format

Survey id
name
details

$CODING format

NM_000012.3
cap() {
CODING=/home/cmccabe/cap/TGS/NM.txt
SAMPLE=/home/cmccabe/cap/TGS
SURVEY=/home/cmccabe/cap/TGS

printf "The survey being used is and here are the details:" "$SURVEY\survey.txt "

PS3="please select a file to analyze with a panel: "

cd "$SAMPLE"
select file1 in $(cat samples.txt)
do	[ "$file1" != "" ] && break
done
      printf "FILE is: ${file1} and will be used for annotation\n"
      read -r -p "Is this correct? [y/N] " response
if [[ $response =~ ^[nN][oO]?$ ]]
then
echo 'please try again'  && return
    printf "\n\n"
    printf "What is the id of the CAP sample getting gene annotation : "; read id
	printf "Please enter the variant(s), the following are examples"
	echo " c.274G>T or c.274-10G>T or c.*18_*19delGCinsAA"
	
	printf "and please use a comma between multiple: "; IFS="," read -a variant
        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf ${CODING}:%s\n ${variant[$i]} >> /home/cmccabe/cap/TGS/out.txt
        done
}

It probably means what it says, that you have unmatched quotes somewhere.

Since you don't have unmatched quotes in this section, it must be in the parts you didn't post.

I don't think this is related to your error, but this:

        for ((i=0; i<${#variant[@]}; i++))
              do printf ${CODING}:%s\n ${variant[$i]} >> /home/cmccabe/cap/TGS/out.txt
        done

should be this:

        for ((i=0; i<${#variant[@]}; i++))
              do printf "${CODING}:%s\n" "${variant[$i]}" 
        done >> /home/cmccabe/cap/TGS/out.txt

...unless you intended ${variant[]} to split, that is.

1 Like
menu() {
    while true
    do
        printf "\n Welcome to target gene annotation and mutalyzer (v1), please make a selection from the MENU \n
        ==================================
        \t 1  GJB2 analysis
        \t 2  MECP2 analysis
        \t 3  Phox2B analysis
        \t 4  CAP annotation
        \t 5  Non-target gene
        \t 6  Syntax checker
        \t 7  Name checker
        \t 8  Position converter
        ==================================\n\n"

        printf "\t Your choice: "; read menu_choice

        case "$menu_choice" in
        1) gjb2 ;;
        2) mecp2 ;;
        3) phox2b ;;
        4) cap ;;
        5) non ;;
        6) syntax ;;
        7) name ;;
        8) position ;;
        *) printf "\n Invalid choice."; sleep 2 ;;
        esac
    done
}

cap() {
CODING=/home/cmccabe/cap/TGS/NM.txt
SAMPLE=/home/cmccabe/cap/TGS
SURVEY=/home/cmccabe/cap/TGS

printf "The survey being used is and here are the details:" "$SURVEY\survey.txt "

PS3="please select a file to analyze with a panel: "

cd "$SAMPLE"
select file1 in $(cat samples.txt)
do	[ "$file1" != "" ] && break
done
      printf "FILE is: ${file1} and will be used for annotation\n"
      read -r -p "Is this correct? [y/N] " response
if [[ $response =~ ^[nN][oO]?$ ]]
then
echo 'please try again'  && return
    printf "\n\n"
    printf "What is the id of the CAP sample getting gene annotation : "; read id
	printf "Please enter the variant(s), the following are examples"
	echo " c.274G>T or c.274-10G>T or c.*18_*19delGCinsAA"
	
	printf "and please use a comma between multiple: "; IFS="," read -a variant
        
        [ -z "$id" ] && printf "\n No ID supplied. Leaving match function." && sleep 2 && return
        [ "$id" = "end" ] && printf "\n Leaving match function." && sleep 2 && return

        for ((i=0; i<${#variant[@]}; i++))
              do printf "${CODING}:%s\n" "${variant[$i]}" 
        done >> /home/cmccabe/cap/TGS/out.txt
}

# actual start of this program
menu # run menu function
/home/cmccabe/Desktop/annovar.sh: line 65: syntax error near unexpected token `}'
/home/cmccabe/Desktop/annovar.sh: line 65: `}'

I mispoke on the error message, but did test the menu call and that executes perfectly. Thank you :).

Also, you were correct ... ${variant[]} is not intended to split.

I don't see a matchig fi for if [[ $response =~ ^[nN][oO]?$ ]]

1 Like

That was it... the program does execute but do printf cat "$CODING"/samples.txt:"%s\n" "${variant[$i]}" prints cat to /home/cmccabe/cap/TGS/out.txt . If the user inputs c.200G>A should be:

/home/cmccabe/cap/TGS/out.txt

NM_00012.3:c.200G>A

contents of $CODING

NM_00012.3

I'm can't seem to get the syntax correct.

Thank you :).

Hi.

$ shellcheck z10

In z10 line 1:
cap() {
      ^-- SC1009: The mentioned parser error was in this brace group.


In z10 line 16:
if [[ $response =~ ^[nN][oO]?$ ]]
^-- SC1046: Couldn't find 'fi' for this 'if'.
^-- SC1073: Couldn't parse this if expression.


In z10 line 32:
}
^-- SC1047: Expected 'fi' matching previously mentioned 'if'.
 ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

Some information on shellcheck :

shellcheck      analyse shell scripts (man)
Path    : /usr/bin/shellcheck
Version : ShellCheck - shell script analysis tool
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with -h
Repo    : Debian 8.7 (jessie) 
Home    : http://hackage.haskell.org/package/ShellCheck

Best wishes ... cheers, drl

The script does execute just that line seems to output the incorrect output. I will make the changes to check as well. Thank you :).