LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple c-shell question (https://www.linuxquestions.org/questions/programming-9/simple-c-shell-question-337746/)

mathias1979 06-27-2005 03:19 PM

simple c-shell question
 
Two questions really, and they both probably have simple solutions, but I can't seem to find one anywhere. I'm trying to write a simple c-shell script, and here what I need:

1) Is there a way to search a variable for a string of characters and return a simple true/false depending on whether or not the string was found. So I have

set var = "randomtext"

and I want to search 'var' for the string "random", and have the system return something to indicate whether it was found or not. How can I do this?


2) I want to be able to search for empty variables using an if statement...can this be done? If I do this:

set var =
if ($var == "") echo "true"

This works fine. If there is something actually assigned to 'var', then the system just returns

if: Expression Syntax

So how can I get such an if statement to work whether or not 'var' is blank?


-Matt

Dark_Helmet 06-27-2005 04:51 PM

I'll combine answers for both in one example:
Code:

#!/bin/csh

set var = "randomtext"

set grep_result = `echo $var | grep "random"`

if ( "${grep_result}x" == "x" ) then
  echo "false"
else
  echo "true"
endif



All times are GMT -5. The time now is 04:38 PM.