问题描述:

i made a simple script to check if a directory exist and if is or is not a link. But it is not working and i don't know why. Help is requested.

if [ -d "$test"] ; then

if [ -L "$test"] ; then

echo "1 is a symlink link"

else

echo "DIR 1 found"

fi

fi

if [ -d "$test01"] ; then

if [ -L "$test01"] ; then

echo "2 is a symlink link"

else

echo "DIR 2 found"

fi

fi

from terminal :

-rwxrwxr-x 1 zero zero 317 Feb 3 18:22 dir_check.sh

-rwxrwxr-x 1 zero zero 339 Feb 3 18:17 dir_check.sh~

drwxrwxr-x 2 zero zero 4096 Feb 3 11:15 test

lrwxrwxrwx 1 zero zero 11 Feb 3 18:18 test01 -> /home/zero/

[email protected]_pc:~/Desktop/bash_pro$ ./dir_check.sh

1 is a symlink link

2 is a symlink link

The first 1 should have been "DIR 1 found"

网友答案:

This should do the trick.

if [ -d "test" ] ; then
  if [ -L "test" ] ; then
     echo "1 is a symlink link"         
  else
     echo "DIR 1 found"
  fi  
fi



if [ -d "test01" ]; then
  if [ -L "test01" ]; then
     echo "2 is a symlink link"         
  else
     echo "DIR 2 found"
  fi
fi

Explanation: $test or $test01 refers to the value of variable with name test and test01, not the actual directory names. As commenters pointed out, your code misses a space before the closing ], which is the culprit here, plus the fact that you expand non-existing variables, rather than passing strings.

相关阅读:
Top