Bash function using variable in it syntax error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash function using variable in it syntax error
# 1  
Old 03-23-2017
Bash function using variable in it syntax error

The below bash function uses multiple variables CODING, SAMPLE, SURVEY, andvariant
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 Smilie.

samples.txt in $SAMPLE
Code:
test-01
test-02
test-03
test-04

$SURVEY format
Code:
Survey id
name
details

$CODING format
Code:
NM_000012.3

Code:
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
}


Last edited by cmccabe; 03-23-2017 at 01:39 PM.. Reason: fixed format and added details
# 2  
Old 03-23-2017
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:

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

should be this:

Code:
        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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-23-2017
Code:
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 Smilie.

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

Last edited by cmccabe; 03-23-2017 at 02:10 PM.. Reason: added comment
# 4  
Old 03-23-2017
I don't see a matchig fi for if [[ $response =~ ^[nN][oO]?$ ]]
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 03-23-2017
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
Code:
NM_00012.3:c.200G>A

contents of $CODING
Code:
NM_00012.3

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

Thank you Smilie.

Last edited by vgersh99; 03-23-2017 at 05:06 PM..
# 6  
Old 03-23-2017
Hi.
Code:
$ 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:
Code:
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
# 7  
Old 03-23-2017
The script does execute just that line seems to output the incorrect output. I will make the changes to check as well. Thank you Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Syntax error in subtraction in Bash

I am sharing a code snippet. for (( i=0; i<=$(( $count -1 )); i++ )) do first=${barr2} search=${barr1} echo $first echo "loop begins" for (( j=0; j<=5000; j++ )) do if } == $search ]]; then echo $j break; fi done second=${harr2} echo $second (2 Replies)
Discussion started by: ngabrani
2 Replies

2. Shell Programming and Scripting

-bash: syntax error near unexpected token `('

// AIX 6.1 I am getting a syntax error below. Please advise what to be corrected. :confused: runmqsc CERN.$(echo `hostname` | cut -d'.' -f1 | tr '' '').$(echo $environment | tr '' '') <<! | egrep -i '(FROM.NC.APPLIANCE)' | sort -u |awk '{print $2}' | cut -d '(' -f2 | cut -d ')' -f1 |... (1 Reply)
Discussion started by: Daniel Gate
1 Replies

3. Shell Programming and Scripting

Bash syntax error

while read line do mkdir $line scp -r Docking_results/docking_$line.pdb $line/ cd /$line/ set a=`grep ENDMDL docking_'$line'.pdb | wc -l` set b=`expr $a - 2` csplit -k -s -n 3 -f docking_'$line'. docking'$line'.pdb '/^ENDMDL/+1' '{'$b'}' foreach f (... (4 Replies)
Discussion started by: chrisjorg
4 Replies

4. Shell Programming and Scripting

Function works, trigger causes syntax error, whats my fault??

Needing a hint. Creating that function called Meter my simple script works well. What I want now is to start the last four commented lines to include or trigger a reaction, if there are more than n lines in that .txt-file it shall display that message for example. But the interpreter says there is... (3 Replies)
Discussion started by: 1in10
3 Replies

5. Shell Programming and Scripting

BASH: variable and function scope and subscripts

Hi, I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest: I'm... (6 Replies)
Discussion started by: rse
6 Replies

6. Shell Programming and Scripting

Variable syntax error in $?

hi all , i just tried to take the status of previous command inside the script using echo $?. It throws me a variable syntax error , but when i use echo $? as an individual command it works perfectly . can anyone Please tell me why am getting a variable syntax error when i use echo $?... (7 Replies)
Discussion started by: Rahul619
7 Replies

7. Shell Programming and Scripting

Syntax Error while using CMP function

Hi All, I am getting a syntax error message while trying to compare 2 files using the compare function (LINUX) command substitution: line 79: syntax error near unexpected token `(' command substitution: line 79: `cmp -s <(tr , \n < $COMMON_TMP/nt_per_gs.done | sort) <(tr , \n <... (5 Replies)
Discussion started by: dsfreddie
5 Replies

8. Shell Programming and Scripting

bash syntax error: command not found

I am trying to create a shell that asks the user to enter their name, and compare it to my own by saying we have the same name or saying my name and that they have a nice name too. Here is my script... #!/bin/bash-x echo "Enter your name". read name if then echo "My name is Adam too"... (1 Reply)
Discussion started by: amaxey45
1 Replies

9. Shell Programming and Scripting

BASH Script syntax error

I'm trying to write a simple script that takes all the .tar.gz files in a directory and verifies them by using the gzip -tv command: for zip in *.tar.gz do gzip -tv $zip if ; then #Check return code from tar echo "File ${zip} verified OK." exit... (4 Replies)
Discussion started by: kelldan
4 Replies

10. Shell Programming and Scripting

Bash: how to call function having it's name in variable?

Hello. Looking for a method of modularizing my bash script, I am stuck with such a problem. For example, I have: MODULE_NAME="test" FUNCTION_NAME="run" How do I can a function with name test_run? (4 Replies)
Discussion started by: FractalizeR
4 Replies
Login or Register to Ask a Question