LinuxQuestions.org
Visit Jeremy's Blog.
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 10-28-2008, 01:35 PM   #1
IsharaComix
Member
 
Registered: Sep 2008
Location: Raleigh
Distribution: Ubuntu 8.10
Posts: 88
Blog Entries: 1

Rep: Reputation: 15
Want to compare strings in bash script


I'm trying to make an automated compiler to invoke gcj to compile classes in folders recursively. It works OK now, but it spits out these ugly error messages when it hits non-.java files and ignores them.

Is there a way I can run an if string contains substring "foo", do this in Bash?

compilejava.sh
Code:
#! /bin/bash
#
# $1 is the directory
# $2 is the classpath

function runprocess
{
for myfilename in $1/* ;
do
	if test -d $myfilename ; then
		echo "Entering $myfilename/"
		runprocess $myfilename $2
	elif $myfilename contains ".java" ; then
		echo "Compiling $myfilename"
		gcj -C --classpath=$2 $myfilename
	else
		echo "Skipping $myfilename"	
	fi
done

}

runprocess $1 $2
The bolded line of code is the pseudocode for what I want to accomplish.

Any help?

Last edited by IsharaComix; 10-28-2008 at 01:39 PM.
 
Old 10-28-2008, 02:09 PM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Code:
elif $myfilename contains ".java" ; then
For this substitute this :

Code:
elif echo $myfilename | grep -q ".java"; then
Should have the wanted effect.
 
Old 10-28-2008, 02:10 PM   #3
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
In Bash 3.2:

Code:
$ test="somesubstring"
$ if [[ $test =~ "sub" ]] ; then echo "true"; else echo "false"; fi
true
This is the pure Bash method, but dont know if I would recommend using it or not -- depending on the situation.
 
Old 10-28-2008, 02:14 PM   #4
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Thinking about this method 1 may give problems if you have .java in a directory name so try this instead :


Code:
elif echo $(basename "$myfilename") | grep -q ".java"; then

Last edited by bgeddy; 10-28-2008 at 02:20 PM.
 
Old 10-28-2008, 05:50 PM   #5
IsharaComix
Member
 
Registered: Sep 2008
Location: Raleigh
Distribution: Ubuntu 8.10
Posts: 88

Original Poster
Blog Entries: 1

Rep: Reputation: 15
Many thanks! It works like a charm!
 
Old 10-28-2008, 07:58 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
 if [ "${test/java/}" != "$test" ];then echo "found"; fi
 
Old 10-28-2008, 08:49 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
elif [ "${myfilename#*.}" = "java" ]; then
 
  


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
Bash script to compare two files swatward Linux - Newbie 17 04-18-2012 06:14 PM
need help with bash script to compare file sizes rimvydazas Programming 3 04-03-2008 06:18 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM
bash script help (arrays and strings from files) nkoplm Programming 14 12-02-2005 09:50 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 06:08 AM

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

All times are GMT -5. The time now is 09:28 PM.

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