How to start multiple processes in Bash

前端 未结 5 527
臣服心动
臣服心动 2020-12-24 01:00

I want to start 100 processes in bash, but the for statement doesn\'t seems to like the & symbol and I\'m getting a syntax error,

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 01:16

    The only reason I can think of why this wouldn't work is if you were really using some other shell, like /bin/sh.

    Do you have #!/bin/bash at the top of your file? If yes, please change it to #!/bin/bash -x (to turn on tracing, or xtrace as it's called in the manual page) and paste the relevant output into your question, along with the exact syntax error that is occurring. If no, that might be your problem. ;-)

    The other possibility I can think of is if you have ^M characters (DOS line endings) in your file, which might result in errors such as the following (depending on which line they are on, if they are scattered around, or depending on if the script starts with a #! line):

    -bash: ./myscript.sh: /bin/bash^M: bad interpreter: No such file or directory
    '/myscript.sh: line 2: syntax error near unexpected token `do
    

    This page has a nice perl snippet that can remove them, as follows (which I have modified slightly so it will work in the unlikely case that you have a stray ^M in the middle of a line):

    perl -pi -e 's/\r//g' myscript.sh
    

提交回复
热议问题