Command not found, but using function in bash

In the bash below, if the answer is "y" then goto function remove. If the answer is "n" then goto the id variable line (where the date is inputted). However, I am getting command remove not found, but remove is a function not an command. I must have the syntax incorrect? Thank you :).

#!/bin/bash
# README(v1) 12_16_2015

echo " Welcome to NGS module (v1) for medical exome analysis, the following steps will be performed
        ==================================
        1  remove @PG tag
        2  run picard markduplicates
        3  run picard HSmeterics
        4  run bedTools coverageBed
        5  detect bases less than 30 reads
        6  combine files
        =================================="
        
printf "\n\n"
printf "What is the date to analyze : "; read id
    date=$(date -d "$id" "+%m-%d-%Y") #convert to month-date-year
    [ -z "$id" ] && printf "\n No date supplied. Leaving match function." && sleep 2 && printf "\n Goodbye! " && exit
        for f in /home/cmccabe/Desktop/NGS/bam/$date/*.bam ; do
        bname=${f##*/}
        pref=${bname%%.bam}
        echo "detected ${pref}.bam in the directory"
done
printf "Are all the files there? "; read match_choice
case "$match_choice" in
        [yY]) remove ;;
        [nN]) printf "Not all the files are there, please find the missing files(s) and try again " && sleep 2 && printf "\n Goodbye! " && exit
        esac
remove() {
for f in /home/cmccabe/Desktop/NGS/bam/$date/*.bam ; do
    bname=`basename $f`
    pref=${bname%%.bam}
    samtools view -H $f | sed '/^@PG/d' | samtools reheader - $f > home/cmccabe/Desktop/NGS/bam/$date/${pref}_newheader.bam
done
}

man bash :

Notabene: LATER!

Your function is not yet known when it is being called.

1 Like
#!/bin/bash
#README(v1.1) 12_16_2015

############ Functions ############

# Enter a comment to why this function was coded.
# Avoid to comment what the function is doing.
# It is more important to know the why that the what.
# Some comment examples are given below, highlighted in blue.
remove() {

    for f in /home/cmccabe/Desktop/NGS/bam/$date/*.bam; do
        bname="${f##*/}"
        pref="${bname%%.bam}"
        samtools view -H "$f" |
                sed '/^@PG/d' |
                samtools reheader - "$f" > "home/cmccabe/Desktop/NGS/bam/${date}/${pref}_newheader.bam"
    done
}

###################################

# It is more proper to end printf with the right amount of new lines
# you want to format, that to add it to the beginning of a string
# in the next printf

printf " Welcome to NGS module (v1) for medical exome analysis, the following steps will be performed
        ==================================
        1  remove @PG tag
        2  run picard markduplicates
        3  run picard HSmeterics
        4  run bedTools coverageBed
        5  detect bases less than 30 reads
        6  combine files
        ==================================\n\n"

# It would be nice to give the user a visual clue
# of how the date format looks like
printf "What is the date to analyze?: "
read id

# bail out if a date is not given
[ -z $id ] && printf "No date was supplied. This program is existing...\n" \
           && sleep 2                                                      \
           && printf "Goodbye!\n"                                          \
           && exit
# now you or others might know that the idea was to bail out if a date was
# not given, but easy a bug since entering any
# character is still accepted as a date.

# dates must be formatted to match directory name form
date=$(date -d "$id" "+%m-%d-%Y")

# user need to visually verify the files outputed and confirm
for f in /home/cmccabe/Desktop/NGS/bam/$date/*.bam; do
    bname="${f##*/}"
    pref="${bname%%.bam}"
    echo "detected ${pref}.bam in the directory"
done

printf "Are all the files there? Please ENTER (Y/N): "
read match_choice

# The user must confirm by entering either Y or y, n or N
# Again, commenting the why gives an idea of the intention for this code,
# making it easy to debug when the user enters a "don't know" or just
# presses ENTER.
case "$match_choice" in
        [yY]) remove ;;
        [nN]) printf "Not all the files were there."
              printf "Please find the missing files(s) and try again\n"
              sleep 2
              printf "Goodbye!"
              exit ;;
esac
1 Like

Thank you very much for the great suggestions and improvements :).