0

First the user will call a file name Time.sh and enter two data, namely StartTime , EndTime. I have the code in Time.sh file to extract the data between the StartTime and EndTime. I am using the follwing code.

#!/bin/bash
read -p "Enter StartTime:" StartTime
read -p "Enter EndTime:" EndTime
awk -v StartTime="$StartTime" -v EndTime="$EndTime" -f Script.sh Test.log

So in the bash I will something like below,

$ ./Time.sh
Enter Start Time :(User will enter StartTime. Example:01:04:22)
Enter End Time : (User will enter EndTime. Example:01:05:22)

I want to simplyfy it for the user like, the user should type all three data in a single line like,

Time.sh 01:04:22 01:05:55

How can I modify to get the same result? Please Help.

Vedh
  • 93
  • 1
  • 10
  • Why do you call your Awk script `Script.sh`. If you use a file name extension, it would seem more conventional to use `.awk`; but Unix doesn't particularly care, so you are only confusing humans. – tripleee Jun 15 '16 at 09:17
  • Cross-site duplicate: http://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script – tripleee Jun 15 '16 at 09:23

1 Answers1

0

EDIT: Added input validation

You can use the command line arguments as follows:

#!/bin/bash
StartTime=$1
EndTime=$2

if [[ "$StartTime" > "$EndTime" ]]; then
    echo "StartTime cannot be greater than EndTime"
else
    awk -v StartTime="$StartTime" -v EndTime="$EndTime" -f Script.sh Test.log 
fi

And then run it by: ./Time.sh 01:04:22 01:05:55

Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
  • Thank you @Avihoo Mamka.I have used the following code to check if the starttime is greater than endtime. But i cannot execute the else part. Whatever time i entered, it is showing only the echo part. Is there any mistake in the syntax. `if [ "$1" > "$2" ]; then echo "StartTime cannot be greater than EndTime" else awk -v StartTime="$StartTime" -v EndTime="$EndTime" -f Script.sh Test.log fi` – Vedh Jun 15 '16 at 08:46
  • I edited my answer, and it worked for me. check this. – Avihoo Mamka Jun 15 '16 at 08:51
  • Pay attention, I edited again, changed `>` to `-gt`. – Avihoo Mamka Jun 15 '16 at 08:53
  • You can do: `if [ "$#" -eq "3"]; then echo "3 params" else echo "less than 3 params" fi` – Avihoo Mamka Jun 15 '16 at 08:58
  • Show me the arguments you use for the scripts that gets only the else part, and check that you're using `-gt` and not `>`. – Avihoo Mamka Jun 15 '16 at 08:59
  • I think `-gt` is used only for integers. I am getting ` line 5: [: 01:07:14: integer expression expected` error if I enter a startime greater than endtime. – Vedh Jun 15 '16 at 09:00
  • Fixed that. Added double `[[]]` to the `if` – Avihoo Mamka Jun 15 '16 at 09:03
  • I have used `if [[ "$#" -ne "3" ]]; then echo "Enter All three parameters" fi` this code before the IF part.But I am getting both the echo part in the output. Should I have to use else if? – Vedh Jun 15 '16 at 09:17
  • No, you should add `exit 1` after the echo of the error, to quit the script with an error code indicates there's a problem. – Avihoo Mamka Jun 15 '16 at 09:24
  • `#!/bin/bash StartTime=$1 EndTime=$2 if [[ "$#" -ne "3" ]]; then { echo "Enter All three parameters" exit 1 } fi if [[ "$StartTime" > "$EndTime" ]]; then { echo "StartTime cannot be greater than EndTime" exit 2 } else awk -v StartTime="$StartTime" -v EndTime="$EndTime" -f Script.sh Test.log fi`. Is this code is correct? But I am getting`Enter All Three parametrs` for all conditions. Also I have to check if Script.sh and Test.log files exits or not. – Vedh Jun 15 '16 at 09:36