Getting started with Shell Scripting?

I'm not familiar with shell commands, so I'm not sure how to do this or if it's possible. If you can give me links or advice, that would be great.

What I want to do:

  • Create a file, a plain EX text file:

    param1 (RANDOMVALUE)

    If a random value is generated by a random number.

  • run the program with the file we just created and output it 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.

  • run another program with the file we created, the program already exists and puts it in a file

./ Another program file to be created with> B

  • Run diff-comamand on A, B

    diff AB

Display what diff returns ...

thank

[Edit] I am using shell: tcsh

0


source to share


3 answers


You've almost written your script. The only thing missing is a random number; I'll do it with Perl. Here's a quick and dirty solution in sh (or bash; I'm assuming 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 the shell do:

chmod +x script.sh

      



to make it executable, and

./script.sh

      

to run it.

+3


source


I'm not sure about the random number generation function in tcsh. However, in the more common shell like BASH, variable references $RANDOM

generate random numbers.

So, in your shell script (a BASH

shell script here) the content 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

      

+3


source


Shell scripts are basically just bundling different programs together in a way to get the job done. There are many programs out there that only do one simple thing and can be combined to perform larger tasks, which you will learn when you get into the world of shell scripting. An example of a large shell script is the perl Configure script . In the first bit, you see (along with some humorous comments) cat, true, sh, rm, test, sed, uname, and grep.

0


source







All Articles