bash - regex and grep match only string with valid port in an if loop -
I am trying to check in a bass script if I am getting a curl response then give me a valid IANA port If it is, then I want to break the loop and continue. If this does not give me a port, then I want to delete it and try again after a sleepy period.
My problem is grep and regex query. I'm still learning regex and not sure if I got this right. Can anyone verify it. (Or offer an easy solution to the same problem.)
port_forwarding_assignment = / home / user / port_forwarding_assignment.txt while [! -f "$ port_forwarding_assignment"]; Sudo Curl- "Post_datastast" Https: // website / port_forward_assignment -o "/home/user/port_forwarding_assignment.txt" if [grep -q '^ port (6553 [0-5] | 655 [0-2] \ d | 65 [0-4] \ D \ d | 6 [0-4] \ d {3} | [1-5] \ d {4} | [1- 9] \ d {0,3} | 0) $ '' $ port_forwarding_assignment]] ; Then fire RM-F / Home / User / Port / Forwarding_signment. End TST 30
< Some comments on P> code: < / P>
-
[
Characters in bash are actually short stories for the builtintest
command, then you should have a place after this. Code> if [... . - but
if
tests the return code of your logic, then you actually do not[grep < / Code>. Instead, simply type:
to test the return code from grep -q ...
grep
. - To use the repetition in grep, you need to use either
-E
or\ {... <}
( Orfor example using
short forgrep -E
). - Output to a file but then you have to handle that case which gives
curl
an error, possibly withPIPESTATUS
. - Not sure why you
sudo
for this
Write a rewrite according to the first 3 comments:
port_forwarding_assignment = / home / user / port_forwarding_assignment.txt while [! -f "$ port_forwarding_assignment"]; Do sudo curl -d "post_data_stuff" https: // website / port_forward_assignment -o "/home/user/port_forwarding_assignment.txt" if grep -qE 'port (6553 [0-5] | 655 [0-2] \ D | 65 [0-4] \ d \ d | 6 [0-4] \ d {3} | [1-5] \ d {4} | [1-9] \ d {0.3} | 0) $ '' $ Port_forwarding_assignment "; Again fire RM-F / Home / User / Port / Forwarding_signment. End test 30
Since you are not seeing the return code
return code, you can also make changes according to my 4th comment all five comments The following code has been applied:
port_forwarding_assignment = / home / user / port_forwarding_assignment.txt while! Curl-d "post_data_stoff" https: // website / port_favourword_sagmentement | T "$ port_forwarding_assignment" | Grep -qE 'port (6553 [0-5] | 655 [0-2] \ d | 65 [0-4] \ d \ d | 6 [0-4] \ d {3} | [1-5] \ D {4} | [1- 9] \ d {0,3} | $) $ 'Dale Sleep 30 has been completed
Comments
Post a Comment