Command not found, but using function in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command not found, but using function in bash
# 1  
Old 12-17-2015
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 Smilie.

Code:
#!/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
}

# 2  
Old 12-17-2015
man bash:
Quote:
FUNCTIONS
A shell function, defined as described above under SHELL GRAMMAR, stores a series of commands for later execution.
Notabene: LATER!

Your function is not yet known when it is being called.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-17-2015
Code:
#!/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


Last edited by Aia; 12-17-2015 at 10:08 PM..
This User Gave Thanks to Aia For This Post:
# 4  
Old 12-18-2015
Thank you very much for the great suggestions and improvements Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Red Hat

-bash: sendmail: command not found

Hello Experts, I have been trying to send a test mail in our linux server with sendmail command.But I am getting command not found error message. -->when I tried whether sendmail installed or not with the command rpm -qa sendmail* I got the below, sendmail-cf-8.14.4-8.el6.noarch... (26 Replies)
Discussion started by: Devipriya Ch
26 Replies

3. Shell Programming and Scripting

Bash script fails with "function: not found" error

Hello everyone, I am having problems figuring this out. This script below is supposed to create a list of file names with their "md5sum", in a file "lib-list.txt" When I run it "sh component-list.sh " I get this:component-list.sh: 4: component-list.sh: function: not found component-list.sh:... (4 Replies)
Discussion started by: joemb
4 Replies

4. Shell Programming and Scripting

bash: <command nm> command not found

I created a script that I need to run from time to time, but get this error message. To get it working again I run this command from time to time: export PATH="$PATH:~/scripts" I put all my automated scripts in the /scripts directory and would like to run my scripts from any directory... (5 Replies)
Discussion started by: catalinawinemxr
5 Replies

5. Shell Programming and Scripting

bash:vi:command not found

I downloaded and installed "Cygwin yesterday onto my PC running Windows XP. When I tried to type "vi" in Cygwin's window, I got the following message bash: vi: Command not found What shud i do inorder to get into vi editor Thanks (10 Replies)
Discussion started by: bobby1015
10 Replies

6. Red Hat

BASH command not found strang behavior

Hi all I am relatively new to linux (specifically red hat). I have installed Fedora 13 on my machine and started playing with the terminal when i found a very strange behavior when typing a command that is not found: the terminal does not prompt me back. In other words, i am logged as root (or... (4 Replies)
Discussion started by: abohmeed
4 Replies

7. Shell Programming and Scripting

rm:command not found in linux Bash shell script

Hi All, Linux lxs3er06 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:58:04 EST 2007 i686 i686 i386 GNU/Linux Issue: While executing shell scripts in bash shell, following error messages are thrown: rm:command not found On doing little investigation, I added '/bin' to $PATH and on doing echo... (9 Replies)
Discussion started by: a1_win
9 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

Running function or command concurrently in a bash script

I currently run a script over a vpnc tunnel to back-up my data to a remote server. However for a number of reasons the tunnel often collapses. If I manually restore the tunnel then the whole thing can continue, but I want to add into my script a section whereby while the transfer is taking place,... (8 Replies)
Discussion started by: dj_bridges
8 Replies

10. UNIX for Dummies Questions & Answers

bash: cron-config: command not found - PLEASE HELP ME!!

Hello All, I have installed Cygwin on Windows XP and working with some Shell scripting work, I have needed to schedule my script using the cornetab and whatever commands such as "cron", "cron-config" are not working in my cygwin shell, I re-installed the Cygwin but the same problem is with... (2 Replies)
Discussion started by: sriya
2 Replies
Login or Register to Ask a Question