Comparing string read from csv file - shell

I'm trying to compare string read from csv file in if condition. Below is the sample code I have written:
export IFS="~"
while read a b c
do
echo "line Num:$a"
if [ "$b"=="sat" ];then
echo "Its saturday"
echo $c
elif [ "$b"=="sun" ];then
echo "Its sunday"
echo $c
else
echo "Its weekday"
echo $c
fi
done < "$1"
Below is the csv file:
1~sat~enjoy
2~sun~enjoy
3~mon~work
4~tue~work
5~sun~enjoy
Below is the output I'm getting:
line Num:1
Its saturday
enjoy
line Num:2
Its saturday
enjoy
line Num:3
Its saturday
enjoy
line Num:4
Its saturday
enjoy
line Num:5
Its saturday
enjoy
Needed the below output instead:
line Num:1
Its saturday
enjoy
line Num:2
Its sunday
enjoy
line Num:3
Its weekday
work
line Num:4
Its weekday
work
line Num:5
Its sunday
enjoy
Any idea how to achieve this?

try
export IFS="~"
while read a b c
do
echo "line Num:$a"
if [ "$b" = "sat" ];then
echo "Its saturday"
echo $c
elif [ "$b" = "sun" ];then
echo "Its sunday"
echo $c
else
echo "Its weekday"
echo $c
fi
done < "$1"
do not use "$foo"=="$bar" as bash will only see one token.
you can also try
case "$b" in
sat) echo "It's saturday" ;;
sun) echo "It's sunday" ;;
*) echo "It's weekday" ;;
esac
echo $c

Related

I Want To Write A BASH Script To Find The Current DAY And Then Greet The User As Below: [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 2 years ago.
For Monday, print Moanday
For Friday, print Friyay
For other days, print Foobar
input date: Fri July 20 10:02:15 IST 2018
Output: Friyay
See My Code Below:
day = $(date | cut -d' ' -f1)
if ["$day" == "Monday"];
then
echo Moanday
elif ["$day" == "Friday"];
then
echo Friyay
else
echo Foobar
fi
With bash, GNU date and an array:
d=([1]=Moanday Foobar Foobar Foobar Friday Foobar Foobar)
echo "${d[$(date +%u)]}"
#!/bin/bash
# From 'man date'
#-----------------------------------------
# %d day of month (e.g., 01)
# %u day of week (1..7); 1 is Monday
# %m month (01..12)
#-----------------------------------------
DAY="$(date +%u)"
DATE="$(date +%d)"
MONTH="$(date +%m)"
case $DAY in
"1")
echo -e "Moanday"
;;
"2")
echo -e "Tueasday"
;;
"3")
echo -e "Wedneasday"
;;
"4")
echo -e "Thurasday"
;;
"5")
echo -e "Friday"
[[ $DATE = "13" ]] && echo "It's friyay the 13th";exit 0
;;
"6")
echo -e "Saturday"
;;
"7")
echo -e "Sunday"
;;
esac
# Now you can customize this, my example for christmas:
[[ ${MONTH} = "12" && ${DATE} = "24" ]] && echo "It's christmas time" || echo "It's not christmas time :/"
I was able to solve the problem with the help of "shellcheck.net" as recommended by Cyrus. See below for the correct answer.
#!/bin/bash
day=$(date | cut -d' ' -f1)
if [ "$day" == "Monday" ];
then
echo Moanday
elif [ "$day" == "Friday" ];
then
echo Friyay
else
echo Foobar
fi

Bash counting Arguments

I am having an issue counting the arguments I enter in my shell script. My script is suppose to echo "You have entered too many arguments" by using and Else statement at the end of my code. If I enter more than 3 I get nothing. Am I missing something simple or should I change this to a case statement. I am new to shell scripting so any help would be greatly appreciated.
#!/bin/bash
clear
Today=$(date +"%m-%d-%y")
echo -en '\n'
echo "To find out more about this calculator please refer to the calc.readme file."
echo -en '\n'
echo "You entered $*"
echo -en '\n'
if [ "$#" -eq "3" ]; then
if [ "$2" == "+" ]; then
answer=`expr $1 + $3`
echo "You entered the correct amount of arguments"
echo -en '\n'
echo "Your total is: "`expr $1 + $3`
echo "[$Today]: $# = $answer" >> calc.history
elif [ "$2" == "-" ]; then
answer=`expr $1 - $3`
echo "You entered the correct amount of arguments"
echo -en '\n'
echo "Your total is: "`expr $1 - $3`
echo "[$Today]: $# = $answer" >> calc.history
elif [ "$2" == "*" ]; then
answer=`expr $1 \* $3`
echo "You entered the correct amount of arguments"
echo -en '\n'
echo "Your total is: "`expr $1 \* $3`
echo "[$Today]: $# = $answer" >> calc.history
elif [ "$2" == "/" ]; then
asnwer=`expr $1 / $3`
echo "You entered the correct amount of arguments"
echo -en '\n'
echo "Your total is: "`expr $1 / $3`
echo "[$Today]: $# = $answer" >> calc.history
else
echo -en '\n'
echo "You entered too many arguments."
fi
fi
Your if statements are wrongly nested. You wrote:
if <test on number of arguments>
if <values>
else
<wrong number of arguments>
fi
fi
while you should have written:
if <test on number of arguments>
if <values>
fi
else
<wrong number of arguments>
fi
Your else statement is associated with the wrong if statement, but you can replace the big if-elif chain with a single case statement.
#!/bin/bash
clear
today=$(date +"%m-%d-%y")
echo
echo "To find out more about this calculator please refer to the calc.readme file."
echo
echo "You entered $*"
echo
if [ "$#" -eq "3" ]; then
echo "You entered the correct amount of arguments"
case $2 in
[+-*/])
echo
answer=$(( $1 $2 $3 ))
echo "Your total is $answer"
echo "[$today]: $# = $answer" >> calc.history
;;
*) echo "Unrecognized operator $2"
;;
esac
else
echo
echo "You entered too many arguments."
fi

