我有类似的东西
文件夹 A,当我这样做时ln -s A a
,如果我重复该命令,则现在创建 sym 链接文件夹 a,ln -s A a
我得到死链接 a/A
如果链接存在,那么有没有办法让 ln 失败,然后将所有内容包装在 if exists 语句中?
如果您正在寻找的只是单个命令的单个条件测试,则不需要if
语句\xe2\x80\x94,只需使用list。
根据LESS=+/Lists man bash
:
\n\n\nRun Code Online (Sandbox Code Playgroud)\nA list is a sequence of one or more pipelines separated by one of the\n operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or\n <newline>.\n...\n An AND list has the form\n\n command1 && command2\n\n command2 is executed if, and only if, command1 returns an exit status\n of zero.\n
假设您希望存在该文件夹mydir
并且您想要创建一个链接mylink2dir
,并且您只想在该目录存在时你可以使用:
[ -d mydir ] && ln -s mydir mylink2dir\n
Run Code Online (Sandbox Code Playgroud)\n\n或者等价地:
\n\ntest -d mydir && ln -s mydir mylink2dir\n
Run Code Online (Sandbox Code Playgroud)\n