Reputation: 1403
I'm trying to get the size of files in a bash script on OS X. If I explicitly specify the file's name, I get the correct answer. If I try to do this looping thing, I get an incorrect answer. This bash script is part of a folder action in Automator and I need to not set the name explicitly because the contents of the folder will change.
This gives me the correct answer:
function get_file_size {
filesize1=$(stat -f%z "/Users/jcoulson/Nexus Transfer/Test.mpeg")
echo $filesize1
}
for f in "/Users/jcoulson/Nexus Transfer/"
do
get_file_size
done
This gives me an answer of 136:
function get_file_size {
filesize1=$(stat -f%z "$f")
echo $filesize1
}
for f in "/Users/jcoulson/Nexus Transfer/"
do
get_file_size
done
EDIT: The problem must be with stat. If I just put echo "found something"
in place of get_file_size, I see the text echo.
Upvotes: 0
Views: 72
Reputation: 1403
For anyone stumbling across this in the future, here's my finished script:
function get_file_size {
filesize1=$(stat -f%z "$f")
echo $filesize1
}
for f in /Users/jcoulson/NexusTransfer/*
do
get_file_size
done
Upvotes: 0
Reputation: 3806
Variables are OK, but you forgot the *
wildcard, so you'r stat'ing directory instead of file
function get_file_size {
filesize1=$(stat -f%z "$f")
echo $filesize1
}
for f in "/Users/jcoulson/Nexus Transfer/*"
do
get_file_size
done
Upvotes: 2
Reputation: 657
I think what you want is
function get_file_size {
filesize1=$(stat -f%z "$1")
echo $filesize1
}
for f in "/Users/jcoulson/Nexus Transfer/"
do
get_file_size $f
done
$1 refers to the first parameter, and putting $f after get_file_size will call it with $f as the first param
Upvotes: 1