user3317519
user3317519

Reputation: 78

What's wrong with my function() in Shell Script! Syntax Token errors near {?

#!/bin/bash

# I get the newest file in Directory

latest_file=$(ls -t | head -n 1)
getAlldoublicate() 

# getting Token syntax error here getAlldoublicate() '{

{
 Alldoublicate=$(tr -s ',' ' ' <latest_file  | awk  '{print $2" "$3" "$4}' | uniq -d) 

# here I try to find dublicate rows in csv

 }
if [[ -s latest_file]] ; then

# here I check if file is emty

getAlldoublicate
else
cat "$latest_file"  | mailx -s "$latest_file is empty" bla..`@bla 
fi

Upvotes: 1

Views: 76

Answers (1)

Paul
Paul

Reputation: 1657

I guess this is your code.

#!/bin/bash

# I get the newest file in Directory
latest_file=$(ls -t | head -n 1)

# getting Token syntax error here
getAlldoublicate()
{
    # here I try to find dublicate rows in csv
    Alldoublicate=$(tr -s ',' ' ' < $1 | awk '{print $2" "$3" "$4}' | uniq -d) 
}


if [[ -s $latest_file ]]; then

# here I check if file is emty
    getAlldoublicate $latest_file
else
    cat $latest_file | mailx -s "$latest_file is empty" bla.. @bla 
fi

Three points you need to pay attention:

  1. function must be defined first before being use.
  2. You can pass the latest_file as an argument when calling getAlldoublicate. Then you could use it by $1 in the function. ($0 stands for the function being called itself).
  3. It would be better if you read the How to Format Tutorials before asking questions.

Upvotes: 1

Related Questions