'Get file size in Automator workflow

Creating a service in Automator (Run shell script > Bash) to copy to clipboard filesize (in bytes) of selected file in Finder. I'm not very well versed in bash, and can't see where this is wrong. Aim is to have it appear in the 'services' menu when right clicking a file, and then I can just paste the filesize wherever I fancy. Options I have selected in Automator are:

Service receives selected "files or folders" in "Finder.app"

Shell /bin/bash

Pass input as arguments

for f in "$@"
do
    getty=$(ls -l "$f")
done

IFS=' ' read -a newList <<< "${getty}"

echo -n ${newList[4]} | pbcopy

Run Shell Script failed - 1 error:

-: -c: line1: syntax error near unexpected token `do



Solution 1:[1]

To make it clearer to everyone, the solution is :

  1. create an empty automator script
  2. add action "get selected elements from Finder
  3. add action "Run shell script" with inputs as arguments
  4. with the following script :
 stat -f  '%z' "$@" | pbcopy

Solution 2:[2]

Instead of using ls, use stat to get the file size.

stat -f '%z' "$f" | pbcopy

To copy, say, the name and size of a group of files to the clipboard:

stat -f '%N %z' "$@" | pbcopy

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hugues
Solution 2 chepner