How interact in for with bash?

1

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 ?

Valter Silva

Posted 2013-05-23T14:26:26.650

Reputation: 1 331

1I 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 – 2013-05-23T14:35:28.493

@nerdwaller, No problem dude, thank you for your comment, now I understand what was wrong, with your comment and the choosed answer. – Valter Silva – 2013-05-23T14:45:25.060

Answers

4

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

Dennis

Posted 2013-05-23T14:26:26.650

Reputation: 42 934

2OLDIFS=$IFS; IFS=$'\n'; set -f; for ... done; IFS=$OLDIFS; set +f – Uwe – 2013-05-23T14:42:51.533

Thank you sir! I didn't know about using IFS, thank you very much! – Valter Silva – 2013-05-23T14:44:42.603