처리하는 정규 표현식 및 파일 :쉘 프로그램 코드에 대해 : 내가 쉘이 작은 프로그램을 쓰고 있어요
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
#!/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
나는 몇 가지 질문이
다른 사람에게 물건을 인쇄하고 싶습니다. 따라서, 올바른 메시지를 인쇄하십시오. 예 : "$ outputfileNOM"에 쓰기 권한이 없으면 해당 오류를 인쇄하십시오.
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 ]]
는 대신 -r 또는 -w "-x"OK입니다. 나는 무엇의 의미인지에 대한 명확한 정의가 없습니다.
3) 내 코드에 ATTENTION 레이블이 있는지 확인하십시오. 예를 들어, 전, 후 또는 전후에 공백이있을 수있는 가능성이 있음을 알았습니다. 나는 입력 파일의 일관성을 믿는다.하지만 파일이 바뀌면 폭발 할 것이다. 이 경우 어떻게해야합니까? 그것을 관리하는 우아한 방법이 있습니까? (예외?)
대단히 고마워요!
이가 쉬에 기록 된 특별한 이유가 있나요? Perl이나 Python으로 훨씬 더 깨끗한 프로그램을 얻을 수 있습니다. –
@Rafe : 예, 선택의 여지가 없습니다. 쉘로 작성해야합니다. – Kani
BTW - 당신이 코드의 버전을 준비했다면, 좀 더주의를 기울일 것입니다. 나는 이것이 매우 좌절스러운 기억을 벗어났다는 것을 제외하고는 이것을 보지 못했을 것이다. – dmckee