Looking for way to automate testing kshell app - shell

I inherited a shell-script application that is a combination of kshell scripts, awk, and java programs. I have written JUnit tests for the java pieces.
Is there a good way to do something similar for the kshell scripts and awk programs?
I have considered using JUnit and System.exec() to call the scripts, but it seems like there should be a better way.

I have found shUnit2 and will try that.
Update with the results of trying out shUnit
shUnit works as expected. Script files are written with test functions defined and then a call to shUnit.
Example:
#!/bin/sh
testFileCreated()
{
TESTFILE=/tmp/testfile.txt
# some code that creates the $TESTFILE
assertTrue 'Test file missing' "[ -s '${TESTFILE}' ]"
}
# load shunit2
. /path/to/shUnit/shunit2-2.1.5/src/shell/shunit2
Result
Ran 1 test.
OK
The 'OK' would be replaced with 'FAILED' if the file did not exist.

You might want to try Expect. It was designed for automating interactive programs. Of course Expect was written on top of TCL, which is an abominable scripting language. So there are interfaces for Python (Pexpect) and perhaps other languages that are more programmer friendly. But there is lots of documentation laying around for TCL/Expect that is still useful.

This is not a direct answer to your question, but you may consider using a simple Makefile to run bash scripts with different parameters.
For example, write something like this:
cat >Makefile
test_all: test1 test2 test3
test1:
script1 -parameter1 -parameter2
test2: $(addprefix test2file_, $TESTFILES)
test2file_%:
script2 -filename $*
test3:
grep|awk|gawk|sed....
By calling 'make test_all' you will execute all the scripts automatically, and the syntax is not so difficult to learn - you just have to define a rule name (test1, test_all...) and the commands associated with it.

Related

Script for running multiple Make Commands

