user3732793
user3732793

Reputation: 1979

If then else in Bash for file types

I am new to bash on Linux. I try to simple iterate over files in a directory which can be links or executeables. On Links I mention Link and on other files get file version

for i in *; do
if test -f "$i"
then
  if test -L "$i"
    then
      echo "File $i,Link"
  else
    echo "File $i,"  readelf -a -W $i |grep SONAME
  fi
fi
done

it only workes on the links. How would I do it correctly ? Also some examples mention [ ...] instead of test. What is the difference ?

Thanks for any hint that helps understanding bash !

Upvotes: 0

Views: 68

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140266

use -n to avoid newline after echo (or printf for better portability), and use the readelf command on a separate line or it is interpreted as an argument of echo.

for i in *; do
if test -f "$i"
then
  if test -L "$i"
    then
      echo "File $i,Link"
  else
    printf "File $i,"
    readelf -a -W $i |grep SONAME
  fi
fi

Upvotes: 2

Related Questions