Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Basically I want to create a quick set of commands so that I can type into the terminal and have it do this:

cd /path/to/folder/of/symlink
sudo rm -f symlink
sudo ln -s /new/path/of/symlink
cd /new/path/of/symlink

I work for a company whose software is housed in a central location and uses symlinks to work around problems created by working out of a subversion repo branch (or whatever else) since the software relies on specific paths to run. My dilemma is that I tend to have to switch back and forth between various branches and the trunk on a regular basis (several times a day). So what I would like to do is at the very least create some type of script I can run in the terminal and enter a unique command example:

./subchange trunk

or

./subchange branch1

I don't mind having to hardcode the above stuff per folder I need to switch to, or making a unique identifier for each to make my life just a little easier, but I'm not sure how to actually create a script so that I can use it as a custom command or what kind of scripting I would need to use for it to work in linux. So any advice/help would be good for me at this point.

share|improve this question
    
What have you tried to do so far? –  Daniel Beck Aug 21 '12 at 17:44
    
To get started, you're probably going to want to look at the topic of "bash shell scripting" for how to write scripts. You might also find the subject of "aliases" in bash a relevant topic. –  Darth Android Aug 21 '12 at 17:44
    
@DanielBeck havent tried anything (yet).. Hence but I'm not sure how to actually create a script so that I can use it as a custom command or what kind of scripting I would need to use for it to work in linux. So any advice/help would be good for me at this point. –  chris Aug 21 '12 at 17:46
    
@DarthAndroid thank you, I will take a look into shell scripting and aliases, that will hopefully lead me on the path I am looking for. –  chris Aug 21 '12 at 17:47
    
Of note, I personally recommend the Advanced Bash Scripting Guide. Don't dig too deep too fast (it gets into really advanced topics), but the introduction and basics assume you know nothing and should help you get started, and it's got great examples. –  Darth Android Aug 21 '12 at 17:51
show 1 more comment

1 Answer

up vote 1 down vote accepted

If a script is executable and its parent directory is in your $PATH, then you can use it as a command. For example you can create a bash script out of the commands you mentioned:

#!/bin/bash

cd /path/to/folder/of/symlink
sudo rm -f symlink
sudo ln -s /new/path/of/symlink
cd /new/path/of/symlink

Then you can save that as a file, subchange, and make it executable with chmod +x subchange. I have a directory of scripts on my system (for example /tech/scripts) that I add to my $PATH. Often times the $PATH environment variable is set in a ~/.profile, ~/.bashrc, /etc/bashrc or similar file. You can add your directory by doing:

export PATH=/tech/scripts:$PATH

If you added that to a startup file you'll need to restart your shell or source it (e.g. source ~/.bashrc). Now you can use subchange on the command line and it will execute those commands. To add an argument to the command you can use the $1 operator. In bash this stands for the first argument, so if I do subchange trunk then $1 will be trunk. You can then add this into your script as needed:

#!/bin/bash

cd /path/to/folder/of/symlink
sudo rm -f symlink
sudo ln -s /new/path/$1/symlink
cd /new/path/$1/symlink

The #! at the top of the file is called a shebang (she move... she move...). For a script on a unix system it tells the system what binary to run the following commands through. In this case we're specifying /bin/bash because we want to execute this file as bash commands and the bash binary is in /bin. This could specify another language though and point to its binary.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.