PDA

View Full Version : Bash script help



Purell86
November 7th, 2009, 04:27 PM
Hi i'm a newbie in scripting and i need help with developing a script that takes 2 lines of numbers from a file and subtracts them and echo's their result.

for instance i have a file test.txt with


200
50
now i have a script to ADD the two lines of number which is



value=0
while read var
do
value=$(($value + $var))
done < ~/test.txt
echo $value
but i can't figure out how too subract these two lines and echo the result

Rany Albeg
November 7th, 2009, 04:42 PM
#!/bin/bash

value=0
while read var
do
value=$(($var - $value))
done < test.txt
echo $(($value*(-1)))

exit 0

tried to keep it similar to what you wrote.

Purell86
November 7th, 2009, 05:07 PM
thanks a lot. i knew it was somehow similar just didnt know what to change to get the difference

Rany Albeg
November 7th, 2009, 05:16 PM
You welcome.

ghostdog74
November 8th, 2009, 01:10 AM
just 2 lines right?


# echo $(tr "\n" "-" < file)|bc
150

roccivic
November 8th, 2009, 01:30 AM
Also:

To add:

roccivic@roccivic-pc:~$ cat textfile
12
10
33
roccivic@roccivic-pc:~$ echo $((`cat textfile | xargs -d "\n" | sed 's/ /+/g'`))
55

To Substract:

roccivic@roccivic-pc:~$ cat textfile
33
10
roccivic@roccivic-pc:~$ echo $((`cat textfile | xargs -d "\n" | sed 's/ /-/g'`))
23

roccivic
November 8th, 2009, 02:40 AM
Or how about this? :p

To add:


roccivic@roccivic-pc:~$ echo -e "\
#include <stdio.h>\
\n#include <stdlib.h>\
\n\
\nint main (int argc, char *argv[]) {\
\n\tif ( argc > 1 ) {\
\n\t\tint i, sum = 0;\
\n\
\n\t\tfor (i=1; i<argc; i++) {\
\n\t\t\tsum+=atoi(argv[i]);\
\n\t\t}\
\n\t\tprintf(\"%i\\\
\n\", sum);\
\n\t}\
\n\treturn 0;\
\n}" > sum.c
roccivic@roccivic-pc:~$ gcc -Wall -O2 sum.c -o sum
roccivic@roccivic-pc:~$ sudo mv ./sum /usr/local/bin/mysum
roccivic@roccivic-pc:~$ rm ./sum.c

The test file:

roccivic@roccivic-pc:~$ cat textfile
12
10
33

The sum command (3 words :D):

roccivic@roccivic-pc:~$ mysum $(cat textfile)
55

That works for integers only, for floating numbers it would be something like this:

roccivic@roccivic-pc:~$ echo -e "\
#include <stdio.h>\
\n#include <stdlib.h>\
\n\
\nint main (int argc, char *argv[]) {\
\n\tif ( argc > 1 ) {\
\n\t\tint i;\
\n\t\tfloat sum = 0;\
\n\
\n\t\tfor (i=1; i<argc; i++) {\
\n\t\t\tsum+=atof(argv[i]);\
\n\t\t}\
\n\t\tprintf(\"%f\\\
\n\", sum);\
\n\t}\
\n\treturn 0;\
\n}" > sum.c
roccivic@roccivic-pc:~$ gcc -Wall -O2 sum.c -o sum
roccivic@roccivic-pc:~$ sudo mv ./sum /usr/local/bin/myfloatsum
roccivic@roccivic-pc:~$ rm ./sum.c

To substract integers:

roccivic@roccivic-pc:~$ echo -e "\
#include <stdio.h>\
\n#include <stdlib.h>\
\n\
\nint main (int argc, char *argv[]) {\
\n\tif ( argc > 1 ) {\
\n\t\tint i, diff = atoi(argv[1]);\
\n\
\n\t\tfor (i=2; i<argc; i++) {\
\n\t\t\tdiff-=atoi(argv[i]);\
\n\t\t}\
\n\t\tprintf(\"%i\\\
\n\", diff);\
\n\t}\
\n\treturn 0;\
\n}" > diff.c
roccivic@roccivic-pc:~$ gcc -Wall -O2 diff.c -o diff
roccivic@roccivic-pc:~$ sudo mv ./sum /usr/local/bin/mydiff
roccivic@roccivic-pc:~$ rm ./diff.c

ghostdog74
November 8th, 2009, 06:01 AM
you are doing the unnecessary.

roccivic
November 8th, 2009, 04:11 PM
Yeah, I know, was somewhat bored.