Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know this must be sound a weird question, but for me, that always developed in C, C++ and Java (and now in bash) seems more weird tough. I'm trying to check if md5 and filename from two list (one from my remote server and another from my local server) matches. This is what I did so far:

#!/bin/bash
datacenter="amazon"
hostname=`hostname`;
path="backup/server245"

s3=`s3cmd ls --list-md5 s3://company-backup/company/"$datacenter"/"$hostname"/"$path"/`;
s3_list=$(echo "$s3" | tr -s ' ' | cut -d ' ' -f 4,5 | sed 's= .*/= =');
echo "$s3_list"

locally=`md5sum /"$path"/*.gz`;
locally_list=$(echo "$locally" | sed 's= .*/= =');
echo "$locally_list";

This give me the follow output:

S3 LIST
d41d8cd98f00b204e9800998ecf8427e 
41eae9b40d23de2f02bf07635870f6d0 app.20121117040001.gz
31d90af7969f5003b27f68e27e7f2cb1 app.gz

LOCALLY LIST
31d90af7969f5003b27f68e27e7f2cb1 app.gz

I'm trying to create two for's to do the checking, but I'm having some problem in interact through the list in for with bash. Below the code that I provide above is:

for i in "$s3_list"
do
  echo "$i"
  echo "-----------"
done

Which is given the follow output:

d41d8cd98f00b204e9800998ecf8427e 
41eae9b40d23de2f02bf07635870f6d0 app.20121117040001.gz
31d90af7969f5003b27f68e27e7f2cb1 app.gz
-----------

which it should be something like:

d41d8cd98f00b204e9800998ecf8427e 
-----------
41eae9b40d23de2f02bf07635870f6d0 app.20121117040001.gz
-----------
31d90af7969f5003b27f68e27e7f2cb1 app.gz
-----------

Which means I'm interacting with the lines one by one, not all together as it shows above.

What I'm doing (understading wrong about bash) here ?

share|improve this question
1  
I apologize that I am heading out and don't have the time to get you the answer, but I think I see the issue. When you are making the s3 list, you are making it with the output of your command. But that doesn't make it an array, or other iterable object. So as far as it knows, there is one item in the object, not 3 (as in your current expectation). That looks to be the case, but I am heading out - so hopefully someone else will be along shortly to help further (or maybe that gets you on the track). –  nerdwaller May 23 '13 at 14:35
    
@nerdwaller, No problem dude, thank you for your comment, now I understand what was wrong, with your comment and the choosed answer. –  Valter Henrique May 23 '13 at 14:45

1 Answer 1

up vote 4 down vote accepted

A double quoted variable (such as "$s3_list") gets treated as a single argument, no matter if it contains spaces or newlines.

Just dropping the double quotes won't produce the desired result either, as the items will now be split by spaces as well. To overcome this, set the internal file separator to newline only:

IFS=$'\n'

for i in $s3_list
   ...
done

unset IFS
share|improve this answer
2  
OLDIFS=$IFS; IFS=$'\n'; set -f; for ... done; IFS=$OLDIFS; set +f –  Uwe May 23 '13 at 14:42
    
Thank you sir! I didn't know about using IFS, thank you very much! –  Valter Henrique May 23 '13 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.