我在外殼寫這個小程序:殼牌程序代碼約:正則表達式和文件處理
#!/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
我有一些問題:
我)當我這樣做:
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
我想打印其他的東西,因此,這是,打印正確的消息。例如:如果「$ outputfileNOM」沒有寫入權限,只需打印該錯誤。但是,我不希望把大量的if/else語句,例如:
if [[ -r $inputfile ]]; then
[...]
if [[-w $outputfileNOM ]] then
[...]
else
For the READ permission, and the other else for the WRITE
有沒有辦法做到這一點,利用嵌套的方式沒有,並維持可讀性。
II)關於:
if [[ -r $inputfile && -w $outputfileNOM && -w $outputfilePGP ]]
是確定的,如果我使用的標誌,而不是 「-x」 -r或-w。我沒有一個清晰的定義是什麼意思:
-x FILE
FILE exists and execute (or search) permission is granted
三)注意我的代碼中的注意標籤。我注意到有一些可能性,例如:在之前,之後或之前或之後有空格。我相信輸入文件的一致性,但如果它們改變了,它會爆炸。我能在這種情況下做什麼?有沒有一種優雅的方式來管理它? (例外?)
非常感謝!
這有什麼特別的理由寫在sh?你會得到一個更清潔的Perl或Python程序。 –
@Rafe:是的,我沒有選擇,是要寫在shell中的要求。 – Kani
順便說一句 - 如果你準備了一個精簡版的代碼 - 你可能會得到更多的關注 - 就足以展現不良行爲。我甚至沒有看過這個,只是它絆倒了一個非常令人沮喪的記憶。 – dmckee