0

I'm trying to create a shell script in Linux to accept only 2 arguments for the date and one for the time (while at the same time accepting am/pm)

Here's one of the scripts I attempted to write up.

#!/bin/bash

date=$1
space=" "
time=$2
ampm=$3
timeampm=$2$space$3

print("date $1")
print("time $2")
print("ampm $3")
print("timeampm $timeampm")

Heres the file I'm going off

0310 02:00:00 AM Abigale Rich 
0310 05:00:00 AM Billy Jones 
0310 08:00:00 AM Billy Jones 
0310 11:00:00 AM Summer-Louise Hammond 
0310 02:00:00 PM Billy Jones 
0310 05:00:00 PM Rahima Figueroa 
0310 08:00:00 PM Billy Jones 
0310 11:00:00 PM Billy Jones 
0312 02:00:00 AM Abigale Rich 
0312 05:00:00 AM Billy Jones 
0312 08:00:00 AM Billy Jones 
0312 11:00:00 AM Summer-Louise Hammond 
0312 02:00:00 PM Billy Jones 
0312 05:00:00 PM Rahima Figueroa 
0312 08:00:00 PM Billy Jones 
0312 11:00:00 PM Billy Jones 
0315 02:00:00 AM Abigale Rich 
0315 05:00:00 AM Billy Jones 
0315 08:00:00 AM Billy Jones 
0315 02:00:00 PM Billy Jones 
0315 05:00:00 PM Rahima Figueroa 
0315 08:00:00 PM Billy Jones

For example, on line 3, I want to be able to ./scriptname.sh 0310 08:00:00 AM and it pulls out the name "Billy Jones"

I have been mostly trying to use grep, awk and sed. If you want to see my other codes I've written up, I'll add them.

  • after getting time stamp(timeampm=$2$space$3) you can do something like grep timeampm filenmae.txt | awk '{print $(NF-1), $NF}' – Rahul Jun 13 '21 at 11:25
  • It looks like you're just trying to do `awk '$1 == date && $2 == time && $3 = ampm {print $4}' date="$1" time="$2" ampm="$3" input-file`, but that doesn't seem very useful. – William Pursell Jun 13 '21 at 11:28
  • 2
    @Rahul That's a [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep); Awk can obviously and trivially select lines by regular expression without the help of `grep`. – tripleee Jun 13 '21 at 11:54
  • You tagged this [tag:sh] but actually posted a Bash script; probably see [Difference between sh and bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Jun 13 '21 at 11:56

1 Answers1

1

Your script contains several syntax errors and other oddities. Perhaps you are looking for

#!/bin/bash

date=$1
time=$2
ampm=$3
timeampm="$2 $3"

echo "date $1"
echo "time $2"
echo "ampm $3"
echo "timeampm $timeampm"

sed -n "s/^$date $timeampm //p" file

which of course can be reduced to just

#!/bin/sh
sed -n "s/^$1 $2 $3 //p" file

and you can run it with sh -x if you want to see exactly what it's doing.

For robustness, maybe add some validation for the arguments, or maybe switch to Awk which has less bewildering failure modes when you pass in things in unexpected formats. Plain standard grep doesn't easily let you remove the matching text and print the rest (though if you have GNU grep, waybe try grep -Po "^$1 $2 $3 \K.*" file).

More fundamentally, perhaps your file should contain dates in a standard format. On Linux, you can ask date -d to convert a large number of date formats and other time expressions to whichever format your file uses.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Need to be careful with `sed -n "s/^$1...."` as that can easily lead to a syntax error and obscure error messages for invalid arguemnts. (eg, the user enters a date "03/10") – William Pursell Jun 13 '21 at 12:09