The actual error is that you need to use command substitution. For example, instead of
varYear= $line | cut -d '_' -f 3
you need to use
varYear=$(cut -d '_' -f 3 <<< "$line")
A secondary error there is that $foo | some_command
on its own line does not mean that the contents of $foo
gets piped to the next command as input, but is rather executed as a command, and the output of the command is passed to the next one.
Some best practices and tips to take into account:
- Use a portable shebang line -
#!/usr/bin/env bash
(disclaimer: That's my answer).
- Don't parse
ls
output.
- Avoid useless uses of
cat
.
- Use More Quotes™
- Don't use files for temporary storage if you can use pipes. It is literally orders of magnitude faster, and generally makes for simpler code if you want to do it properly.
- If you have to use files for temporary storage, put them in the directory created by
mktemp -d
. Preferably add a trap to remove the temporary directory cleanly.
- There's no need for a
var
prefix in variables.
grep
searches for basic regular expressions by default, so .mp3
matches any single character followed by the literal string mp3
. If you want to search for a dot, you need to either use grep -F
to search for literal strings or escape the regular expression as \.mp3
.
- You generally want to use
read -r
(defined by POSIX) to treat backslashes in the input literally.