Refactoring via SHELL utilities

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Refactoring via SHELL utilities
# 1  
Old 10-06-2011
Refactoring via SHELL utilities

I am completely new to Shell scripting and I have almost no idea what I'm doing here. I'm not a stranger at all to programming, but the way SHELL is done drives me to insanity. Here's my current issue:

1. We are to create a BASH script called rafactor.sh
This program takes in two (or three) arguments. A Java class name, (without the .java extension) and a "new" Java class name (again with no extension), optionally a directory may be given as an argument.

This script must search each source file for the first argument and replace it with the second, THIS HOWEVER DOES NOT INCLUDE PATTERNS (i.e. arg1:aaa, arg2: AAA will not change: aaaInstance, but will change: aaa).

The directories must be searched recursively (but does not have to be done using "recursive programming")

Additional requirements:

Check that at least two arguments are given
Check that the second argument is "valid" (no numbers, or spaces, just alphanumeric characters and underscores)
The source file name must also be changed if it has the same name as the first argument (i.e. aaa.class changes to: AAA.class)
Instances of the class name within the program must be changed accordingly (if the class name was changed)
The script must recurse through the directory
sed and find must be used.


2. Relevant commands, code, scripts, algorithms:
sed and find must be used.


3. The attempts at a solution (include all code and scripts):
Code:
#!/bin/sh

# rafactor.sh