another unexpected token `fi' error

I am trying to test 7 days for specific conditions and need to account for leap years. I think I have the logic right, been over it 100 times. But for some reason I keep getting the same error.
:~$ bin/shabbat/test
bin/shabbat/test: line 90: syntax error near unexpected token `fi'
bin/shabbat/test: line 90: ` fi'
Don't laugh to hard please. I am not a programmer. I have to copy/paste code that works and change it try to get it to do what I want it to do.
#!/bin/bash
STORDIR="${HOME}/bin/shabbat/data"
DAY=28
MONTH=1
YEAR=2016
# Calculate next day accounting for leap years
function Next_Day
{
case ${ $MONTH } in
1)
FULL="31"
;;
2)
FULL="28"
;;
3)
FULL="31"
;;
4)
FULL="30"
;;
5)
FULL="31"
;;
6)
FULL="30"
;;
7)
FULL="31"
;;
8)
FULL="31"
;;
9)
FULL="30"
;;
10)
FULL="31"
;;
11)
FULL="30"
;;
12)
FULL="31"
;;
esac
if [[ "$DAY" -le "27" ]]; then
(( DAY++ ))
exit
elif [[ ${MONTH} == "2" ]]; then
rem1=$((YEAR%4))
rem2=$((YEAR%100))
rem3=$((YEAR%400))
if [ ${rem3} = "0" ]; then
$FULL="29"
exit
fi
if [ ${rem2} = "0" -a ${rem3} != "0" ]; then
# echo "$YEAR Is Not A Leap Year"
exit
fi
if [ ${rem1} = "0" -a ${rem2} != "0" ]; then
# echo "$YEAR Is A Leap Year"
$FULL="29"
else
# echo "$YEAR Is Not A Leap Year"
exit
fi
if [ ${DAY} = ${FULL} -a ${MONTH} -lt "12" ]; then
(( MONTH++ ))
(( DAY++ ))
exit
fi
elif [ ${DAY} = ${FULL} -a ${MONTH} = "12" ]; then
(( YEAR++ ))
let MONTH=1
let DAY=1
exit
fi
exit
fi
}
#################
# # Main Code # #
#################
for (( i = 1; i < 7; i++)); do
YOMTOVE=$(hdate -dhq $DAY $MONTH $YEAR | awk 'NR==3')
echo "$YOMTOVE"
if [[ -n "$YOMTOVE" ]]; then
grep ^"$YOMTOVE" "$STORDIR"/YomTove.cvs
# actions based on what holiday it is
fi
done
Next_Day
exit
I have only gotten this far with help from my local lug and linuxquestion.org but I hate to keep bugging them with my old brain problems.
Thanks.
You either want to remove line 88 or line 90, they are unmatched fis. I am guessing you need to remove line 90.
Also I think you need to replace case ${ $MONTH } in with case ${MONTH} in

How can I loop If Statements in Bash

Here is my code:
#!/bin/bash
echo "Letter:"
read a
if [ $a = "a" ]
then
echo "LOL"
fi
if [ $a = "b" ]
then
echo "ROFL"
fi
Is there a way for me to loop this so that, after displaying either LOL or ROFL, I would be asked for a letter again?
Yes.
Oh, you want to know how?
while true; do
echo "Letter:"
read a
if [ $a = "a" ]
then
echo "LOL"
elif [ $a = "b" ]
then
echo "ROFL"
fi
done
Of course, you probably want some way to get out of that infinite loop. The command to run in that case is break. I would write the whole thing like this:
while read -p Letter: a; do
case "$a" in
a) echo LOL;;
b) echo ROFL;;
q) break;;
esac
done
which lets you exit the loop either by entering 'q' or generating end-of-file (control-D).
Don't forget that you always want -r flag with read.
Also there is a quoting error on that line:
if [ $a = "a" ] # this will fail if a='*'
So here is a bit better version(I've also limited the user to input only 1 character):
#!/bin/bash
while true; do
read -rn1 -p 'Letter: ' a
echo
if [[ $a = 'a' ]]; then
echo "LOL"
elif [[ $a = 'b' ]]; then
echo "ROFL"
else
break
fi
done
Or with switch statement:
#!/bin/bash
while read -rn1 -p 'Letter: ' a; do
echo
case $a in
a) echo LOL;;
b) echo ROFL;;
*) break;;
esac
done

comparing $var to

I have a test script the needs to read the variable 'LAB' and e-mail the correct company.
I've looked but can't find anything that has worked yet.
Any thoughts?
#!
#
LAB=3
#
if [ "$LAB" = "$1" ];then
echo "Got Zumbrota" && ./mailZ
fi
#
if [ "$LAB" = "$2" ];then
echo "Got Barron" && ./mailB
fi
#
if [ "$LAB" = "$3" ];then
echo "Got Stearns" && ./mailS
fi
If this a bash script, start your file with
#!/bin/bash
and use -eq for integer comparison and since LAB is an integer in your script
if [ $LAB -eq $1 ]
These cascading if statements can be condensed into a case statement:
case "$LAB" in
1) echo "Got Zumbrota" && ./mailZ
;;
2) echo "Got Barron" && ./mailB
;;
3) echo "Got Stearns" && ./mailS
;;
*) echo "don't know what to do with $LAB"
;;
esac

Resources