I'm writing this little program in shell:
#!/bin/bash
#***************************************************************
# Synopsis:
# Read from an inputfile each line, which has the following format:
#
# llnnn nnnnnnnnnnnnllll STRING lnnnlll n nnnn nnnnnnnnn nnnnnnnnnnnnnnnnnnnn ll ll
#
# where:
# n is a <positive int>
# l is a <char> (no special chars)
# the last set of ll ll could be:
# - NV
# - PV
#
# Ex:
# AVO01 000060229651AVON FOOD OF ARKHAM C A S060GER 0 1110 000000022 00031433680006534689 NV PV
#
# The program should check, for each line of the file, the following:
# I) If the nnn of character llnnn (beggining the line) is numeric,
# this is, <int>
# II) If the character ll ll is NV (just one set of ll) then
# copy that line in an outputfile, and add one to a counter.
# III) If the character ll ll is NP (just one set of ll) then
# copy that line in an outputfile, and add one to a counter.
#
# NOTICE: could be just one ll. Ex: [...] NV [...]
# [...] PV [...]
# or both Ex: [...] NV PV [...]
#
#
# Execution (after generating the executable):
# ./ inputfile outputfileNOM outputfilePGP
#***************************************************************
# Check the number of arguments that could be passed.
if [[ ${#@} != 3 ]]; then
echo "Error...must be: myShellprogram <inputfile> <outputfileNOM> <outputfilePGP>\n"
exit
fi
#Inputfile: is in position 1 on the ARGS
inputfile=$1
#OutputfileNOM: is in position 2 on the ARGS
outputfileNOM=$2
#OutputfilePGP: is in position 3 on the ARGS
outputfilePGP=$3
#Main variables. Change if needed.
# Flags the could appear in the <inputfile>
#
# ATTENTION!!!: notice that there is a white space
# before the characters, this is important when using
# the regular expression in the conditional:
# if [[ $line =~ $NOM ]]; then [...]
#
# If the white space is NOT there it would match things like:
# ABCNV ... which is wrong!!
NOM=" NV"
PGP=" PV"
#Counters of ocurrences
countNOM=0;
countPGP=0;
#Check if the files exists and have the write/read permissions
if [[ -r $inputfile && -w $outputfileNOM && -w $outputfilePGP ]]; then
#Read all the lines of the file.
while read -r line
do
code=${line:3:2} #Store the code (the nnn) of the "llnnn" char set of the inputfile
#Check if the code is numeric
if [[ $code =~ ^[0-9]+$ ]] ; then
#Check if the actual line has the NOM flag
if [[ $line =~ $NOM ]]; then
echo "$line" >> "$outputfileNOM"
(( ++countNOM ))
fi
#Check if the actual line has the PGP flag
if [[ $line =~ $PGP ]]; then
echo "$line" >> "$outputfilePGP"
(( ++countPGP ))
fi
else
echo "$code is not numeric"
exit
fi
done < "$inputfile"
echo "COUN NON $countNOM"
echo "COUN PGP $countPGP"
else
echo "FILE: $inputfile does not exist or does not have read permissions"
echo "FILE: $outputfileNOM does not exist or does not have write permissions"
echo "FILE: $outputfilePGP does not exist or does not have write permissions"
fi
I have some questions:
I) When I do:
if [[ -r $inputfile && -w $outputfileNOM && -w $outputfilePGP ]]; then
[...]
else
echo "FILE: $inputfile does not exist or does not have read permissions"
echo "FILE: $outputfileNOM does not exist or does not have write permissions"
echo "FILE: $outputfilePGP does not exist or does not have write permissions"
fi
I would like to print the things on the else, accordingly, this is, print the right message. Ex: if "$outputfileNOM" did not have the write permission, just print that error. BUT, I don't want to put a lot of if/else, Ex:
if [[ -r $inputfile ]]; then
[...]
if [[-w $outputfileNOM ]] then
[...]
else
For the READ permission, and the other else for the WRITE
Is there a way to do it, without using a nesting approach, and that maintains the readability.
II) About the:
if [[ -r $inputfile && -w $outputfileNOM && -w $outputfilePGP ]]
is OK if I use the flag "-x" instead of -r or -w. I don't have a clear definition of what is the meaning of:
-x FILE
FILE exists and execute (or search) permission is granted
III) Notice the ATTENTION label in my code. I notice that there are some possibilities, for ex: having white spaces before, after or before or after. I'm believing in the consistency of the input files, but if they change, it will explode. What could I do in this case? Is there an elegant way to manage it? (exceptions?)
Thank you very much!