if (($# == 0));
    then
    echo "Not enough arguments"

exit 0

fi

if (( -z "$3" ));

BAKDIR="./bakup"
TEMPDIR="./temp"

cd $3

sed 's/$1/$2/g'

The above code is obviously no where near complete, it also doesn't have all the attempts I've made to make heads or tails of how to tackle the problem in my terminal.

I created two dummy files and placed them in a folder on my desktop called "Test" and attempted to change every instance of "blah" to "moo" in both files, and I can't even seem to do that.

I cannot get my program to recognize temp and backup folders which are also on my desktop.

The only success I've managed to achieve thus far is a simple sed substitution in a single dummy file from "blah" to "moo" however I cannot restrict it to single instances (as opposed to patterns within the file).

Perhaps I don't have enough knowledge to tackle this problem, we've only been scripting for a couple weeks, and I can't even properly write a factorial program yet...

Things as simple as if statements throw errors if I so much as look at them and I've yet to successfully compile a BASH script on my own.

4. University of Windsor, Windsor(ON),Canada, Prof. Rahaman , (I do not have 5 posts and therefore am unable to post the course URL. I will edit this when I have enough posts) course number 03-62-256


I've tried breaking this down into smaller chunks, but I can't even seem to do that. I've been scouring my notes for the last few hours, trying to get SOMETHING to compile right, but I can't even get that far.Smilie

Addition to help with this problem, if there's any special hints anyone could give me concerning compiling in general, I'd greatly appreciate it.
# 2  
Old 10-06-2011
Code:
if (( -z "$3" ));

You've got an 'if' with no 'fi' and no 'then'. The ; on the end seems to have no reason to be there -- ; is treated like a linebreak, so you can do command1 ; command2 but the line ends right there anyway...

The usual syntax goes

Code:
if STATEMENT
then
        ...
else
        ...
fi

where STATEMENT can be:

Code:
# Run an external program and execute the code inside if it succeeds.
# externals are things like cut, grep, sed, awk, javac, wget, true, false, ...
if program param1 param2 ...
then
        ...
fi

# Run an external program and execute the code inside if it fails.
# in fact, you can put an ! in front of any STATEMENT.
if ! program param1 param2 ...
then
        ...
fi

# Check a logical statement as described by man test.
# -z for instance means 'test if string is empty'.  There's also operators
# available to compare decimal numbers [ 9 -gt 2 ], [ 3 -lt 5 ], [ 9 -eq 9 ]
# compare strings [ "abc" = "abc" ], [ "def" \> "abc" ], [ "abc" \< "def" ] 
# check for the presence of files [ -e "filename.sh" ]
# check for the presence of directories [ -d "dirname" ], and others.
if [ -z "$3" ]
then
        ...
fi

# [[ ]] are extended versions of [ ].  They support a more "C-like" syntax
# with && instead of -a and such, and are faster than [] in some shells,
# but behave much like [ ] otherwise.
if [[ "aaaa" == "bbbbb" ]]
then
        ...
fi

# This only works in BASH and newer KSH
A=3
B=5
if ((B>A))
then
        ...
fi

I'd like to see an example of what an input source file would look like, and what the file ought to look like after substitution.
# 3  
Old 10-06-2011
Thanks Corona, I didn't actually realize that a ";" meant a line break, I thought it was part of the if statement process.

Here's some sample input/output:
Code:
package uwindsor.cs256;
public class aaa {
aaa aaaInstance ; // an instance of aaa
int aaaNum=0 ;
public aaa() {
}
}

output:
Code:
package uwindsor.cs256;
public class AAA {
AAA aaaInstance ; // an instance of AAA
int aaaNum=0 ;
public AAA() {
}
}

I'm going to point out again how the pattern is not changed, but rather explicit instances of the original input is changed.

Also, I know that the example I gave (which is from the assignment) just capitalizes, and that can be done using the "tr" command, but that's not at all what the professor wants.

I also apologize for my lack of an attempt. I'm just that lost. If this were any other programing language I wouldn't even be posting
# 4  
Old 10-06-2011
Quote:
Originally Posted by Okysho
Thanks Corona, I didn't actually realize that a ";" meant a line break, I thought it was part of the if statement process.
You'll sometimes see if statements shortened like if this ; then that ; fi which is probably the source of the confusion.

Quote:
Here's some sample input/output:
Code:
package uwindsor.cs256;
public class aaa {
aaa aaaInstance ; // an instance of aaa
int aaaNum=0 ;
public aaa() {
}
}

output:
Code:
package uwindsor.cs256;
public class AAA {
AAA aaaInstance ; // an instance of AAA
int aaaNum=0 ;
public AAA() {
}
}

I'm going to point out again how the pattern is not changed, but rather explicit instances of the original input is changed.

Also, I know that the example I gave (which is from the assignment) just capitalizes, and that can be done using the "tr" command, but that's not at all what the professor wants.

I also apologize for my lack of an attempt. I'm just that lost. If this were any other programing language I wouldn't even be posting
Hmm.

You can tell it's at least a whole word and not part of another word when the first char is either whitespace, brackets, semicolon, or the beginning of the line; and the last is either whitespace, brackets, semicolon, or the end of the line... This won't protect you from having the name inside strings though:

Code:
$ echo "slartibartfast q;  int aslartibartfast;  slartibartfast b, c;  int d;" | sed 's/\([ ;]\|^\)slartibartfast\([ ;]\|$\)/\1qwerty\2/g'
qwerty q;  int aslartibartfast;  qwerty b, c;  int d;
$

# 5  
Old 10-06-2011
Woah! You lost me there, with your hip, young lingo son! (sorry, bad joke)

I was considering trying to modify the input to include white space, that way it'd be more distinguishable from other strings with the pattern inside it, but even if I wanted to, I can't get anything to compile yet.

Also... could you document your code for me? All I understand is "echo" and I see a few "pipes"
# 6  
Old 10-06-2011
sed is always ugly. it's unavoidable. :/

completely explaining how sed works would be a career, not a post, but I'll explain in brief.

Code:
# Just prints one big long string, and pipes it into sed.
echo "slartibartfast q;  int aslartibartfast;  slartibartfast b, c;  int d;" |
        # sed 's/a/b/g' searches for all a, replaces with b.
        # a can be an entire expression, not just a literal string.
        # Just 's/slartibartfast/qwerty/g' would change our
        # 'aslartibartfast' to 'aqwerty', which is wrong, so we need to check
        # where it is too.
        #
        # [ ;] matches either a semicolon or a space, so
        # [ ;]slartibartfast[ ;] would catch it in the middle of a line, but
        # not the beginning or end.
        #
        # ^ means beginning, $ means end, | means 'or', () group things.  So
        " ([ ;]|^) would mean the beginning of the line or space or semicolon.
        # ([ ;]|^)slartibartfast([ ;]|$) should catch it anywhere in the line.
        #
        # Except if we search-replace that, we're stripping out lots of
        # whitespace and semicolons -- you need those.  You can put back
        # anything you find in () with \1, \2, ... for match 1, match 2 ...
        #
        # So:
        #
        # s/([ ;]|^)slartibartfast([ ;]|)/\1qwerty\2/g'
        # ...tells it to replace ;slartibartfast; with qwerty and put the ;'s back.
        #
        # Except, sed is horrible and won't understand ( | ) unless
        # you put \ in front of them, which makes otherwise-simple expressions
        # capable of blinding small dogs and the elderly.
        #
        #If you have sed -r, you can leave them as-is.
        sed 's/\([ ;]\|^\)slartibartfast\([ ;]\|$\)/\1qwerty\2/g'
# And this is what it prints.  See how it left aslartibartfast alone.
qwerty q;  int aslartibartfast;  qwerty b, c;  int d;
$

# 7  
Old 10-06-2011
Ok. I get it now. The extra "\" makes it a bit overwhelming, but I understand what you've done. That's actually pretty clever and I probably wouldn't have considered it as a solution in any other programing language.

I think it's helped me think of a way to check for validation also. I'll post an updated code and any more problems once I've made the adjustments and have had some thinkin' food.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Crontab utilities

Hi gurus/lead, I have one doubt in crontab. I have shell script source code in one server. where I don't have priviliges acess R/W the shell script in that server. how can I setup the crontab to pick the code from the other unix server ? whether is there any possibility. in my... (1 Reply)
Discussion started by: ramkumar15
1 Replies

2. UNIX for Dummies Questions & Answers

Custom Reporting Utilities for SHell (CRUSH)

If you are interested in a set of utilities designed for reporting solutions within the shell, check out CRUSH on Google code. crush-tools - Project Hosting on Google Code (0 Replies)
Discussion started by: watingo
0 Replies

3. UNIX and Linux Applications

Utilities for CSV

I'd like to be able to do something like 'select column_a column_c column_b from file.csv'. Scripting it probably wouldn't be too extraordinarily difficult but I'd rather not reinvent the wheel if someone's built a better one already. (3 Replies)
Discussion started by: Corona688
3 Replies

4. UNIX for Advanced & Expert Users

utilities

hi experts, i have a file sample: ======= 000123 5 7 0008 00345 5 9 0004 how can i get an output as 123 5 7 8 345 5 9 4 thanks in an advance subhendu (5 Replies)
Discussion started by: subhendu81
5 Replies

5. UNIX for Dummies Questions & Answers

bc and wc utilities???

Hi, Can anybody explain bc and wc system utilities in Unix? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

6. UNIX for Dummies Questions & Answers

top ten utilities in shell scripting?

Let's get some feedback about the top ten ute's you guys use in writing your scripts - I mean yeah it depends on the job and what you're trying to accomplish, but there ARE those commands (sed, grep, awk, cut, etc.) that most will use time and again... ...so, what do you use? (3 Replies)
Discussion started by: diego
3 Replies

7. Shell Programming and Scripting

How to manage multiple versions of a set of shell and SQL script utilities

Hi all --- I have the need to manage multiple versions of a set of utility scripts -- both shell and SQL and other .dat files. I am wondering if anyone out there knows of a good way to "PATH" to SQL and text files in a way similar to how PATH facilitates finding executables in a pre-specified... (2 Replies)
Discussion started by: DennisB
2 Replies

8. UNIX for Dummies Questions & Answers

Zmodem Utilities

Where can I find Zmodem utilities for Soloris 7 ie: rz sz thnx (1 Reply)
Discussion started by: SmartJuniorUnix
1 Replies
Login or Register to Ask a Question