Create action to display warning and exit when true is received in shell script if branch

Asked 1 years ago, Updated 1 years ago, 363 views

I would like to check the contents of the hoge file and decide whether to finish or not.

·Procedure for the confirmation process you want to create

Determining if the first line of the 22-24 line of the hoge file is preceded by a #

  • true
  • if # is at the beginning
  • If there is no #, false (in this case, proceed to step 2)

Next, check if there is a duplicate ID for each group (there is a duplicate t44444 in the ccccc group at line 24 of hoge below)

  • False
  • if there is an ID to add and if it is not true
  • If duplicate IDs are not true, false

I want to create a foo file with the termination process if step 1.2 fails.
The foo file has only been verified in step 1.
I would like to create an if branch that will receive the result of 1.2 in boolean and end the warning.
I would appreciate it if you could give me advice on how to handle step 2 together!!

Contents of the target hoge file

#Assume group name = ID, ID, ID...
Line 22 #aaaaaa=txxxxx,txxxxx
Line 23 bbbbb=t11111,t2222
Line 24 ccccccc=t44444, t55555, t44444

The contents of the foo file to perform the verification process

aaaaa=$(cathoge|head-22|tail-1)
bbbbb=$ (cathoge | head-23 | tail-1)
cccccc = $ (catch | head-24 | tail-1)

# Determining if the line begins with or does not have a #
if ["`echo$aaaa|grep^#`";then
  echo 'Yes'
else
  echo 'None'
fi

if ["`echo$bbbbb|grep^#`"];
  echo 'Yes'
else
  echo 'None'
fi

if ["`echo$cccccc|grep^#`";then
  echo 'Yes'
else
  echo 'None'
fi

The current foo file is only processed by determining whether it has a # at the beginning of the line or no.

------- "What do you want to do" below ----

  • Display warning if # is at the beginning
  • Display warning if there is no duplication (if false) to end the process

Create an if branch where the warning end pattern becomes true after receiving the result of 1.2 in boolean
→ Do the warning, exit above
→ Do nothing about the pattern that continues the follow-up processing (just catch the warning pattern that should not continue)

That's all.
I would appreciate your advice!
Thank you for your cooperation.

shellscript

2022-09-30 21:54

1 Answers

------- "What do you want to do" below ----

  • Display warning if # is at the beginning
  • Display warning if there is no duplication (if false) to end the process

The following scripts utilize bash-specific features.

An additional binary operator, =~, is available, with the same precedence as == and!=.When it is used, the string to the right of the operator is committed a POSIX extended regular expression and matched accodingly (as in regex(3)). The return value is 0 if the matching.

  • ${l/#*=/}:pattern substitution

${parameter/pattern/string}.The pattern is expanded to produce a pattern just as in pathname expansion, Parameter is expanded and the longest match of pattern agreements value is replaced with string.

#!/bin/bash

mapfile-tarr<(sed-n'22,24p'data.txt)

for line "${arr[@]}"
do
  [[[[$l"=~^#]]&
    {
      printf "'#' is found at starting of '$l'\n" > 2
      continue
    }

  echo${l/#*=/}|tr, '\n'| sort | uniq-d2>/dev/null | grep-Eq'.'||
    {
      printf "None duplication in '$l'.\n" > 2
      continue
    }

  printf "Passed: '$l'\n"

done

# execution result
'#' is found at starting of '#aaaaa=txxxxx,txxxx'
None duplication in 'bbbbbb=t11111, t22222.
Passed: 'ccccccc=t44444, t55555, t44444'


2022-09-30 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.