command not found error

hello

every time i run the following code

for val in fileX fileY fileZ
do
$val=`ls -l $val | awk '{print $5}'`
done

i got error message command not found , i tried to add ' and " but nothing works

its only worked wen remove $val=

but i want the name of the file and the value

thanks to advice

I think what you want is something like this:

info="$val $(ls -l $val | awk '{print $5}')"

which will assign the current file name and then the file size into the variable info.

Do as a favor and post what you are trying to accomplish.
The example lines are broken at many levels and figuring out what you are trying to do is not productive.

In any case, to answer your question. $val=`ls -l $val | awk '{print $5}'`

Remove the dereferencing $ in red.

hello

i want the out put like this

fileX=54831 (the number is the file size)
fileY=75778
fileZ=457455

---------- Post updated at 01:22 AM ---------- Previous update was at 01:22 AM ----------

hello

i want the out put like this

fileX=54831 (the number is the file size)
fileY=75778
fileZ=457455

Try:

for val in fileX fileY fileZ
do
  size=`ls -l val | awk '{print $5}'`
  echo "$val=$size"
done
# for val in fileX fileY fileZ; do echo $val=$(stat -c %s $val) ; done
# for val in fileX fileY fileZ; do echo $val=`ls -l $val|awk '{print $5}'`; done

If you prefer awk then..

ls -l file? | awk '{print $NF"="$5}'

its works good ... thanks for your help :smiley:

it does not work..must add a `$` to before the val

size=`ls -l $val | awk '{print $5}'`
1 Like

@ygemici
Yes, you are right. It was so simple so I didn't test my script before posting. My mistake.