LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   question about bash command (https://www.linuxquestions.org/questions/linux-general-1/question-about-bash-command-4175499005/)

mia_tech 03-21-2014 02:58 PM

question about bash command
 
I have to questions about this function I found on a script
Code:

function FILESIZE ( )
{
FN=${1:-/dev/null}
if [[ -e $FN ]]
then
# FZ=$(ls -s $FN | cut -d ' ' -f 1)
set -- $(ls -s "$FN")
FZ=$1
fi
}

first:
Code:

FN=${1:-/dev/null}
does this means initialize FN to the value of $1 and if $1 is not pass then set FN TO /dev/null?
second:
Code:

set -- $(ls -s "$FN")
the set command is a bit tricky here what is set being used for and what -- means in this case? I googled it, but the explanation I found was of no help

custangro 03-21-2014 03:02 PM

No, it means that set FN to $1 and if $1 isn't passed then set FN TO /dev/null

metaschima 03-21-2014 04:42 PM

You can get file size using

Code:

stat -c '%s' file

mia_tech 03-21-2014 07:41 PM

Quote:

Originally Posted by custangro (Post 5138864)
No, it means that set FN to $1 and if $1 isn't passed then set FN TO /dev/null

what's the difference from what I said?

grail 03-22-2014 10:15 AM

Yes you were correct on the first point (and yes I see no difference with yours and first reply)

As to the set command, there are actually 2 questions:

1. What does -- do? Answer, for some commands, set being one, -- means that anything received after this will be an argument to the set command. This means that if after -- you should receive something like '-a', which may normally be an a switch for set, that it should now be treated as an argument, example:
Code:

$ set -- -a
$ echo "$1"
-a

2. What is set being used for? Answer, the output from the command (ls -s "$FN"), will undergo word splitting based on IFS and each piece returned will be assigned to a parameter, ie the first piece will be $1. This would seem to be poor code because should any of the files referenced have spaces in them you will have unusual results for parameters, example:
Code:

$ touch 'this is my file'
$ ls
this is my file
$ ls -s
total 0
0 this is my file
$ for f
>do
>    echo "$f"
>done
0
this
is
my
file

As you can see this is not showing the expected output.


All times are GMT -5. The time now is 07:05 PM.