为什么我的bash脚本试图将注释作为代码运行?

时间:2021-02-21 01:06:40

I am writing a bash script to run several python programs in a particular order and it looks like this:

我正在编写一个bash脚本,以特定的顺序运行几个python程序,它看起来像这样:

#!/bin/bash
set -e
python prog1.py #make database
python prog2.py #plots
python prog3.py #more plots

This runs fine, but when I comment out the first line:

运行正常,但当我注释掉第一行时:

#!/bin/bash
set -e
#python prog1.py #make database
python prog2.py #plots
python prog3.py #more plots

It crashes with:

它崩溃了:

./python_progs.sh: line 3: plots: command not found

It is as if it is ignoring the '#' in front of 'plots' and is trying to run it as code. Another strange thing is this does not happen all the time, sometimes the second code runs with no problems, sometimes it crashes. Am I missing something basic about how commenting works in bash scripts?

就好像它忽略了'plots'前面的'#',并试图将它作为代码运行。另一个奇怪的事情是这不会一直发生,有时第二个代码运行没有问题,有时它会崩溃。我是否遗漏了关于评论如何在bash脚本中运行的基本内容?

For the people commenting below here is the exact code:

对于下面的评论人员,这里是确切的代码:

#!/bin/bash
set -e
python footprint_hex.py >> ./paper/qso_num.txt #this makes the footpring figures
python sed_db_read.py #makes the inital databases
python sed_db_read2.py #makes the new databases for lum and civ and modles
python sed_db_plots_paper.py #plots
python sed_db_plots_paper_png.py #plots

When no lines are commented it works fine but when commenting out lines 3 and 4 it crashes with:

当没有注释行时它工作正常但是当注释掉第3行和第4行时它会崩溃:

./compile_dbs.sh: line 5: and: command not found

and when commenting out lines 3, 4, and 5 it crashes with :

当评论第3,4和5行时,它会崩溃:

./compile_dbs.sh: line 6: plots: command not found

My exact steps for running the script are:

我运行脚本的确切步骤是:

./compile_dbs.sh

2 个解决方案

#1


0  

I found the problem! I was editing the bash script while it was running, this is what caused the crashes.

我发现了问题!我正在编辑运行时的bash脚本,这就是导致崩溃的原因。

#2


-2  

I'm not entirely sure what's going on, but my guess is that it is interpreting #plots as an argument to script prog2.py

我不完全确定发生了什么,但我的猜测是它将#plots解释为脚本prog2.py的参数

To be safe you might just want to put the comment on a separate line

为了安全起见,您可能只想将评论放在单独的一行上

#1


0  

I found the problem! I was editing the bash script while it was running, this is what caused the crashes.

我发现了问题!我正在编辑运行时的bash脚本,这就是导致崩溃的原因。

#2


-2  

I'm not entirely sure what's going on, but my guess is that it is interpreting #plots as an argument to script prog2.py

我不完全确定发生了什么,但我的猜测是它将#plots解释为脚本prog2.py的参数

To be safe you might just want to put the comment on a separate line

为了安全起见,您可能只想将评论放在单独的一行上