I have thought about this for quite some time and know what I want to do but am having some trouble at it. I have a text file filled with numbers like this, there are more in the file obviously. Each number is separated by a space.
The idea is to use these numbers to calculate some kind of a profit. The first line is price per item and the second line indicates an action. 1 means you bought it, -1 means you sold it, 0 means no action. So, to demonstrate things, you buy an item at $1, the running total is -1, you sell (add 3), subtract 2, ignore the no transaction, then add 5, leaving you with 5. Anyway, I have a script called test and if you run it, it allows you to enter a line of numbers separate by spaces, when you hit enter it displays the second line results. I think the idea is to feed lines in from testcase.txt to the check.sh file (the one I'm scripting now) and compare the program output with the expected output in testcase.txt. So, if I can get every odd line from testcase and feed it in, read the result output, then I can compare it. Is there a good way to do this? To call a .sh file within a .sh file?
Oh yeah, if I can somehow feed a line from testcase to test.sh and store its result in a variable (ala the second line), then there might be a way to get it done.
Last edited by tastybrownies; 04-13-2013 at 05:48 PM..
Reason: Clarification
I am not sure I understand what you are after, but I suppose you could try doing something like this to compare the calculated result with what is in the last column of every second file:
Actually I think I have a better understanding of what I'm after now. I wrote out some psuedo code with what I'm trying to do but am unsure how to get each line from a file and send each line to the script.
I apologize for before. What I wrote was kind of confusing and even I didn't fully understand what I was trying accomplish. Now I am sure what I want to do.
Last edited by tastybrownies; 04-13-2013 at 07:51 PM..
Reason: edited
You can't just subtract numbers from the odd lines, it would need to be the sum product of the fields of the odd and even lines, no? That is what I used in the bits of code I suggested...
Yes, you are correct in what you say about the sum. Either doing that or seeing whether the return line provided from the function is the same as the one in the testcase file. I know diff works for files but maybe I can use it for smaller cases.
---------- Post updated at 03:04 AM ---------- Previous update was at 02:06 AM ----------
Okay, I'm at the point where I am trying to pass an argument(odd line) to the ./test program and it keeps saying "command not found." I am trying to get the return value and store it in a variable. If I just run ./test in the terminal and hit enter, the cursor goes to the next line and I can enter something. When I call ./test in this way inside a script, it doesn't call it like this does it? ./test 0 0 0
Hopefully not. I know some of this code is repetitive and needs cleaned up but I am just trying to figure out how to get a value back from this program. Once I do this I can compare the line I got back to the even line in the evens file. Sorry, I am kind of new to shell programming so my code may look bad.
---------- Post updated at 12:55 PM ---------- Previous update was at 03:04 AM ----------
Well, I've almost done it. I managed to pipe pipe odd lines in, send them to the program and put them into a return variable. I've compared them in terminal and I am getting what I want. All the test cases run and I get a 10/10, my only problem is the trailing command not found messages. I do not know why this is happening. It's probably something really small but I don't have enough experience to immediately see it. Any helpful hints as to why I'm getting "command not found." Is there something wrong with my if statement?
The output in terminal I get when I run check.sh is the following:
You don't mention the shell that you are using (although we might infer it being bourneish). Should it be a modern one (bash, ksh), try running the script with the -x option. In bash, set sth will set positional parameters, not assign variables. If you want to assign variables, don't use spaces around the = sign. I don't know any command that is related/similar to your usage of the @ char.
And, using awkanyway, why don't you get the job done entirely in awk, as scrutinizer pointed out?
hi all,
trying this using shell/bash with sed/awk/grep
I have two files, one containing one column, the other containing multiple columns (comma delimited).
file1.txt
abc12345
def12345
ghi54321
...
file2.txt
abc1,text1,texta
abc,text2,textb
def123,text3,textc
gh,text4,textd... (6 Replies)
Hello Team,
I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file.
Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Hello, I am new in shell scripting. I need help regarding following.
I have 4 files generated by backups daily. I have stored the names of these 4 files into one file. i.e I have 4 files names as a, b, c & d and these names have been put into one file abcd.txt.
Now I want to cat each file in... (7 Replies)
Hi,
I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Hi all,
I jut use a loop to read lines from the user and redirect it to a file.
echo "Enter the line"
while read -r LINE
do
echo $LINE >> FILE
if ;then
break
fi
done
input
app... (1 Reply)
Hi Experts,
I am tryin to read a file and while doing so i need to skip the lines which start with a hash (#) char.
I thought of using a goto command but a lot of guys on this site say its not the good way to program. Moreover I am using a ksh shell which deos not support goto command.
... (4 Replies)
How can i have a while loop as follows
while read inputline
do
<task>
done < name_list
and also store the values (delimited) on each line to temp variables so as to print them on screen as follows
while read inputline
do
set name | cut -d "," -f1 name_list # #i know this is not... (1 Reply)
How can I exclude reading lines in a file that contains the following:
filesystem:/home/pach/liv_patches 128005120 88456640 37270758 71% /home/patches
That is, all lines that contain and begins with filesystem: should not be processed/read from a file (5 Replies)
I have a file that has a list of numbers in it. Each line has a different number. I am trying to create some sort of loop within a script that will pick the numbers up on lines 1 and 2 and then put those figures into the script. It then goes through the process then loops back and reads lines 2 and... (5 Replies)