LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-23-2012, 09:03 AM   #1
asandybox
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Rep: Reputation: Disabled
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
 
Old 10-23-2012, 09:16 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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/
 
Old 11-02-2012, 04:35 PM   #3
asandybox
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-03-2012, 09:18 AM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
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.
 
Old 11-05-2012, 08:26 AM   #5
asandybox
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
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.

Last edited by asandybox; 11-05-2012 at 08:28 AM.
 
Old 11-05-2012, 09:13 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by asandybox View Post
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.
 
1 members found this post helpful.
Old 11-07-2012, 12:22 PM   #7
asandybox
LQ Newbie
 
Registered: Jun 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Understood, Thanks for the help. Finally got the tape labeling part done.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Loop - Breaking with hanging file descriptor caylan Programming 2 05-13-2012 09:55 AM
Breaking a loop from the keyboard. stf92 Programming 7 01-31-2012 05:31 PM
gdb with fork() - not breaking, following multiple child processes in loop TheMac Programming 2 11-02-2011 08:38 AM
problem with comparison and breaking out of loop java xskycamefalling Programming 3 10-12-2010 09:11 AM
Python, breaking out of infinite loop edM Programming 7 05-05-2009 11:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration