I have written a java program that maintains a database for bank accounts (This is a course assignment) and I want to create a bash script to run. Upon running the program, you have to input "i", "h", "t" or "?" to get it to perform a task. I would like to take those command line arguments and use them for when running this program in bash. For example, if the script were named accounts, I want to be able to type accounts -i and the script would launch the program with the i command already input and perform the task for that argument? How would I go about doing that?
-
see `man getopt` or `help getopts`. – choroba Dec 04 '16 at 08:48
1 Answers
The best would properly be for your Java program to take arguments.
But I assume all of this is for educational purpose only, and that said Java program reads from standard input.
Usually configuration for a program should go as arguments:
$ ./my_program --configA --configB --optC=valD
But In your case it seems like you have an interactive program that prompts the user for questions:
$ ./my_program
Question 1?
> Answer1
Question 2?
> Answer2
$
is command prompt, and >
is user input.
Anyway one can feed standard input from a pipe, file, etc etc:
my_program1 | my_program2
Output from my_program1
goes as input to my_program2
.
my_program < my_file
Input to my_program
is coming from a file my_file
.
You can also feed input from a here documents, the syntax is <<MARKER
and ends with MARKER
:
my_program << NAME_DOESNT_MATTER
line1
line2
line3
NAME_DOESNT_MATTER
This will put three lines into my_program
.
In bash it's simply to refer to positional parameters as they are called $1
, $2
, .. $n
:
$ cat my_shell_program
#!/bin/bash
echo "$2" "$1"
$ ./my_shell_program "hello world" "John Doe"
John Doe hello world
Now you should be able to figure out the rest.

- 46,145
- 13
- 104
- 123