I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters.
How about:
if ! grep -q '[^acgt]' file
then
...
fi
or
if grep -q '[^acgt]' file
then
echo "This file contains non-acgt characters"
exit 1
fi
What Scrutinizer suggested will work if your "file with DNA" is a UNIX formatted text file that contains zero or more lines where each line contains a DNA string of zero or more characters.
If the files contain a single DNA string with no terminating <newline> character (which is what is specified in post #1), you'll need something more like:
#!/bin/bash
IAm=${0##*/}
tmpfile="$IAm.$$"
trap 'rm -f "$tmpfile"' 0
if ! tr -d 'acgt' < "$1" > "$tmpfile" || [ -s "$tmpfile" ]
then printf 'File "%s" is not a DNA file. Exiting.\n' "$1" >&2
exit 1
fi
printf 'File "%s" is a DNA file. Continuing...\n' "$1"
# Do whatever you want to do with the specified DNA file...
This script requires one operand that is a pathname of the file you want to test.
Fair enough. Just out of curiosity: wouldn't something like:
(cat /file/in/question - < printf '\n') | ...
suffice? This would either add an empty line (which would be irrelevant) or terminate the last line properly. No?
bakunin