How do you run bash script as a command?

后端 未结 4 950
离开以前
离开以前 2021-01-22 10:53

I have a bash script, which I use for configuration of different parameters in text files in my wireless access media server.

The script is located in one directory, and

相关标签:
4条回答
  • 2021-01-22 11:10

    You can place a softlink to the script under /usr/local/bin (Should be in $PATH like John said)

    ln -s /path/to/script /usr/local/bin/scriptname
    

    This should do the trick.

    0 讨论(0)
  • 2021-01-22 11:13

    The script needs to be executable, with:

    chmod +x scriptname
    

    (or similar).

    Also, you want the script to be located in a directory that is in your PATH. To see your PATH use:

    echo $PATH
    

    Your choices are: to move (or link) the file into one of those directories, or to add the directory it is in to your PATH.

    You can add a directory to your PATH with:

    PATH=$PATH:/name/of/my/directory
    

    and if you do this in the file $HOME/.bashrc it will happen for each of your shell's automatically.

    0 讨论(0)
  • 2021-01-22 11:15

    Another way is to run it with:

    /bin/bash /path/to/script
    

    Then the file doesn't need to be executable.

    0 讨论(0)
  • 2021-01-22 11:31

    You can write a minimal wrapper in your home directory:

    #!/bin/bash
    exec /yourpath/yourfile.extension
    

    And run your child script with this command ./NameOfYourScript

    update: Unix hawks will probably say the first solution is a no-brainer because of the additional admin work it will load on you. Agreed, but on your requirements, my solution works :)

    Otherwise, you can use an alias; you will have to amend your .bashrc

    alias menu='bash /yourpath/menuScript.sh'
    
    0 讨论(0)
提交回复
热议问题