0

I'm not familiar with shell scripting, 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

5
  • Please, could you clarify how should you create the first file? Is that file the output of a program that takes a random value? Should it contain al line of text with a random value? Commented Dec 1, 2008 at 5:24
  • The file thats created just has a word and a random value associated with it. Commented Dec 1, 2008 at 5:25
  • @Fred, you don't think this smells like homework? I'd be interested in your rationale. Commented Dec 1, 2008 at 5:58
  • I think that it smells like a simple question from an user that doesn't have experience with Unix scripts. That doesn't necessarily mean homework :) Commented Dec 1, 2008 at 6:01
  • As you wish, @Fred. You're the one spending time on it, so I'll leave it open (questioner also at least attempted to do it before asking so I'll credit them for that). Cheers. Commented Dec 1, 2008 at 6:10

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

The shell is not perl :-) You wanna think long and hard about FILE_NAME=shift...
3

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.

4 Comments

peeve: that script is a bit more portable if you start it with #!/bin/sh -- it's not doing anything bash specific.
bash has $RANDOM, anyway. An non-perl alternative would be awk 'BEGIN { print int(rand(1000)) }'
@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.
another way to get a random number (assuming GNU coreutils): seq 10000|sort --random-sort|head -n 1
0

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.