Make multiples links with a parameter (Bash & Perl)
Hi, I try to make several links (all files in /my_path/*) in one location (/usr/local/bioinfo) in one step, to do that I create a script "links"
I try this, and that work PHP Code:
Code:
./links -path /my_path/* PD: Somebody can say me how do that in perl, I have to use STDIN but the rest I have no idea. Thanks DNL |
Say you save the following script as args:
Code:
#!/bin/bash The shell will first apply parameter expansion. It sees that the second item is a glob pattern, and will expand it to the list of all matching files. If you have files /somewhere/one and /somewhere/two, then the output will be Code:
Argument "/somewhere/one" _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ I would approach the problem in a different way. I'd expect my script to take at least two parameters, the first parameter being the directory to create the links in, and all others would be the desired symlink targets, absolute or relative to current directory. You could then use tab expansion in most shells to find both the link directory, and the symlink target files. The one issue with that approach is that both are specified as relative to current directory, whereas for the ln command, the symlink target is always relative to the symlink itself. In other words, we get/know some-path-to-A and some-path-to-B, but need to supply from-A-to-B to ln -s. The beginning of the script is simple. If there are not enough parameters, then output usage. (Although I am using Bash, I tend to use POSIX shell idioms. Others will recommend Bash-specific replacements. If you only use Bash for scripting, go with Bash; I'm a crufty curmudgeon.) Code:
#!/bin/bash Code:
# Pop the link directory from the parameter list. Code:
# Find out the path to link directory. If it is not a real directory, abort. Code:
# Loop over all other arguments. (The link directory was shifted out.) Code:
# Filename part of target, Code:
# The list of directories needed to ascend first from link dir, Code:
# Remove leading common directories from uplist and downlist. Next, we can construct the actual path. Start with ./ so that there will always be a trailing slash. We can remove it after the path is constructed: Code:
# The symlink starts at the link directory. Code:
# Create the symlink; abort if failure. Note that the above script uses POSIX idioms for a reason: it should run using dash too, not just Bash (by changing only the first line to #!/bin/dash ). If you always have Bash at your fingerprints, you can clean up the syntax quite a bit using Bash-only features. Like I said, I'm just stuck in my ways. For now. For debugging, I recommend you modify the second-to-last line to echo ln -s , and perhaps sprinkle some informative echo or printf lines here and there. (printf is supported by both Bash and POSIX shells; use echo only for unformatted string output.) Hope you find this informative, |
I forgot to tell you that I am beginner, can you explain me in simple words
|
what is wrong with:
ln -s my_path/* /usr/local/bioinfo |
Quote:
|
ln will create the entries you specify. To avoid broken links you can execute it directly inside the target directory:
Code:
$ mkdir demo |
All times are GMT -5. The time now is 10:42 PM. |