1

How do I collect user input and store in variables to use in parameters for jar file execution?

Tried using read -p and bash's select command from following link but get errors: How do I prompt for Yes/No/Cancel input in a Linux shell script? here is what I have so far:

#!/bin/bash

echo "Hello "$USER"."
echo "Enter System :"
read name
echo "Enter Format (JPG/PDF):"
read format 

java -jar ~/folder/myjar.jar -input ~/scripts/file -output$format ~/output/$name.$format

This partially works in that it produces output file but the name variable is lost, format variable is generated in string ok. Running this also generates the following errors: ": command not found" under hello user, " ': not a valid identifierame" after entering system name

Community
  • 1
  • 1
michael
  • 27
  • 1
  • 1
  • 3
  • Is that "fi" without an if statement in your original script or did you just forget to delete it when you copied it here? Otherwise I don't see any problems with your user input stuff. (That first prompt might be better written as `"Hello $USER."` though.) – William Apr 01 '13 at 21:20
  • that "fi" should not be there, forgot to delete. I should also add that the jar executable string is the command line for ksar output. – michael Apr 01 '13 at 21:31
  • Can you execute your script with the verbose flag so we can see what it is doing? --> "bash -x myscript.sh" – Jay Apr 01 '13 at 21:48
  • + $'\r' : command not found ' echo 'Hello michael. Hello michael. + $'\r' : command not found ' echo 'Enter System: Enter System: + read $'name\r' ': not a valid identifiername ' echo 'Enter Format (JPG/PDF): Enter Format (JPG/PDF): + read format $'\r' ': not a valid identifier + $'\r' : command not found + java -jar /home/michael/sar/myjar.jar -input /home/michael/scripts/file -output $'/home/michael/output//.\r' time to parse: 2207ms number of line: 52381 line/msec: 23.0 + $'\r' : command not found + $'\r' : command not found – michael Apr 01 '13 at 22:03
  • @michael, those error messages mean that the error has nothing to do with prompting, and everything to do with your script being saved as a DOS-format text file, not a UNIX-format one. – Charles Duffy Oct 02 '14 at 14:06
  • Prompting the user and reading data is a *horrible* interface. It is far better to take the parameters as arguments. Imagine if `grep` worked this way, and prompted the user to enter the pattern to search for and the list of files in which to search. – William Pursell Jul 17 '16 at 06:59

1 Answers1

0

Looks like you may have carriage return / windows line terminations, try to run

dos2unix input.txt

or

sed 's/^M$//' input.txt > output.txt

or

tr -d '\r' < input.txt > output.txt