>>viabc.log↓
10
20
30
40
50
The contents of abc.log are as above.
NG if 40 or 50 is included,
Otherwise return "Ok" as a result,
Could you tell me how to create a shell program (description method)?
For Csh, the termination status of the previous command can be found in $status
($?
can also be used in tcsh).
The if
statement allows you to compare numbers in a C-language way.
#!/bin/csh-f
grep-qE'^(40|50)$'abc.log
if($status==0)then
# There were forty or fifty lines.
echo "NG"
else
# There were no lines of forty or fifty.
echo "OK"
endif
You can also execute the enclosed command with {}
where the expression can be written, such as the if
statement.
The value in this part is 1
if the command exit status is successful (0), otherwise 0
.
Therefore, you can write grep
and if
above.
if({grep-qE'^(40|50)$'abc.log})then
Note:
csh(1)-Linux man page
Manpage of TCSH-JM Project
Oira has the same opinion as Sakuro, so I will try to write the answer in the balloon shell.
I'll write down the thought process for someone's reference.
You can immediately think of using grep
to find 40
or 50
in the file.
What I want to do this time is search for OR, but how do I search for OR in grep
?
Try mangrep
first
https://linuxjm.osdn.jp/html/GNU_grep/man1/grep.1.html
Then you can read all of the following from this man page
- You can specify -e
multiple times for OR searches.
- The exit code is
0 when found, 1 if not found
- I don't need 40
or 50
echo back this time, but it can be suppressed by →-q
and -s
So you can simply use if
.
The then
section of if
starts with the 0
exit code.
$if grep-s-q-e40-e50abc.log;the echo NG;else echo Ok;fi
Therefore, if you summarize the omission of consideration that is not presented in the specification, your boss won't complain.
450
or 540
or 5040
are included, what is Ok
?Or NG
?-40
or -50
?40
in so-called full-width characters?The rest is waiting for the instructions of the boss/former speaker.
Thank you all for letting me know.
The abc.log does not contain strings of 角full-width 」 or マイナスminus 」 characters.
Therefore,
in "if grep-s-q-e 40-e 50 abc.log;the echo NG; else echo Ok;fi"
This time, I solved it.
Sorry for the late reply.
Another explanation.
Read each line, and if the content is 40
or 50
, ng
otherwise OK
is displayed for each line, and is implemented in the balloon shell.
In the balloon shell, if you search for read
to read one line from the standard input, or while read
to repeat it to EOF, it will be a hit immediately.If you've written a shell script several times, you know that you can compare strings with the OR
operator of Bourne shell's built-in command test
, so what do you do with OR
?This can also be subtracted from man
and is easily searchable.
$while read x; if [$x=40-o$x=50]; then echo NG; else echo Ok; fi; done
I think I can write it down fairly quickly until (this is a bad example).
Also, it is good to give various input to this.If you're familiar with debugging shell scripts, you know that giving "string with spaces" or "string with shell glob characters" often results in errors.Therefore, try giving ab
or *
as the reading input for read
.
-bash:[: Too many arguments
Wow, they complained.So echo
is useful to find out what the problem is.All I want to debug now is around the test
expression, so I'll try it like this.
$whilereadx; echo[$x=40-o$x=50]; done
a
[a=40-oa=50]
ab
[ab=40-oab=50]
*
[a.out baz.cbaz.h foo.cpp foo.h=40-o a.out baz.cbaz.h foo.cpp foo.h=50]
$
If you know that $x
has been deployed, you can simply double-quote it to avoid unnecessary deployments of $x
.
$ while read x; if [ "$x" = "40" -o "$x" = "50" ]; then echo NG; else echo Ok; fi; done
When you double-quote the left side, the trick is to double-quote the right side like "40"
or "50"
.
Also, if you decide whether to choose read
or read-r
because you should accept the delimiter as an opportunity, and if you specify the redirect at the end, it will be sufficient
$catabc.log | while read-rx; if ["$x" = "40" -o "$x" = "50" ]; then echo NG; else echo Ok; fi; done
Or
$while read-rx; if ["$x" = "40" -o "$x" = "50" ]; then echo NG; else echo Ok; fi; done<abc.log
Become a one-liner as shown in .
The other answer is the same, so that is omitted.Please be aware that the difference in behavior between test
and grep
will result in different results for 450
.
Oira has no intention of writing a csh
script, so if it really has to be csh
, leave the rest to someone.
© 2024 OneMinuteCode. All rights reserved.