【发布时间】:2011-10-10 03:43:38
【问题描述】:
我正在用 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) 当我这样做时:
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
我想打印 else 上的东西,因此,这是打印正确的消息。例如:如果“$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
III) 注意我的代码中的 ATTENTION 标签。我注意到有一些可能性,例如:在之前、之后或之前或之后有空格。我相信输入文件的一致性,但如果它们发生变化,它就会爆炸。在这种情况下我能做什么?有没有一种优雅的方式来管理它? (例外?)
非常感谢!
【问题讨论】:
-
这是用sh写的有什么特别的原因吗?你会在 Perl 或 Python 中得到一个更简洁的程序。
-
@Rafe: 是的,我别无选择,必须用 shell 编写。
-
顺便说一句——如果你准备了一个精简版的代码,你可能会受到更多的关注——刚好足以展示错误行为。我什至不会看这个,除非它让我非常沮丧。
-
@dmckee:对不起,如果代码看起来很长,但实际上不是,也许我过度记录了它。不过,我认为我在[I]、[II]和[III]中写了清晰简洁的问题。对于我的下一个问题,我将尝试更紧凑。谢谢。
-
Kani,你不应该在“问题”中有三个不同的问题,我试图回答你选择的标题中提到的问题。剩下的我就不看了。只需要花费更多的时间来弄清楚您的要求,而不是我愿意投资。我怀疑其他潜在的帮助者也在同一条船上。