繁体   English   中英

在BASH中逐行循环文件内容

[英]Looping file contents line by line in BASH

我有一个名为installer.txt的文件,该文件将逐行包含.tar.gz文件的URL,例如:

http://oscargodson.com/projects/example1.tar.gz
http://oscargodson.com/projects/anotherexample.tar.gz
http://oscargodson.com/projects/onemore.tar.gz 

我尝试过:

#!/bin/bash
file = `cat installs.txt`
for word in file; do
  echo $word
done

但是我的输出是这样的:

Oscars-MacBook-Air:Desktop oscargodson$ ./test.sh
=:                                                     cannot open `=' (No such file or directory)
http://oscargodson.com/projects/example1.tar.gz:       cannot open `http://oscargodson.com/projects/example1.tar.gz' (No such file or directory)
http://oscargodson.com/projects/anotherexample.tar.gz: cannot open `http://oscargodson.com/projects/anotherexample.tar.gz' (No such file or directory)
http://oscargodson.com/projects/onemore.tar.gz:        cannot open `http://oscargodson.com/projects/onemore.tar.gz' (No such file or directory)
file

它应该输出每一行,或者,所以我想。 想法?

您的错误实际上是由赋值语句生成的。 您的作业周围不能有空格。 始终引用rhs及其优良作法。 所以你想要的是

#!/bin/bash -u
file="$(cat installs.txt)"
for word in $file; do
  echo $word
done

我也将您的qoutes改回了$( )因为这是推荐的方式,我认为它也更容易阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM