在不同文件夹中的 bash 脚本中创建符号链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3862179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:44:16  来源:igfitidea点击:

Creating a symbolic link in a bash script in a different folder

bash

提问by Matthieu

My bash script is getting two arguments with folders (that exist and everything).

我的 bash 脚本正在获取两个带有文件夹的参数(存在和所有内容)。

Inside the first one I want to create a link to the second

在第一个里面我想创建一个链接到第二个

Suppose I have the folders /home/matt/a and /home/matt/b, I call the script like this :

假设我有文件夹 /home/matt/a 和 /home/matt/b,我这样调用脚本:

/home/matt # ./my_script ./a ./b

I want to see a symbolic link inside a that points to b

我想在 a 中看到一个指向 b 的符号链接

And of course, just doing

当然,只是做

ln -s  /link

in the script does not work... (it will create a link that looks for a ./b inside a)

在脚本中不起作用...(它将创建一个链接,在 a 中查找 ./b)

This is just a very simple example, I am looking for a script that will be generic enough to take different arguments (absolute or relative path...etc...)

这只是一个非常简单的例子,我正在寻找一个足够通用的脚本来接受不同的参数(绝对或相对路径...等...)

采纳答案by Paused until further notice.

Give this a try:

试试这个:

ln -s "$(readlink -e "")" "/link"

if you have readlink.

如果你有readlink.

Or perhaps this variation on the answer by larsmans:

或者也许是 larsmans 对答案的这种变化:

cd ""
dir=$(pwd)
cd -
ln -s "$dir" "/link"

回答by dogbane

Here's another cute one-liner:

这是另一个可爱的单线:

ln -s `cd \`dirname \`; pwd`/`basename ` /link

回答by Fred Foo

#!/bin/sh
cd 
ln -s "`pwd`" /link