Trouble with if then statement in bash

Viewed 38

Have a bonus assignment for a class and am tasked with displaying information given a few different inputs. my current script for this to pull the information I need

    #!/bin/bash

date=$1

hour=$2

dayornight=$3

cat $date* | grep -i $hour | grep $dayornight | $4

if [ $4 == "BlackJack*" ]; then awk '{print $1, $2, $3, $4}'

if [ $4 == "Roulette*" ]; then awk '{print $1, $2, $5, $6}'

if [ $4 == "Texas*" ]; then awk '{print $1, $2, $7, $8}'

fi

When I run this with my inputs (filename/hour/am/pm/"BlackJack"), it tells me

"roulette_dealer_finder_by_time_and_game.sh: line 9: BlackJack: command not found" and bricks my terminal depending on what variables I change.

Is there a way to add in a if-then statement in this line of script to pull different columns based on different keywords, for example Texas/Roulette/BlackJack?

bash roulette_dealer_by_time_and_game.sh 0310 5 PM BlackJack would be the command to run it

Hour AM/PM  BlackJack_Dealer_FNAME LAST Roulette_Dealer_FNAME LAST  Texas_Hold_EM_dealer_FNAME LAST

12:00:00 AM Izabela Parrish Marlene Mcpherson   Madina Britton
01:00:00 AM Billy Jones Saima Mcdermott Summer-Louise Hammond
02:00:00 AM Summer-Louise Hammond   Abigale Rich    John-James Hayward
03:00:00 AM John-James Hayward  Evalyn Howell   Chyna Mercado
04:00:00 AM Chyna Mercado   Cleveland Hanna Katey Bean
05:00:00 AM Katey Bean  Billy Jones Evalyn Howell
06:00:00 AM Evalyn Howell   Saima Mcdermott Cleveland Hanna
07:00:00 AM Cleveland Hanna Abigale Rich    Billy Jones
08:00:00 AM Rahima Figueroa Billy Jones Madina Britton
09:00:00 AM Marlene Mcpherson   Cleveland Hanna Summer-Louise Hammond
10:00:00 AM Izabela Parrish Madina Britton  John-James Hayward
11:00:00 AM Madina Britton  Summer-Louise Hammond   Chyna Mercado
12:00:00 PM Summer-Louise Hammond   John-James Hayward  Katey Bean
01:00:00 PM John-James Hayward  Chyna Mercado   Evalyn Howell
02:00:00 PM Chyna Mercado   Billy Jones Cleveland Hanna
03:00:00 PM Katey Bean  Evalyn Howell   Rahima Figueroa
04:00:00 PM Evalyn Howell   Cleveland Hanna Billy Jones
05:00:00 PM Billy Jones Rahima Figueroa Summer-Louise Hammond
06:00:00 PM Rahima Figueroa John-James Hayward  John-James Hayward
07:00:00 PM Marlene Mcpherson   Chyna Mercado   Chyna Mercado
08:00:00 PM Saima Mcdermott Billy Jones Katey Bean
09:00:00 PM Abigale Rich    Evalyn Howell   Billy Jones
10:00:00 PM Evalyn Howell   Katey Bean  Cleveland Hanna
11:00:00 PM Cleveland Hanna Billy Jones Rahima Figueroa

and this is the file it would try to grab information from and based on the $4 input it would grab say coumn 123and4

edit: I updated the script a bit to give a clearer picture of what I'm trying to accomplish. And the game type would just be to designate what column the script should be pulling from. the file name is actually the date so 0310 for example

1 Answers

line 9: BlackJack: command not found"

Because you pipe into $4, which tries to run a command called Blackjack. I assume that is your input file? If so, that's what you should process, rather than cat $date*


Your file appears to use spaces instead of tabs, or any other consistent delimiter. That being said, awk will be more difficult to use. More specifically, names shouldn't be assumed to be "First Last", even if hyphenated, so your column numbers won't be consistent. If you edit the file to use commas/tabs/pipes, then you can know for sure which name is which...

Regarding your inputs, it's not clear what 0310 should return since passing that into grep with the shown file will return nothing...


I'd recommend you fix the column delimiters, such as using a CSV, then you can use cut

Here's an example

Data

$ cat Blackjack.csv
20220919,4 am,John,2,3
20220919,4 pm,Jane,5,6

Script

$ cat process.sh
#!/bin/bash

if [ $# -ne 4 ]; then
  echo "usage: $0 [date] [hour] [am/pm] [gamefile]"
  exit 1
fi

DATE=$1
HOUR=$2
DAY_NIGHT=$3
GAME=$4

if [ -e $GAME ]; then
  # TODO: Add [[ $GAME == "Blackjack" ]] check here
  cut -d, -f1-4 $GAME | grep $DATE | grep $HOUR | grep -i $DAY_NIGHT
else
  echo "Unable to find $GAME" 1>&2
  exit 1
fi

Usage

$ ./process.sh
usage: ./process.sh [date] [hour] [am/pm] [gamefile]
$ ./process.sh 20220919 4 pm Blackjack.csv
20220919,4 pm,Jane,5
$ ./process.sh 20220919 4 am Blackjack.csv
20220919,4 am,John,2
$ ./process.sh 20220920 4 am Blackjack
Unable to find Blackjack
$ ./process.sh 20220920 4 am Blackjack.csv  # no output

Keep in mind that grep -i $DAY_NIGHT will also include rows where the name includes am (case insensitive), such as Amber

Related