littlerunaway
littlerunaway

Reputation: 453

check string for illegal characters

I know this question was asked here quite a few times and I've read all the answers. they're all similar and I've tried all kinds of variations but nothing seems to work right. this is my script file:

#!/bin/bash
names=` cut -d" " -f1-3 $1 `
if [[ "$names" =~ ^[-a-zA-Z]+$ ]] ; then
    echo names are ok
else
echo bad names in file
fi

a legal name is a name with only a-z A-Z or a '-'

$1 is the name of the file, and this is what's in it:

olga geller haifa 56 43
sharon langer haifa 89 31

I basically take all the names (with cut command) and make them into 1 string-> names this variation of the file gives me "bad names in file" although all the names are valid. if I put a number somewhere it prints the same. if I put '!' inside [[]] or right after the "if", it still won't work. it simply prints "names are ok" for valid and not valid names. so where's my mistake?

Upvotes: 2

Views: 915

Answers (1)

l0b0
l0b0

Reputation: 58908

The string with names is separated by newlines, and as such you have to either search for them or read each line and test them individually for matching.

Upvotes: 2

Related Questions