LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 02: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 02:39 PM.
 
Old 10-28-2008, 03: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: 227Reputation: 227Reputation: 227
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, 03: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, 03: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: 227Reputation: 227Reputation: 227
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 03:20 PM.
 
Old 10-28-2008, 06:50 PM   #5
IsharaComix
Member
 
Registered: Sep 2008
Location: Raleigh
Distribution: Ubuntu 8.10
Posts: 88
Blog Entries: 1

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

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
 if [ "${test/java/}" != "$test" ];then echo "found"; fi
 
Old 10-28-2008, 09:49 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration