Automatic Ubuntu terminal commands get cut

huangapple go评论68阅读模式
英文:

Automatic Ubuntu terminal commands get cut

问题

首先,我不是Ubuntu专家,我几乎没有使用过它的终端,所以可能有更好的方法来执行这个任务。

我试图在多个.fits文件上执行这个命令:

astmkcatalog ${filename} --ids --ra --dec --magnitude --sn --zeropoint=${zeropoints} --brightness --clumpbrightness --brightnessnoriver --riverave --rivernum --clumpscat

问题是每个文件都有一个特定的"zeropoint"值。我将这些值保存在一个.txt文件中,每个值占据一行,所以我希望每次运行该命令时都能访问这些行中的每一个。我创建了这个.sh文件来实现这一目标(.fits文件被排序,以便第一个传递的文件对应于第一行中的zeropoint,所以没有问题)。

我找到了这两种访问.txt文件行值的方法(上面的awk和sed),它们都有相同的问题。当我运行时,只有最后一个.fits文件正常工作。

为了看到问题,我在代码中添加了注释的echo行,当我取消注释它们并注释掉我想要运行的实际命令(astmkcatalog...),我得到了这个结果:

如你所见,它只传递了最后一个的zeropoint值。此外,当我再次运行代码时,会出现以下情况:

在执行.sh文件两次后,它变得混乱。首先,问题被理解,它没有传递第一次命令的zeropoint值,但现在,当我第二次运行完全相同的.sh文件时,它以某种原因重新排列命令。

显然,我做错了什么,但我不知道是什么。我猜测awk和sed方法会中断进程,所以我尝试将它们的结果保存在zeropoints变量中,但没有成功。

简而言之:我如何能够自动运行几乎相同的命令,但使用.txt文件中的不同值作为变量的每个命令的值?

我尝试了不同的方法(awk和sed)来访问.txt文件中特定行的值。

我尝试将它们的结果保存在一个变量中。

将zeropoint=""命令的输入放在最后没有解决问题。

解决方案

感谢tripleee的评论,我找到了问题。正如他们提到的,这是一个DOS回车符的问题。

在这篇帖子https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings中解释了这个问题。只需在终端中运行以下命令来处理我的.txt文件:

dos2unix zeropointsfile.txt

这将删除每行末尾的不可见的^M并解决了问题。

谢谢!

英文:

First of all, I'm not an Ubuntu expert and I have barely used its terminal so there might be a better way to do this.

I'm trying to execute this command over several .fits files:

astmkcatalog ${filename} --ids --ra --dec --magnitude --sn --zeropoint=${zeropoints} --brightness --clumpbrightness --brightnessnoriver --riverave --rivernum --clumpscat

The thing is each file has a specific "zeropoint" value. I have these values saved in a .txt file being each value a row so I want to access to each of this rows every time I run the command. I created this .sh file to do so (the .fits files are ordered so that the first one to pass is the one corresponding to the zeropoint in the first row, so no problem there):

Automatic Ubuntu terminal commands get cut

I found these 2 methods to access the row values of the .txt files (the awk and sed above), both of them have the same problem. When I run this only the last .fits works properly.

In order to see the problem I added the commented echo lines in the code and when I uncomment them and comment the actual command I want to run (astmkcatalog...) I get this:

Automatic Ubuntu terminal commands get cut

As you can see it only passes the zeropoint value for the last one. Also, when I run the code again this happens:

Automatic Ubuntu terminal commands get cut

Here it gets crazy. First the problem was understood, it is not passing the zeropoint values for the first commands but now, when I run the exact same .sh file a second time it disorders the command for some reason.

I'm obviously doing something wrong but I don't know what, I guess the awk and sed methods are stopping the process so I tried to save their result in the zeropoints variable, but it doesn't work.

TL;DR: How can I automatically run almost the same command but with a different value for a variable having this value for each command saved as a row in a .txt file?

I tried different methods (awk and sed) to access the values of specific rows in a .txt file.

I tried to save their result in a variable.

Placing the zeropoint="" input of the command at the end doesn't solve the problem.

SOLUTION:

Thanks to tripleee comments I could find the issue. As they mentioned it was a DOS carriage problem.

In this post https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings the issue is explained. Just passing in the terminal to my .txt file:

dos2unix zeropointsfile.txt

This removed the invisible ^M at the end of each line and solved the problem,

THANKS!!

答案1

得分: 0

Reading the input file repeatedly to align each line with a line number is rather inelegant and inefficient. See also https://stackoverflow.com/questions/65538947/counting-lines-or-enumerating-line-numbers-so-i can loop over them - why is this?

Instead, I would refactor the Awk command to output pairs of values, and then simply loop over those.

  FNR==1 { print z[++n], FILENAME; nextfile }' zeropoints.txt /path/*.fits |
while read -r zp file; do
    echo \
    astmkcatalog "$file" --ids --ra --dec --magnitude --sn \
      --zeropoint="$zp" --brightness --clumpbrightness \
      --brightnessnoriver --riverave --rivernum --clumpscat
done

(I left in an echo so it doesn't really do anything, just print the command it would execute. If it looks correct, take out the echo line.)

I don't believe this will solve your problem, as such; but at least it should hopefully steer you in the right direction.

If the input file contains DOS line endings, run dos2unix on it, or add sub(/\r/, ""); to the Awk script.

英文:

Reading the input file repeatedly to align each line with a line number is rather inelegant and inefficient. See also https://stackoverflow.com/questions/65538947/counting-lines-or-enumerating-line-numbers-so-i-can-loop-over-them-why-is-this

Instead, I would refactor the Awk command to output pairs of values, and then simply loop over those.

awk 'NR==FNR { z[NR] = $1; next }
  FNR==1 { print z[++n], FILENAME; nextfile }' zeropoints.txt /path/*.fits |
while read -r zp file; do
    echo \
    astmkcatalog "$file" --ids --ra --dec --magnitude --sn \
      --zeropoint="$zp" --brightness --clumpbrightness \
      --brightnessnoriver --riverave --rivernum --clumpscat
done

(I left in an echo so it doesn't really do anything, just print the command it would execute. If it looks correct, take out the echo line.)

I don't believe this will solve your problem, as such; but at least it should hopefully steer you in the right direction.

If the input file contains DOS line endings, run dos2unix on it, or add sub(/\r/, ""); to the Awk script.

huangapple
  • 本文由 发表于 2023年3月3日 21:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定