LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   URGENT!!!! Anyone knows how to programme in linux? (https://www.linuxquestions.org/questions/linux-newbie-8/urgent-anyone-knows-how-to-programme-in-linux-4175527133/)

jeffrey1289 12-02-2014 05:50 PM

URGENT!!!! Anyone knows how to programme in linux?
 
I have an assignment that's due on Friday, which ask me to write a programme in linux to format the original source code. I think of it for so long, anyone can help me with it?

Assignment: Write a bash program to format the comments in an assembly language source file.

Conditions:

Comments begin with a ; (semicolon) and end with a newline. The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input. Requirements:

Lines without comments should not be changed. Entire lines which are comments (begin with ;) should not be changed. Lines containing both code and comments (; preceded by other characters) should have spaces inserted before the comments so all comments begin in the same column, at least 4 columns after the end of the longest code. Be sure your program includes all of the following:

comments with your name, the date, and the assignment comments or output with instructions for using the program prompts for any interactive inputs descriptive names and/or comments explaining variables & functions indentation of code blocks comments explaining any non-obvious control flow

The following is the code before and after. We should write a programme which reads a file contains the original code, and turn the original one to the formatted one, then output it on the screen. Please someone help me with it! Thank you!

Source Code: http://50.79.210.49/171/171-asm-14f.xhtml

Habitual 12-02-2014 05:55 PM

Sure, show us what you've done so far, except procrastinate. ;)

jeffrey1289 12-02-2014 05:58 PM

I am having difficulties with calculating each line's length as it reads through, but it must read just up to the semicolon (;), then add the differences between the longest line and the rest to all lines before the semicolon, so to do the format. This is what I have done up to now:
#!/bin/bash

echo "Start!"
echo "Please enter a file name: "
read name
cat $name| while read line; do

dugan 12-02-2014 05:59 PM

Sounds like a job for sed and awk.

The sed and awk chapters in this free book are good:

http://linuxcommand.org/tlcl.php

String manipulation is obviously also useful:

http://www.faqs.org/docs/abs/HTML/st...ipulation.html

jeffrey1289 12-02-2014 06:05 PM

I am confuse of how to use those code. I tried it for hours and all got errors

jeffrey1289 12-02-2014 06:29 PM

Can someone help? I am confuse with the codes

dugan 12-02-2014 06:31 PM

What did you try that you thought would work but gave you errors?

jeffrey1289 12-02-2014 06:35 PM

I was trying to cut the lines after the semicolon, and those lines start at the seconde line. The first line is just semicolon, and end with a new line, so all of the contents got deleted.

jeffrey1289 12-02-2014 06:50 PM

I was trying to do display the content that are not begin with a ;(semicolon), after the first line.
I type this:
echo "$line" | grep _C 1 '^[^[;]]'
it doesn't work. WHY?

dugan 12-02-2014 07:06 PM

Your regex is wrong.

Code:

bash-4.1$ line="a                   
;b
c"
bash-4.1$ echo "$line" | grep '^[^;]'
a
c
bash-4.1$


jeffrey1289 12-02-2014 07:14 PM

What is this mean?

grail 12-02-2014 07:17 PM

As it is a bash assignment, I see no direct need to use other tools (not to say you couldn't)

So a few points of housekeeping first, seeing you are new:

1. Please read the LQ rules. Here you will see that no one will do your homework but we are happy to assist

2. The question is only urgent to you and as this is a community site it will not tend to yield you any quicker results

3. Please place all code and data in [code][/code] tags to maintain formatting and make it easier to read


So, on to your question. Like most first time programmers you are jumping in and have as yet not defined what you need your programme to do.

Having looked at the before and after code and reading the brief my thoughts would be:

1. Ignore all lines starting with a semi-colon

2. First pass of the file is needed to find what position is the greatest when it comes to semi-colons after code

3. Second pass would utilise number found in 2 to set all semi-colons after code, ie. add spaces prior to any that do not have semi-colon at desired position

So see if that helps give you some direction

jeffrey1289 12-02-2014 07:29 PM

How can I find the longest line before the semicolon? I tried use length but it just gives me the number, and I can't ignore the ;

jeffrey1289 12-02-2014 08:00 PM

I just tried some new things, but still it can't process what I needed. I define the max as zero already.

[code]
if length "$line" | grep '^[^;]' > max
max=length "$line" | grep '^[^;]'
[code]

John VV 12-02-2014 08:33 PM

Quote:

How can I find the longest line before the semicolon? I tried use length but it just gives me the number, and I can't ignore the ;
what were the examples that were used in the textbook you are using ?
and there "SHOULD" be something like this in a end of the chapter exercise of that text book



remember software is NOT !!!! "route memorization "
it is DEDUCTIVE and symbolic logic
oops i let the cat out of the bag-- it is MATH!!!

How would "Sherlock Homes" solve a problem ?
then the logical steps that Sherlock used to get from point A to point D ?

Quote:

[code]
if length "$line" | grep '^[^;]' > max
max=length "$line" | grep '^[^;]'
[code]
you missed the close tag
in software
if you OPEN something you need to CLOSE that something
PHP Code:

[code]
the copied 
code
line3
line4
[/code

you missed the closing "/" in the last tag
Code:

if length "$line" | grep '^[^;]' > max
max=length "$line" | grep '^[^;]'



All times are GMT -5. The time now is 10:44 PM.