Arthur Montgolfier
Arthur Montgolfier

Reputation: 11

Adding exceptions during cut returns unwanted results

I have a file that is being generated (sort of an audit file) with who have accessed said file. Looks as follows:

image

I need to write an alarming system that enters said report and checks for all users. however bash for some reason interprets the "------" as an input.

for i in $(cut -c 8-13 report_file.csv)
do
if [[ $i -eq 'suser' ]] || [[ $i -eq '--------' ]] || [[ $i -eq 'login' ]] || $i -eq 'root']] 
then 
break
else 
echo "email text"+ $i | mailx -s "email subject" $EMAILS_LIST
done 

the output for this is:

./script_name.sh: line 26: [[: --------: syntax error: operand expected (error token is "-")

So as I understand it takes the exception "------" and still sees it as sort of input.

So, what am I missing?

Upvotes: 1

Views: 46

Answers (1)

KamilCuk
KamilCuk

Reputation: 142080

-eq in test (same in extended test [[...]]) is an operator for integers. '---------' is not an integer. Use = to compare strings.

... [[ "$i" = 'suser' ]] || [[ "$i" = '--------' ]] || [[ "$i" = 'login' ]] || [[ "$i" = 'root' ]]

or simpler

... [[ "$i" = 'suser' || "$i" = '--------' || "$i" = 'login' || "$i" = 'root' ]]

or simpler:

case "$i" in 
suser|--------|login|root) ;; 
*) echo "email text"+ $i | mailx -s "email subject" $EMAILS_LIST; ;;
esac

Side note:
Reading lines from file using for i in $(...) is bad. It's better to use while read -r line; do .... done < <(cut -c 8-13 report_file.csv) or cut -c 8-13 report_file.csv | while read -r line; do ... done see here.

Upvotes: 3

Related Questions