If you do a "set -x" in your current shell, the problem becomes obvious:
Code:
$ su - rnichols -c "for n in $(seq 1 15); do echo ${n}; done"
++ seq 1 15
+ su - rnichols -c 'for n in 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15; do echo ; done'
Password:
-bash: -c: line 1: syntax error near unexpected token `2'
-bash: -c: line 1: `2'
You have two issues:
- Your variable expansions are being performed in the current shell, and the result of that expansion, including whatever happens to be the current value of n, is passed as a string to su.
- The default separator for the seq command is a newline.
For the simplified command using "echo", you can get the correct result by enclosing the entire string in single quotes rather than double. Since you don't want to do that for your actual
amlabel command, just delay the expansion of the relevant "$" signs by escaping them with a backslash:
Code:
su - amandabackup -c "for n in \$(seq 1 10); do $AMLABEL $TAPEID $TAPEID-\${n} slot \${n}; done"
Now the running of the
seq command and the substitutions for $n occur in the context of the "for" loop, not in the parent shell.
Note: there is no need to escape the "-" sign as it is not a legal character for a variable name.