You are not logged in.

#1 2015-04-02 21:43:26

thriftyb
Member
Registered: 2014-12-19
Posts: 12

[SOLVED] How To Break Up Long Statement In Bash Script?

Hello Everyone,

  If have the following If statement in a script:

if [[ $1 =~ ^[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}\:[A-Fa-f0-9]{4,4}$ ]]; then

Is there any way to break this statement into multiple lines? I tried entering a \ after a portion of the statement and then pressing Enter to add a newline but it did not work.


thriftyb

Last edited by thriftyb (2015-04-03 21:44:13)

Offline

#2 2015-04-02 21:45:05

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

WTF. Use grep -P or something. Or better, tell us what you're trying to do.

http://mywiki.wooledge.org/XyProblem


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#3 2015-04-02 21:51:57

nullified
Member
From: Massachusetts, USA
Registered: 2013-12-09
Posts: 468

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

The backslashes should work: http://stackoverflow.com/questions/1859 … -statement

as the answer points out, make sure you have no whitespace after the backslash.


"We may say most aptly, that the Analytical Engine weaves algebraical patterns just as the Jacquard-loom weaves flowers and leaves." - Ada Lovelace

Offline

#4 2015-04-02 22:05:26

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

You could at least do

if [[ $1 =~ ^([A-Fa-f0-9]{4,4}\:){7,7}[A-Fa-f0-9]{4,4}$ ]]; then

and then maybe put the duplicate pattern in a variable with a descriptive name:

hextet="[A-Fa-f0-9]{4,4}"
if [[ $1 =~ ^($hextet\:){7,7}$hextet$ ]]; then

Offline

#5 2015-04-02 22:44:44

thriftyb
Member
Registered: 2014-12-19
Posts: 12

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

@Alad: This statement tests for a valid IPv6, testing each part of the entered address to make sure it is a group of four hexadecimal digits with each group being separated by a colon.

@Rayman: Cool, I think that your method will work.


thriftyb

Offline

#6 2015-04-02 23:56:24

Alad
Wiki Admin/IRC Op
From: Bagelstan
Registered: 2014-05-04
Posts: 2,418
Website

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

if grep -xP '(([A-Fa-f0-9]{4}):){7}(?2)' <<< "$1"; then

http://www.regular-expressions.info/subroutine.html

Last edited by Alad (2015-04-03 00:01:49)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby

Offline

#7 2015-04-03 03:32:11

thriftyb
Member
Registered: 2014-12-19
Posts: 12

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

Alad wrote:
if grep -xP '(([A-Fa-f0-9]{4}):){7}(?2)' <<< "$1"; then

http://www.regular-expressions.info/subroutine.html

That works too. I didn't know that grep could be used that way. Thanks.

Offline

#8 2015-04-03 12:35:05

fsckd
Forum Fellow
Registered: 2009-06-15
Posts: 4,173

Re: [SOLVED] How To Break Up Long Statement In Bash Script?

That's what grep is for. Think what one would do in an old shell which has no builtin regex operator. smile


aur S & M :: forum rules :: Community Ethos
Resources for Women, POC, LGBT*, and allies

Offline

Board footer

Powered by FluxBB