bash – 检查Shell脚本$1是绝对路径还是相对路径

参见英文答案 > Determine if relative or absolute path in shell program4个
正如标题所示,我试图确定我的bash脚本是否收到完整路径或作为参数的目录的相对文件.

由于某些原因,以下对我来说似乎不起作用:

#!/bin/bash

DIR=$1

if [ "$DIR" = /* ]
then
    echo "absolute"
else
    echo "relative"
fi

当我用完整路径或绝对路径运行我的脚本时,它说:

./script.sh: line 5: [: too many arguments
relative

由于某些原因,我似乎无法弄清这个错误.有任何想法吗?

[…]不做模式匹配. / *正在扩展到/,所以你有效的内容
if [ "$DIR" = /bin /boot /dev /etc /home /lib /media ... /usr /var ]

或类似的东西.改用[[…]].

if [[ "$DIR" = /* ]]; then

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...