Simon Scott Simon Scott - 4 months ago 14
Linux Question

Creating a simple bash script that uses cat and has 3 arguments

I have this bash command I am using to cat the output of two java programs that take 1 parameter each into a file:

cat file.ext | java program1 argument1 | java program2 argument2 > argument3


argument3 is a optional and if it is not provided it should just be file.srtd

How can I create a bash script for this?

Answer

In bash, the variable $n is the n'th argument.

The variable $# is the number of arguments given by the user.

So you can do somthing like that:

#!/bin/bash

if (( $# < 3 )) ; then
    $3="file.srtd"
fi;

cat file.ext | java program1 $1 | java program2 $2 > $3

You can look here for more examples

Comments