1

I've got a text file which looks like this:

b4238ca2-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress b4238f0e-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress b4239058-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress 

NOTE: username, password and ip address have all been changed, so as not expose sensitive info, they are real in my text file I'm using.

I've written the following bash scripting to read line by line and put each item into an array. Previously this worked, however now it is within a process substitution, it's not longer working. I'm assuming it's to do with how I am accessing the array elements, but I'm not sure what is wrong. Snippet below:

i=0 declare -a devices while read line do devices[i]=$line ((i++)) done < test_data.txt timestamp=$(date +"%s") echo "${devices[@]}" IFS="," declare -a devarr for device in "${devices[@]}" do devarr=($device) read dateStrNew dateStrOld < <(curl -k -q "${devarr[1]}://${devarr[2]}:${devarr[3]}@${devarr[4]}/camerainfo" | html2text | gawk '/Newest Sequence/ { new=$3" "$4 }/Oldest Sequence/ {old=$3" "$4}END {OFS=","; print new,old }') 

Also, I tried the following based on a combo of some alternative code that was suggested to an earlier question, along with some other code I came across while researching, but it also doesn't work. However, it would massively simplify things if it did. Could this work and is a better solution?

while read -u 3 line do read UUID protocol username passwd ip_address curl_call="${protocol}://${ip_address}:${username}@${passwd}/report" echo $curl_call read dateStrNew dateStrOld < <(curl -k -q "$curl_call" | html2text | gawk'/Newest Sequence/ { new=$3" "$4 }/Oldest Sequence/ {old=$3" "$4}END {OFS=",";print new,old }') done 3< test_data.txt 

Any help much appreciated, as it's going to be a long night regardless! Thanks!

6
  • 1
    Sorry, you have not accepted any answer in the past. See: What should I do when someone answers my question?
    – Cyrus
    CommentedApr 5, 2015 at 10:11
  • while IFS=',' read -r UUID protocol username passwd ip_address ; do ... done < test_data.txt
    – Costas
    CommentedApr 5, 2015 at 10:13
  • @Costas how do I integrate that into my code, I'm only managing to get the first line to read in. while read -u 3 line do while IFS=','; do read -r UUID protocol username passwd ip_address ........ done done ?
    – jewfro
    CommentedApr 5, 2015 at 10:36
  • How to you put code in comments?
    – jewfro
    CommentedApr 5, 2015 at 10:38
  • Hang on, I had my variables the wrong way round, I think i've got it
    – jewfro
    CommentedApr 5, 2015 at 10:53

1 Answer 1

1

Do you understand simple syntax?

while IFS=',' read -r UUID protocol username passwd ip_address do curl_call="${protocol}://${username}:${passwd}@${ip_address}/report" echo $curl_call done < test_data.txt 
7
  • yes, but I haven't managed to encapsulate it correctly within the outer while read line ; do ... done
    – jewfro
    CommentedApr 5, 2015 at 11:29
  • while read line do; while IFS=',' read -r UUID protocol username passwd ip_address do curl_call="${protocol}://${username}:${passwd}@${ip_address}/report" echo $curl_call done done < test_data.txt with only one do done, I get a syntax error
    – jewfro
    CommentedApr 5, 2015 at 11:30
  • @jewfro Everything what you have done should be placed between do and done without any extra while ... done
    – Costas
    CommentedApr 5, 2015 at 12:22
  • So sorry, I have another problem. I already had set the OFS to "," in the gawk command, as date was splitting at first space. With the url hardcoded it was working fine to read both dates in the way. After adapting it with your code, it's not working again while IFS=',' read -r UUID protocol username passwd ip_address do curl_call="${protocol}://${username}:${passwd}@${ip_address}/report" echo $curl_call read dateStrNew dateStrOld < <(curl -k -q "$curl_call" | html2text | gawk '/Newest Sequence/ { new=$3" "$4 }/Oldest Sequence/ {old=$3" "$4}END {OFS=","; print new,old }')
    – jewfro
    CommentedApr 5, 2015 at 16:53
  • 1
    @jewfro Let me to see 2 row in curl -k -q "$curl_call" | html2text with /Newest Sequence/ and /Oldest Sequence/
    – Costas
    CommentedApr 5, 2015 at 17:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.