Linux 文件类型的Bash中的If-then-else

Linux 文件类型的Bash中的If-then-else,linux,bash,Linux,Bash,我是Linux上bash的新手。我尝试简单地迭代目录中的文件,这些文件可以是链接或可执行的。在链接上我提到链接,在其他文件上得到文件版本 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 它只对链接有效。 我该如何正确地做呢?

我是Linux上bash的新手。我尝试简单地迭代目录中的文件,这些文件可以是链接或可执行的。在链接上我提到链接,在其他文件上得到文件版本

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
它只对链接有效。 我该如何正确地做呢? 还有一些例子提到[…]而不是测试。有什么区别


感谢您提供的任何有助于理解bash的提示

使用
-n
避免echo后换行(或
printf
以提高可移植性),并在单独的行上使用
readelf
命令,或将其解释为
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

使用
-n
避免echo后换行(或
printf
以提高可移植性),并在单独的行上使用
readelf
命令,或将其解释为
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

echo“File$i”
readelf-a-W$i | grep SONAME
应该在两行上。
echo“File$i”
readelf-a-W$i | grep SONAME
应该在两行上。差不多了,谢谢!只是对于某些链接类型,我没有得到任何链接或文件版本
echo-n
是不可移植的。正确的解决方法是
printf
@tripleee并不清楚这一点。编辑我的文章以确保正确性。谢谢。另外,最好使用
printf“%s”文件$i,
,以防
$i
本身包含百分号。差不多了,谢谢!只是对于某些链接类型,我没有得到任何链接或文件版本
echo-n
是不可移植的。正确的解决方法是
printf
@tripleee并不清楚这一点。编辑我的文章以确保正确性。谢谢。另外,如果
$i
本身包含百分号,最好使用
printf“%s”文件$i,