LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Labeling tapes in shell for loop breaking (https://www.linuxquestions.org/questions/linux-general-1/labeling-tapes-in-shell-for-loop-breaking-4175433680/)

asandybox 10-23-2012 09:03 AM

Labeling tapes in shell for loop breaking
 
Hi guys, I am attempting to label some tapes for amanda using amlabel in a for loop. However my for loop appears to be breaking:

Code:

#!/bin/bash

TAPEID=$1
AMLABEL=/usr/sbin/amlabel

#mkdir -p /nfs_backup/amanda/$TAPEID/slots
#for n in `seq 1 15`; do mkdir /nfs_backup/amanda/$TAPEID/slots/slot${n}; done
#chown -R amandabackup.disk /nfs_backup/amanda/$TAPEID/

su - amandabackup -c "for n in $(seq 1 10); do $AMLABEL $TAPEID $TAPEID\-${n} slot ${n}; done"

I have been struggling to accomplish the following one liner:

Code:

su - amandabackup -c "for n in $(seq 1 10); do $AMLABEL $TAPEID $TAPEID\-${n} slot ${n}; done"
I suspect it's the seq command. I can enclose the for loop in single quotes but then the variables won't be substituted.

Here is the error I get upon run:

Code:

[root@amandaserver ~]# ./maketape.sh virtualtape
-bash: -c: line 1: syntax error near unexpected token `2'
-bash: -c: line 1: `2'

Any insights would be helpful.

Thanks

chrism01 10-23-2012 09:16 PM

It'll be easier to debug over several lines
Code:

#!/bin/bash
set -xv
su - amandabackup -c "for n in $(seq 1 10)
do
    $AMLABEL $TAPEID ${TAPEID}-${n} slot ${n}
done

I assumed you were trying to escape the '-' in the middle of that var concatenation. Using ${var} is clearer and probably better.

The set cmd will show you exactly what the parser is doing.

Some links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

asandybox 11-02-2012 04:35 PM

Still banging head
 
So I simplified the script into the following one liner:

Code:

su - amandabackup -c "for n in $(seq 1 15); do echo ${n}; done"
and can run it via the shell. However it still breaks.

odd however because the loop executes fine as a standard user. It's when I use su - that it breaks.

Code:


[root@backup bin]# su - amandabackup -c "for n in $(seq 1 15); do echo ${n}; done"
-bash: -c: line 1: syntax error near unexpected token `2'
-bash: -c: line 1: `2'
[root@backup bin]# for n in $(seq 1 15); do echo ${n}; done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


rknichols 11-03-2012 09:18 AM

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:
  1. 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.
  2. 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.

asandybox 11-05-2012 08:26 AM

Thanks rknichols,

It wasn't until I mulled over your responses 1 & 2 that it became somewhat clear of what was occurring. I guess the confusion comes here:

Code:

-bash: -c: line 1: syntax error near unexpected token `2'
-bash: -c: line 1: `2'

How did you know that variable expansion was occuring in the current shell?

I learned another thing about delayed execution from your post. I never even knew this existed. Thanks for the reply you have helped solve the mystery.

rknichols 11-05-2012 09:13 AM

Quote:

Originally Posted by asandybox (Post 4822567)
How did you know that variable expansion was occuring in the current shell?

Simply because both the command substitution and the variable expansion were not protected by either single quotes or backslash escapes. The first shell that sees them is going to expand them. When you quote those "$" characters by either method, they get passed along literally, and then the next level of shell will see and expand them. You will get used to issues with quoting pretty quickly if you do much script writing.

asandybox 11-07-2012 12:22 PM

Understood, Thanks for the help. Finally got the tape labeling part done.


All times are GMT -5. The time now is 12:25 PM.