"Text File Busy" when running a simple script on bash

When I make a simple script, I get "Text File Busy" when I try to run it.

dbell@det-ui-supergbe:~$ cat > test2
ls
^Z
[2]+  Stopped                 cat > test2
dbell@det-ui-supergbe:~$ chmod a+x test2
dbell@det-ui-supergbe:~$ ./test2
-bash: ./test2: Text file busy

By hitting Ctrl-Z you suspended the cat, therefore the file is still open and being written to "in the background".

I suspect you want Ctrl-D for 'end of file'.

It's true that Ctrl-Z can mean 'EOF', but in this case, because you're in a terminal that supports background tasks, Ctrl-Z means 'suspend'.

Try this link for more information.