Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have my shell script ready to be run.

But my problem is when I need user to run my shell script and not the Ubuntu's built-in file

For example, I want user to run my passwd shell script and not Ubuntu's passwd at /usr/bin/passwd

I have written alias on the .bashrc which look like this

alias passwd='./passwd'

My question is how do I make user to run my passwd instead of Ubuntu's passwd? How do I disable the real passwd file?

Off-topic question, how do I override sudo?
Because my shell script contains sudo which will print out the required password at first run.

share|improve this question
2  
Overriding that type of utility is generally a really bad idea. Why don't you simply call your own version differently and put it in your $PATH? –  Mat Aug 23 '13 at 9:16
    
I need too as the assignment requirement. It's about Computer Security where I need to make user think he/she is running the real command while truth is they're running my command. –  Unknown Aug 23 '13 at 11:25
add comment

2 Answers

You have to provide the name for the alias, like alias passwd='/home/Unknown/bin/passwd'. This is often done by default for some commands like ls or grep, like alias ls='ls --color=auto'. Some background in here.

To run the original command and not the alias, you might use command passwd stuff-to-do.

That being said, substituting security-related binaries is a dumb idea.

EDIT: .bashrc is not read in non-interactive shell by default, that is when running a script, a shell used by the script does not read the .bashrc file. You might use a function for that in .bashrc and then export it, like:

passwd(){
    /home/Unknown/bin/passwd
}
export -f passwd

I did not test that with passwd, but it should work.

Some of the details come from this question's answers.

share|improve this answer
    
Oh, I'm so sorry for not double checking my question. My alias is perfect with the name as well and also the diretory. Just need to access my passwd instead of Ubuntu's passwd –  Unknown Aug 23 '13 at 11:24
    
Did passwd(){ passwd } export -f passwd on my .bashrc but still the real passwd override my passwd file. Or did I wrote wrong? My file is at the root directory. –  Unknown Aug 23 '13 at 12:21
    
@Unknown test with type passwd. Also, do you do source ~/.bashrc or perhaps open a new terminal to check? If not, editing a file is not enough to make the alias work. –  moon.musick Aug 23 '13 at 13:44
add comment

As said in previous answers, best practice is to not name your script with the same name of an already existing system file/command.

If you really have to call it like that, then perhaps at least add the .sh to diversify a bit, e.g. call you script 'passwd.sh' (in general, also a best practice for shell scripts) so you minimize chances of confusion.

As for your question, I would suggest to create a bin directory under your home directory or perhaps (if you have rights) place it in /usr/local/bin and then amend the user PATH variable so that the scripts in the chosen location are found/accessible, for example:

$ cd; mkdir bin
$ export PATH="$HOME/bin:/usr/local/bin:$PATH"

Note that the PATH variable may already contain those locations, best check beforehand via:

$ echo $PATH
share|improve this answer
    
I have to name my file the same with passwd because my assignment is Computer Security related where I need to make user think he/she is running the real command while truth is they're running my command. My problem is overriding the real command with alias –  Unknown Aug 23 '13 at 11:29
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.