4

e.g string = "test test test"

I want after finding any occurance of space in string, it should echo error and exit else process.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
rupali
  • 41
  • 1
  • 2

6 Answers6

3

The case statement is useful in these kind of cases:

case "$string" in 
    *[[:space:]]*) 
        echo "argument contains a space" >&2
        exit 1
        ;; 
esac

Handles leading/trailing spaces.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

There is more than one way to do that; using parameter expansion you could write something like:

if [ "$string" != "${string% *}" ]; then
     echo "$string contains one or more spaces";
fi
marco
  • 4,455
  • 1
  • 23
  • 20
1

For a purely Bash solution:

function assertNoSpaces {
    if [[ "$1" != "${1/ /}" ]]
    then
        echo "YOUR ERROR MESSAGE" >&2
        exit 1
    fi
}

string1="askdjhaaskldjasd"
string2="asjkld askldja skd"

assertNoSpaces "$string1"
assertNoSpaces "$string2" # will trigger error

"${1/ /}" removes any spaces in the input string, and when compared to the original string should be exactly the same if there are not spaces.

Note the quotes around "${1/ /}" - This ensures that leading/trailing spaces are taken into consideration.

To match more than one character, you can use regular expressions to define a pattern to match - "${1/[ \\.]/}".

update

A better approach would be to use in-process expression matching. It will probably be a wee bit faster as no string manipulation is done.

function assertNoSpaces {
    if [[ "$1" =~ '[\. ]' ]]
    then
        echo "YOUR ERROR MESSAGE" >&2
        exit 1
    fi
}

For more details on the =~ operator, see the this page and this chapter in the Advanced Bash Scripting guide.

The operator was introduced in Bash version 3 so watch out if you're using an older version of Bash.

update 2

Regarding question in comments:

how to handle the code if user enter like "asd\" means in double quotes ...can we handle it??

The function given above should work with any string so it would be down to how you get input from your user.

Assuming you're using the read command to get user input, one thing you need to watch out for is that by default backslash is treated as an escape character so it will not behave as you might expect. e.g.

read str              # user enters "abc\"
echo $str             # prints out "abc", not "abc\"
assertNoSpaces "$str" # no error since backslash not in variable

To counter this, use the -r option to treat backslash as a standard character. See read MAN Page for details.

read -r str           # user enters "abc\"
echo $str             # prints out  "abc\"
assertNoSpaces "$str" # triggers error
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
  • this works but it wont remove the spaces at start and end ... If string = " testtest " it doesn't throw error – rupali Dec 15 '10 at 11:52
  • sorry, my bad. Fixed. Quoting it will maintain the spaces around the edges. – Shawn Chin Dec 15 '10 at 11:54
  • it works.. can you explain? also how about \ instead of space? – rupali Dec 15 '10 at 12:05
  • If you want to detect \ instead, use `${1/\\/}`. Or, if you need to detect both, or more than one character, see updated portion on regular expression. – Shawn Chin Dec 15 '10 at 12:22
  • yeah..I have tried it but you know it works for all except e.g string = "test\" can you refine? – rupali Dec 15 '10 at 12:23
  • Let me know which bits you need more explanation on, will update when I get back in an hour. – Shawn Chin Dec 15 '10 at 12:23
  • we can talk tomorrow .. i want to detect "space","backslash \", "dot ." at any place in string and throw error if found... – rupali Dec 15 '10 at 12:26
  • `${1/\\/}` should work for `string='test\'`. You need to watch out when doing `string="asd\"` as `\"` escapes the quotes and you end up with an unterminated string. Use `"asd\\"` or `'asd\'`. – Shawn Chin Dec 15 '10 at 13:35
  • Example updated to include space, backslash and dot. – Shawn Chin Dec 15 '10 at 13:36
  • There can't be a space between the `>` and the `&2` - it should be `>&2`. – Dennis Williamson Dec 15 '10 at 16:45
  • thx for your reply shawn, but i just want to know ...how to handle the code if user enter like "asd\" means in double quotes ...can we handle it?? – rupali Dec 16 '10 at 05:59
  • It should work with any variable, so it's down to how you read input from you user. Updating answer with a little more details. – Shawn Chin Dec 16 '10 at 10:23
1

The == operator inside double brackets can match wildcards.

if [[ $string == *' '* ]]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

You can use grep as:

string="test test test"
if ( echo "$string" | grep -q ' ' ); then
        echo 'var has space'
        exit 1
fi
codaddict
  • 445,704
  • 82
  • 492
  • 529
-1

I just ran into a very similar problem while handling paths. I chose to rely on my shell's parameter expansion rather than looking for a space specifically. It does not detect spaces at the front or the end, though.

function space_exit {
  if [ $# -gt 1 ]
  then
    echo "I cannot handle spaces." 2>&1
    exit 1
  fi
}
Hermann
  • 604
  • 7
  • 23