1

I have a file called installer.txt which will contain a line by line of URLs of .tar.gz files like:

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

And I tried:

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

But my output is like:

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 

It should output each line, or, so i thought. Ideas?

1 Answer 1

1

You errors are actually being generated by the assignment statement. You cant have spaces around your assignment. And its good practice to always quote the rhs. So what you want is

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

I also changed you back qoutes to $( ) as this is the recommended way and I think its easier to read too

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. Coming from a JavaScript background mainly and some rails, this syntax is quite strange to me :)
He was also missing the $ when using file -- I see you fixed that as well.
Also, whats the -u mean in the shebang? Googled it, but no avail.
The -u means the script will print an error if you reference an unset variable. Very useful for catching typos. e.g. if you accidentally had echo $wrod it woulf just print blank lines. But with the -u it would print an error message saying wrod is an undefined variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.