3

I am new to shell script and I am working on shell scripting for jmeter. So far to run a jmeter script, I have written my shell script like below:

#! bin/sh    
start(){
    echo "Please enter the file name .jmx extension"
    read file

echo "Please enter the log file name .jtl extension"
read log_file

jmeter.sh -n -t $file -l $log_file
}
while [ "$1" != "" ]; do
case "$1" in
        start )
            start
            ;;
         *)
            echo $"Usage: $0 {start|stop}"
            exit 1
        esac
   shift
done

I have a stop method to terminate the process. Here, for this script I am asking the user to enter the .jmx fileName and .jtl fileName in different lines. But I want the user to be able to pass the .jmx fileName and .jtl fileName at the time he types the command to execute the script.

example: $ ./script.sh .jmx fileName .jtl fileName then, the script should run.

I don't know how to do it. Can someone please help?

user6348718
  • 1,355
  • 5
  • 21
  • 28

1 Answers1

1

Since read reads from stdin, you need to pass the filenames on stdin:

{ echo "file.jmx"; echo "file.jtl"; } | ./script.sh start

Using a here-document can be tidier:

./script.sh start <<END_INPUT
file.jmx
file.jtl
END_INPUT

A bit of code review: if the usage only takes a single parameter, "start" or "stop", you don't need the while loop:

#!/bin/sh

do_start_stuff() { ... }
do_stop_stuff() { ... }

case "$1" in 
    start) do_start_stuff;;
    stop)  do_stop_stuff;;
    *)     echo "Usage: $0 {start|stop}"; exit 1;;
esac

To rewrite your script to take all the parameters:

#!/bin/sh

usage() {
    echo "Usage $0 {start ...|stop}"
    # provide more info about arguments for the start case
    # provide an example usage
}

case "$1" in 
    stop) do_stop_stuff ;;
    start)
        shift
        if [ "$#" -ne 4 ]; then usage; exit 1; fi
        jmeter.sh -n -t "$1" "$2" -l "$3" "$4"
        ;;
    *) usage ;;
esac
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • I want to run the script using a single line command like this `$ ./script.sh .jmx fileName .jtl fileName` as after the script is done, I will run the parameterized script from jenkins. – user6348718 Jul 04 '16 at 21:57
  • Thank you. This works fine. I am using `while` because I have some other methods and can you tell me how to use the `start` answer you have provided in a `method` and call that method in the `while` loop – user6348718 Jul 05 '16 at 13:38
  • When I try to run it, it says `can't shift that many`. Can you help to resolve this? I have more than 4 functions in the script so, I am using a `while loop` and I have provided the `start` code in a function and calling that in the `while loop` – user6348718 Jul 06 '16 at 11:40
  • I maintain you don't need a while loop. Let's suppose your script takes subcommands "start", "stop", "status" and "restart" . Ignoring any subcommand arguments for now, a while loop would only make sense if you want to process multiple subcommands during a single run (example `script stop start status`) - IMO this is a terrible UI. You don't say `git add commit push checkout merge`.Keep it simple. Process only one subcommand and let the user call the script twice (`script restart && script status`) – glenn jackman Jul 06 '16 at 11:59
  • Now, I have edited my script like `start(){ shift if [ "$#" -ne 2 ]; then exit 1; fi jmeter.sh -n -t "$1" -l "$2" }` and calling that method using `case "$1" in stop) stop ;; *) usage ;; esac` This is not working. I would like to know how I can have a `method` which takes the `command line arguments` and call that in `case`. Can you please let me know – user6348718 Jul 06 '16 at 15:07