xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

How to tell whether a file is a symbolic link in shell script All In One

How to tell whether a file is a soft symbolic link in shell script All In One

shell 脚本中如何判断一个文件是否是软链接 / 软符号链接

error

软链接自动指向原文件 bug ❌

# 软链接
$ test ./test.sh -ef ./test-soft-link.sh
$ echo $?
0

# 硬链接 ❌
$ test ./test.sh -ef ./test-hard-link.sh
$ echo $?
0

# test commnad
$ man test

#!/usr/bin/env bash

# 软链接 bug ❌ 自动指向原文件
echo "./test.sh -ef ./test-soft-link.sh"
# if [ ./test.sh -ef ./test-soft-link.sh ]; then
if [[ ./test.sh -ef ./test-soft-link.sh ]]; then
  echo "yes ✅"
else
  echo "no ❌"
fi

echo "./test.sh -ef ./test-hard-link.sh"
# if [ ./test.sh -ef ./test-hard-link.sh ]; then
if [[ ./test.sh -ef ./test-hard-link.sh ]]; then
  echo "yes ✅"
else
  echo "no ❌"
fi



# 单方括号 [] ✅
echo "[ ./test.sh -ef ./test-demo.sh ]"
if [ ./test.sh -ef ./test-demo.sh ]; then
  echo "yes ✅"
else
  echo "no ❌"
fi

# 双方括号 [[]] ✅
echo "[[ ./test.sh -ef ./test-demo.sh ]]"
if [[ ./test.sh -ef ./test-demo.sh ]]; then
  echo "yes ✅"
else
  echo "no ❌"
fi

solutions

# stat commnad
$ man stat

# stat
if [ "$(stat -c %h -- "$file")" -gt 1 ]; then
    echo "File has more than one name."
fi

# check if shell script is a symlink
if [[ -L "$HOME/test-soft-link.sh" ]]; then
  echo "It's a soft link ✅"
else
  echo "It's not a soft link ❌"
fi

if [[ -L /home/eric/test-soft-link.sh ]]; then
  echo "It's a soft link ✅"
else
  echo "It's not a soft link ❌"
fi
if [[ -L "$HOME/test-hard-link.sh" ]]; then
  echo "It's a soft link ✅"
else
  echo "It's not a soft link ❌"
fi

if [[ -L /home/eric/test-hard-link.sh ]]; then
  echo "It's a soft link ✅"
else
  echo "It's not a soft link ❌"
fi

image

eric@rpi4b:~ $ echo $HOME
/home/eric
eric@rpi4b:~ $ pwd
/home/eric
# /home/eric/test-hard-link.sh
# soft symbol link ✅
eric@rpi4b:~ $ [ -L ./test-soft-link.sh ] && [ -e ./test-soft-link.sh ]
eric@rpi4b:~ $ echo $?
0

# hard symbol link ❌
eric@rpi4b:~ $ [ -L ./test-hard-link.sh ] && [ -e ./test-hard-link.sh ]
eric@rpi4b:~ $ echo $?
1

demos

eric@rpi4b:~ $ ls -ali | grep test
 24553 -rw-r--r--  1 eric eric   411  9月 21 11:37 nvm_echo_test.sh
 30221 -rwxr-xr-x  1 eric eric   172  9月 21 18:03 shell-env-variable-test.sh
 30331 -rwxr-xr-x  1 eric eric    47 10月 25 00:26 test-demo.sh
 24592 -rwxr-xr-x  2 eric eric   156  9月 21 13:15 test-hard-link.sh
 24592 -rwxr-xr-x  2 eric eric   156  9月 21 13:15 test.sh
 23662 lrwxrwxrwx  1 eric eric     7 10月  8 16:44 test-soft-link.sh -> test.sh
eric@rpi4b:~ $ test ./test.sh -ef ./test-soft-link.sh
eric@rpi4b:~ $ echo $?
0
eric@rpi4b:~ $ 

image

refs

https://www.cnblogs.com/xgqfrms/p/17786079.html

https://www.cnblogs.com/xgqfrms/p/17728141.html#5221859

https://stackoverflow.com/questions/5767062/how-to-check-if-a-symlink-exists
https://unix.stackexchange.com/questions/167610/determining-if-a-file-is-a-hard-link-or-symbolic-link

https://koenwoortman.com/bash-script-check-if-file-is-symlink/



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-10-25 Linux shell command line editor All In One
2022-10-25 Linux bash sed command All In One
2022-10-25 Perl Language 从入门到放弃 All In One
2021-10-25 微软外服 AlI In One
2020-10-25 Android 开启 WebView 页面 Chrome debug All In One
2020-10-25 Node.js Backend Developer
2019-10-25 css un-regular triangle & css irregular shapes
点击右上角即可分享
微信分享提示