linux - Bash reading from file and parsing each line -


this question has answer here:

i need manage updates of particular software on several customers' linux servers. updates provided sending .tar.gz files each server , inflating overwriting old software files.

i've put customers' server list in txt file this:

user1@customer-server-1-address:22:/path/to/software/server1:null:customer1name  user2@customer-server-2-address:2222:/path/to/software/server2:/etc/vpnc/client1.conf:customer2name  user3@customer-server-3-address:22:/path/to/software/server3:null:customer3name 

char ":" acts field separator; field 1 server address, 2 port, 3 path software, 4 used tell if vpn tunnel enabled first, 5 customer name.

i've managed create following script:

#!/bin/bash  [...]  while ifs=':' read -ra array;     if [ "${array[3]}" != "null" ];         echo         echo "### using vpn conf '${array[3]}' server '${array[0]}'... ###"         sleep 1         #vpnc ${array[3]}         tunnel1=$(ifconfig | grep -o eth0)         tunnel2='eth0'          if [ "$tunnel1" = "$tunnel2" ];           echo "### tunnel ###"            scp -p${array[1]} $filename.gz ${array[0]}:${array[2]}           scp -p${array[1]} $filelist ${array[0]}:${array[2]}            ssh -p${array[1]} ${array[0]} 'cd '${array[2]}' && cat '${array[2]}'/'$filelist' | cpio --quiet -h ustar -o -0 -l -f '${array[2]}'/'$backup_filename           ssh -p${array[1]} ${array[0]} 'gzip '${array[2]}'/'$backup_filename            ssh -p${array[1]} ${array[0]} 'tar -c '${array[2]}' -xf '${array[2]}'/'$filename.gz           ssh -p${array[1]} ${array[0]} 'cd '${array[2]}' && mkdir -p '$remote_backup_directory' && rm -f '$filelist' && mv -f '$backup_filename.gz $remote_backup_directory            echo "### server '${array[0]}' updated ###"           #vpnc-disconnect           sleep 1         else           echo "### vpn error in server '${array[4]}' ###"         fi     else         echo          scp -p${array[1]} $filename.gz ${array[0]}:${array[2]}         scp -p${array[1]} $filelist ${array[0]}:${array[2]}          ssh -p${array[1]} ${array[0]} 'cd '${array[2]}' && cat '${array[2]}'/'$filelist' | cpio --quiet -h ustar -o -0 -l -f '${array[2]}'/'$backup_filename-${array[4]}         ssh -p${array[1]} ${array[0]} 'gzip '${array[2]}'/'$backup_filename-${array[4]}          ssh -p${array[1]} ${array[0]} 'tar -c '${array[2]}' -xf '${array[2]}'/'$filename.gz          ssh -p${array[1]} ${array[0]} 'cd '${array[2]}' && mkdir -p '$remote_backup_directory' && rm -f '$filelist' && mv -f '$backup_filename-${array[4]}.gz $remote_backup_directory          echo "### server '${array[4]}' updated ###"         echo     fi; done < "$customer_servers_file" 

$customer_servers_file container file described earlier, $filename.gz update package, $filelist list of files update. other variables set other purposes.

problem is, whole thing running only first customer in $customer_servers_file. lines above first being skipped, , can't tell why. hints? thank you.

edit: answer duplicate objection, being script rather large didn't think of link between while , ssh objects. rather thinking more of syntax error. however, can still delete question if asked so.

this happens because while read loop , ssh both read stdin.

you can fix using /dev/null ssh's stdin:

ssh host cmd  < /dev/null  

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -