LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2005, 03:19 PM   #1
mathias1979
LQ Newbie
 
Registered: May 2005
Location: Raleigh, NC
Posts: 7

Rep: Reputation: 0
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
 
Old 06-27-2005, 04:51 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A shell command simple question the_rhino Linux - Newbie 6 09-13-2004 01:57 PM
simple question on shell programming stupid_guy Programming 5 03-10-2004 02:07 PM
probably a simple shell question odious1 Linux - General 6 03-03-2004 10:06 AM
simple shell script question. strifel Programming 7 09-13-2003 01:44 PM
simple question on shell script gsbarry Linux - General 2 05-23-2003 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration