如何在像`ln -s TARGET DIRECTORY`这样的目录中创建符号链接呢?

时间:2016-06-15 20:46:01

标签: perl

我正在尝试使用Perl脚本在目录中创建一个软链接。

我可以使用public class Solution { public static boolean checkPalindrome(String str, int start1, int end1 ){ int start = start1; int end = end1 - 1; int half = end/2; for(int i = 0; i < half; i++, start++, end-- ){ if(str.charAt(start) != str.charAt(end)) return false; } return true; } public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc=new Scanner(System.in); int test=sc.nextInt(); sc.nextLine(); while(test-->0){ String str=sc.nextLine(); int i=0; int j=str.length()-1; if(checkPalindrome(str, 0, str.length())){ System.out.println("-1"); } else{ while(i<j){ if(str.charAt(i)!=str.charAt(j)){// checks if char at j and i are not same if(str.charAt(i+1)==str.charAt(j)){//if not same then check for char at i+1 , eg cbcb: 0 and 3 are not same, so check for 1 and 3. If they are same then check if the string from new position is palindrome. If it is then 0 is the position to be removed. else not possible as you are to only remove once if(checkPalindrome(str,i+1,j)){ System.out.println(i); break; } else{ System.out.println("-1"); break; } } else if(str.charAt(i)==str.charAt(j-1)){//same as above but now for j. if(checkPalindrome(str,i,j-1)){ System.out.println(j); break; } else{ System.out.println("-1"); break; } } } i++; j--; } } /* else{ System.out.println("not palindrome"); }*/ } } } public static void sendViewToBack(View child) { final ViewGroup parent = (ViewGroup)child.getParent(); if (parent!=null) { parent.removeView(child); parent.addView(child, 0); } } 目录中创建指向ln的链接,如下所示:

/home/apuntes

但我不知道如何用Perl做到这一点。我试过了

/home/scripts

但它没有创建链接,也没有错误。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

Perl的symlink并不支持以ln的方式传递目录而不是链接名称。您必须传递链接名称:

symlink('/home/apuntes', '/home/scripts/softlinkname');

答案 1 :(得分:2)

你试过symlink('/home/apuntes', '/home/scripts')吗? (你的问题在没有/在家之前提到它。)它应该有效,成功时返回1,失败时返回0(并设置$!)。

但通常只有root才能拥有/home的写入权限;你是以root身份运行的吗?

这对我来说很好用:

sudo perl -wE'say symlink("/home/ysth","/home/xysth") || "Error: $!"'

第一次打印1,第二次打印“错误:文件存在”。

运行sudo strace perl -wE'say symlink("/home/ysth","/home/xysth") || "Error: $!"'给出(在很多其他事情之后):

symlink("/home/ysth", "/home/xysth")    = 0
write(1, "1\n", 21
)                      = 2

并且第二次运行给出:

symlink("/home/ysth", "/home/xysth")    = -1 EEXIST (File exists)
write(1, "Error: File exists\n", 19Error: File exists
)    = 19

尝试时会发生什么?

答案 2 :(得分:-1)

怎么样?
  

system(“ln -s / home / apuntes / home / scripts”);

相关问题