I would like to get insight on how to get started or what general direction to look in when trying to make a script or makefile that will run 3 make commands at once that take in the same input. These three commands all ask for the same input but just output different excel files due to it manipulating the pulled data in different ways. Therefore If I were able to create a script or makefile that ran all three commands at once when giving the input one time it would SAVE ME A TON OF TIME.
This is all being done in putty pretty much (in terms of the commands)
Thanks,
NP
You want to use a shell script.
For instance, you can create run.sh with:
#!/bin/bash
make FLAG1=ON $*
make FLAG2=ON $*
make FLAG3=ON $*
Make it executable and do `./run.sh MYCOMMOFLAG1=ON MYCOMMONFLAG2=OFF...

(Tcl?) Script for running modelsim with testbench as parameter from shell

I want to make a script, which can be executed from shell like:
./myscript -test1 or tclsh myscript.tcl -test1
I want it to open ModelSim, compile units, load a desired testbench, run simulation. Name of the test would be a parameter. I've already made macro files (.do) containing modelsim commands to compile & simulate desired units (+adding signals to waveform). I'm asking because scripting isn't my area of expertise.
So here's my questions:
How to ,,tell'' Modelsim (at startup) to do the commands in specified file?
Is TCL the language i'm looking for // is it doable in TCL? If so, which commands should i make familiar with?
Or maybe shell script is sufficient and i should look for specific Modelsim commands in reference manual?
Thanks for you time!
EDIT: Posting little example i've made for everyone to use. Usage: ./foo.tcl testname
#!/usr/bin/tclsh
# params
set testname [lindex $argv 0]
set testlist {test1 test2 test3}
# run vsim test $testname
if { [ lsearch $testlist $testname ] >= 0 } {
puts "Test found. Executing..."
open "|vsim -do $testname "
} else { puts "Test not found on the list!" }
You can launch vsim with arbitrary commands using the -do <arg> command line option. The argument can either be a filename of a .do file containing arbitrary Tcl code or a string of Tcl commands ("run -all; quit" is useful for non-interactive command line mode with -c).
Tcl is a full featured scripting language. It can handle any automation task you need to accomplish. Ultimately you cannot escape Tcl with Modelsim since almost everything runs through it internally. I would recommend you piece together what you need in a .do file and run that using the -do option.
If you create a .tcl script (.do files can run QuestaSim/ModelSim commands and tcl commands), you can do everything you want to do, include running other .do/.tcl files. For example:
ModelSim/QuestaSim Command Line:
just like what you are used to...
$: do MyDoFile.do
...instead use a Tcl file, which could call out your .do files:
$: source ./MyDirectory/MyTCLScript.tcl
Within that MyTCLScript.tcl you can have literally the following:
Within MyTCLScript.tcl:
...
#Use tabs for aliases
source ./MyDirectory/OtherDirectory/OtherTCLScript.tcl
MyAlias1
MyAlias2
do ./MyDoFile.do
...
Finally, to let you use commands to run single testbenches and the sort, I suggest looking at Tcl documentation on aliases. Here is an example though:
Within OtherTCLScript.tcl:
...
alias MyAlias1 {
eval <command><command flags>
}
alias MyAlias2 {
eval <command><command flags>
}
...
Sources:
1. Experience
2. Questa SIM User's Manual

Obfuscating a command within a shell script

There are a lot of tips (and warnings) on here for obfuscating various items within scripts.
I'm not trying to hide a password, I'm just wondering if I can obfuscate an actuall command within the script to defeat the casual user/grepper.
Background: We have a piece of software that helps manage machines within the environment. These machines are owned by the enterprise. The users sometimes get it in their heads that this computer is theirs and they don't want "The Man" looking over their shoulders.
I've developed a little something that will check to see if a certain process is running, and if not, clone it up and replace.
Again, the purpose of this is not to defeat anyone other than the casual user.
It was suggested that one could echo an octal value (the 'obfuscated' command) and use it as a variable within the script. e.g.:
strongBad=`echo "\0150\0157\0163\0164\0156\0141\0155\0145"`
I could then use $strongBad within the shell script to slyly call the commands that I wanted to call with arguments?
/bin/$strongBad -doThatThingYouDo -DoEEET
Is there any truth to this? So far it's worked via command line directly into shell (using the -e flag with echo) but not so much within the script. I'm getting unexpected output, perhaps the way I'm using it?
As a test, try this in the command line:
strongBad=`echo -e "\0167\0150\0157"`
And then
$strongBad
You should get the same output as "who".
EDIT
Upon further review, the addition of the path to the echo command in the variable is breaking it. Perhaps that's the source of my issue.
You can do a rotate 13 on any command you want hidden beforehand, then just have the the obfuscated command in the shell script.
This little bash script:
#!/bin/bash
function rot13 {
echo "$#" | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]'
}
rot13 echo hello, world!
`rot13 rpub uryyb, jbeyq!`
Produces:
rpub uryyb, jbeyq!
hello, world!

How do I write a shell script that repeats a java program a specific number of times?

Essentially I am looking to write a shell script, likely using a for loop, that would allow me to repeat a program call multiple times without having to do it by hand (I don't know exactly how to explain this, but i want to perform the java TestFile.java command in the cmd window multiple times without doing it by hand).
I am trying to write it for the UNIX shell in bash, if that helps at all.
My program outputs a set of numbers that I want to look at to analyze end behavior, so I need to perform many tests for many different inputs and I want to streamline the process. I have a pretty basic understanding of shell scripting - i tried to teach myself today but I couldn't really understand the syntax of the for loop or the syntax of how to write a .java file call, but I would be able to write them in shell script with a little help.
This will do:
#!/bin/bash
javac Testfile.java # compile the program
for((i=1;i<=50;i++))
do
echo "Output of Iteration $i" >> outfile
java Testfile >> outfile
done
This will compile your java program and run it for 50 times and store the output in a file named outfile. Likewise, you can change the 50 for the number of iterations you want.
#!/bin/bash
for i in {1..10}
do
#insert file run command here
done
#!/bin/bash
LOOPS=50
for i IN {1 .. LOOPS}
do
java TestFile >> out.log
done

How to write a makefile for python, R and bash scripts

I am a statistician, and I recently wrote an bash pipeline for a research project, it is basically a main bash scripts calling bash, python and R scripts at each step of analysis, and there are lots of steps and lots scripts of course. My friend told me that I can create a makefile for them, but I have little experience on computer except writing scripts. I found somes examples but they are for VC. Does my cases also need to compile like VC programs? Can anyone share me some of his experience?
For example, I have main.sh, which calls step1.py, step2.sh, step3.r, and step2.sh calls step2.1.py, step2.2.r how do I link them by makefile?
step1:
step1.py;\
$(MAKE) step2;\
step3.r
step2:
step2.1.py;\
step2.2.r
all: step1
echo "Done"
From your description, it sounds like main.sh and step2.sh just call the python and R scripts. If so, then the above is the Makefile that would replace them. If they have other steps in them, then you can add those bash commands in the step1 or step2 targets.
Using a Makefile like this will allow you to also execute only part of your execution sequence for debugging purposes.

Resources