0

I want to find whether a string contains a forward slash using "grep" command. It is easy at the beginning, and I wrote the following script.

foo=someone/books
if [ `echo "$foo" | grep '/'` ]
then
    echo "has forward slash"
fi

however, the problem is in the corner, if I set the variable "foo" to the following string,

foo="someone/books in stack"

The above script will be failed since there is "space" in variable foo, when the command expands, the condition in if statement is as below.

grep '/' someone/books in stack

Because of "space", the above "grep" command has too many arguments that is illegal. FYI, I try to solved this problem using case statement:

case $foo in
    */*)
        echo "has forward slash"
        ;;
    *)
        ;;
esac

However, I do not want to use case statement since it's verbose. So how could I solved this problem using grep command or others?

5
  • actually the problem does not exist the way you describe it - if you pipe a text with spaces into grep, it will work just fine, because it'll never expand it onto greps command line like you described. Verify again that echo "stuff with/ spaces" | grep '/' actually works fine Commented Nov 12, 2012 at 15:35
  • @Yefim Dinitz in fact, I try it. It does not work fine Commented Nov 12, 2012 at 15:47
  • then you must be using the strangest grep I've ever seen... Commented Nov 12, 2012 at 15:49
  • @Yefim Dinitz In fact, if I write this command into script, it will not worked find. however if I write this command in the terminal, it will work find, So why? Commented Nov 12, 2012 at 15:52
  • dogbane spotted your error :) Commented Nov 12, 2012 at 15:57

3 Answers 3

2

You are not using the if-statement correctly. The command in the if-condition needs to be quoted so that it becomes a single string when expanded. Like this:

if [ "`echo "$foo" | grep '/'`" ]
then
    echo "has forward slash"
fi

Or even better is if you check the return code of grep in your if-condition:

if $(echo "$foo" | grep -q '/')
then
    echo "has forward slash"
fi

You can also do away with the grep and use this instead:

foo="someone/books in stack"
if [[ "$foo" == */* ]]
then
  echo "has forward slash"
fi
Sign up to request clarification or add additional context in comments.

1 Comment

I know. But what about /bin/sh rather than /bin/bash
0
foo="someone/books with spaces"
bar=`echo $foo | grep '/'`
if [ $? -eq 0 ]
then
    echo "$foo has forward slash"
fi

This works for me.

If you don't want to resort to using exit status this way, I suggest you take a look at dogbane's reply for a more correct answer.

Comments

0

There's nothing wrong with being verbose -- it often aids maintainability. However, you can terse it up: You don't need every newline, nor do you need the default branch if you do nothing in it.

case "$foo" in
    */*) echo "has forward slash" ;;
esac

If you insist on using grep, use the return status from it and not the output:

if echo "$foo" | grep -q /; then
    echo "has forward slash"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.