Shell script not running, command not found

后端 未结 12 1799
天涯浪人
天涯浪人 2020-12-03 04:37

I am very, very new to UNIX programming (running on MacOSX Mountain Lion via Terminal). I\'ve been learning the basics from a bioinformatics and molecular methods course (we

相关标签:
12条回答
  • 2020-12-03 05:29

    Make sure you are not using "PATH" as a variable, which will override the existing PATH for environment variables.

    0 讨论(0)
  • 2020-12-03 05:31

    Also make sure /bin/bash is the proper location for bash .... if you took that line from an example somewhere it may not match your particular server. If you are specifying an invalid location for bash you're going to have a problem.

    0 讨论(0)
  • 2020-12-03 05:35
    #! /bin/bash
      ^---
    

    remove the indicated space. The shebang should be

    #!/bin/bash
    
    0 讨论(0)
  • 2020-12-03 05:37

    Also try to dos2unix the shell script, because sometimes it has Windows line endings and the shell does not recognize it.

    $ dos2unix MigrateNshell.sh
    

    This helps sometimes.

    0 讨论(0)
  • 2020-12-03 05:37

    First:

    chmod 777 ./MigrateNshell.sh
    

    Then:

    ./MigrateNshell.sh
    

    Or, add your program to a directory recognized in your $PATH variable. Example: Path Variable Example Which will then allow you to call your program without ./

    0 讨论(0)
  • 2020-12-03 05:39

    Unix has a variable called PATH that is a list of directories where to find commands.

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/david/bin
    

    If I type a command foo at the command line, my shell will first see if there's an executable command /usr/local/bin/foo. If there is, it will execute /usr/local/bin/foo. If not, it will see if there's an executable command /usr/bin/foo and if not there, it will look to see if /bin/foo exists, etc. until it gets to /Users/david/bin/foo.

    If it can't find a command foo in any of those directories, it tell me command not found.

    There are several ways I can handle this issue:

    • Use the commandbash foo since foo is a shell script.
    • Include the directory name when you eecute the command like /Users/david/foo or $PWD/foo or just plain ./foo.
    • Change your $PATH variable to add the directory that contains your commands to the PATH.

    You can modify $HOME/.bash_profile or $HOME/.profile if .bash_profile doesn't exist. I did that to add in /usr/local/bin which I placed first in my path. This way, I can override the standard commands that are in the OS. For example, I have Ant 1.9.1, but the Mac came with Ant 1.8.4. I put my ant command in /usr/local/bin, so my version of antwill execute first. I also added $HOME/bin to the end of the PATH for my own commands. If I had a file like the one you want to execute, I'll place it in $HOME/bin to execute it.

    0 讨论(0)
提交回复
热议问题