0

What I basically want to achieve is to type in a custom command in the terminal and a specific shell script should run each time.

I could achieve the above requirement with folders,by modifying the bashrc file like below

alias myScripts="cd /home/arun/Desktop/scripts"

Now when I try to do the same with a bash script by modiying the bashrc file as given below,

alias masterScript="bash /home/arun/Desktop/scripts/myMasterScript.sh"

now when I type masterScript im getting the following error:

"bash : No such file or directory" error

How can I correct this?

  • I think i may have an answer but let me make sure I have this correct .. when you ... alias myScripts in bashrc it will open the folder path but if you alias it to a script it says no file... is this right? – John Orion May 5 '16 at 6:40
  • 3
    I do not understand why this would be preferable to just put your script in your $PATH. If you move your script to ~/bin and add ~/bin to your path variable you can execute it from everywhere. – Bruni May 5 '16 at 6:43
  • Or if you don't want to change $PATH linking the script into a directory that is already in $PATH, i.e. ln -s /home/arun/Desktop/scripts/myMasterScript.sh /usr/local/bin/ – con-f-use May 5 '16 at 14:56
  • Sounds like the problem is/was that /home/arun/Desktop/scripts/myMasterScript.sh just didn't exist - typo? – Xen2050 May 5 '16 at 15:01
3

Just create a function:

function masterScript()
{
    if [ -e /home/arun/Desktop/scripts/myMasterScript.sh ]
    then
        bash /home/arun/Desktop/scripts/myMasterScript.sh
    fi
}

And make sure your script is executable:

chmod 755 /home/arun/Desktop/scripts/myMasterScript.sh
  • That's the way i would do it. +1 . There's no need for script , if you can achieve same action in few lines of code. Functions are ideal for that. – Sergiy Kolodyazhnyy May 5 '16 at 14:59
1

I tried this out .. I simply made it

alias masterScript="cd /home/arun/Desktop/scripts && myMasterScript.sh"
  • sorry you are correct .. i updated my answer .. missed the ending quote :( – John Orion May 5 '16 at 14:46
  • a bash terminal will display a newline & > waiting to close an open quote, makes it easy to catch. anyway, i'll del my old comment, and maybe this one tomorrow. +1 – Xen2050 May 5 '16 at 14:58
1

Make sure your masterScript.sh is executable by doing:

chmod +x masterScript.sh

If the script is not executable, the bash command cannot run it and you'll get these kind of error messages.

  • This is not true - try it with bash non-executable-file and bash will run it – Xen2050 May 5 '16 at 12:22
  • Ah right.. the +x permission seems to only matter if run via ./masterScript.sh – a25bedc5-3d09-41b8-82fb-ea6c353d75ae May 5 '16 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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