AK10
AK10

Reputation: 13

shell script to Output details of all the files in a folder passed as command line argument

So I am trying to write a script in which I will pass folder name as command line argument. The script will search a the files within that folder if it exists and output all the details. My script is as shown

for entry in "$1"/*
do
  data = $(stat $entry)
  echo  "${data}"
done

when I run it gives me following output.

./test.sh: line 3: data: command not found

I have taken the idea from here: Stat command Can someone help what is wrong
I have tried variations like making $entry to entry and echo $data

Upvotes: 0

Views: 62

Answers (1)

vdavid
vdavid

Reputation: 2544

Your main issue is that you are putting space characters around the equal operator, you must remove them:

  data=$(stat $entry)

It is also good practice to pass variables between quotes in case your folder or any of the filenames contains whitespaces:

  data=$(stat "$entry")

I assume that you are storing the value into a variable because your intent is to use it into an algorithm. If you just want to call stat to list your files, you can simply call:

stat "$1"/*

Upvotes: 2

Related Questions