views:

288

answers:

4

Hi, I am not sure what I am to use for this task, but I am guessing it is shell scripting. But I myself am not familiar with what shell scripting is, so I'm not sure how to do it or if it is possible. If you can give me links or advice, that would be great. What I want to do is:

  • Create a file, simple text file EX:

    param1 (RANDOMVALUE)

    Where randomvalue is a random number generated.

  • run a program with that file we just created and output to a file

    ./program filewejustcreated > A

The program has already been created and it takes a filename as a parameter, no need to worry about that stuff.

  • run another program with the file we just created, the program already exists and out put it to a file

./Anotherprogram filewejustcreated > B

  • run a diff comamand on A, B

    diff A B

Display what diff returns...

Thanks

[Edit] I am using shell: tcsh

+3  A: 

You have almost written the script already. The only missing thing is the random number; I'll do it with Perl. Here is a quick & dirty solution in sh (or bash; I'm presuming you're on a Linux/Unix system):

#!/bin/sh
perl -e 'print "TheWord (", int(rand(1000)), ")\n"' > tempfile
./program tempfile > A
./Anotherprogram tempfile > B
# rm tempfile  # this would delete 'tempfile' if uncommented
diff A B

Now save this in a file (say, script.sh) and in a shell execute:

chmod +x script.sh

to make it executable, and

./script.sh

to run it.

Federico Ramponi
peeve: that script is a bit more portable if you start it with #!/bin/sh -- it's not doing anything bash specific.
Dustin
bash has $RANDOM, anyway. An non-perl alternative would be awk 'BEGIN { print int(rand(1000)) }'
ysth
@BobS: Don't worry about the shell you are using to run commands, the first line of the script qualifies it as a Bourne Shell script, which is a standard on Unix systems.
Federico Ramponi
another way to get a random number (assuming GNU coreutils): seq 10000|sort --random-sort|head -n 1
ysth
+1 I was just looking for that ./script.sh
Rulas
A: 

Correction: I think the questioner wanted to run both programs with the same input...

./Anotherprogram tempfile > B
Chris Nava
already corrected, thanks :D
Federico Ramponi
could you remove this "answer" which is actually not an answer ? :)
Barth
+3  A: 

I am not sure about the function to generate a random number in tcsh. However, in a more common shell like BASH, references to the variable $RANDOM, generates random numbers.

So, in your shell script (a BASH shell script here), the contents would be:

#Pick the first argument to the call as the file name
FILE_NAME=shift
echo "param1 $RANDOM" > $FILE_NAME
./program $FILE_NAME > $FILE1
./Anotherprogram $FILE_NAME > $FILE2
diff $FILE1 $FILE2
Shyam
A: 

Shell scripting is mostly just putting together different programs in ways that get the job done. There are a lot of programs that do just one simple thing and can be combined to accomplish larger tasks that you will learn of as you get into the shell scripting world. An example of a large shell script is perl's Configure script. In the first bit you see (along with some humorous comments) cat, true, sh, rm, test, sed, uname, and grep used.

ysth
All true, but not directly relevant to the question, I think.
Jonathan Leffler
There's a not all that implicit "what is shell scripting" question in there that no one else had addressed yet.
ysth