LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2015, 06:53 PM   #1
bong_water
LQ Newbie
 
Registered: Jun 2013
Posts: 2

Rep: Reputation: Disabled
Linux Bash while loop reading multiple field text input and running remote commands


I need to create a lot of file systems, of different sizes, owners, groups, and mount points, on a lot of different servers on a regular basis. The file system info was provided to me in a spreadsheet with the following info:

Code:
servername mountpoint owner group size
My script works when I echo the information to the screen. It will go through all 20 or so lines to the end of the file. When it ssh's to the server to run the commands it only runs the first line of the input file and the script exits. What am I missing to make the script go through all of the text file?


Code:
#!/bin/bash
input=fs.txt
 
## Read in Data loop
 
while read -r line
do
  IFS=$'\t' "server" "mount" "owner" "group" "size"<<<"$line"
 
echo "Host : $server" "Mount: $mount" "Owner: $owner" "Group: $group" "Size: $size";
 
 
## Login to server
ssh -xq user@${server} "mkdir -p '$mount'";
ssh -xq user@${server} "chown '$owner':'$group' '$mount'";
 
done < "$input"
Thanks

Last edited by bong_water; 02-11-2015 at 07:10 PM.
 
Old 02-11-2015, 07:48 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,152

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
one of the commands inside the loop is swallowing data from stdin , save stdin just after the do command and restore it just before the done command, sorry not at my machine at moment so can;t give you a code example but have a look at the advanced bash scripting guide
 
Old 02-11-2015, 10:28 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
As with most things, you have several options:

1. Investigate the '-n' option for ssh

2. Use exec to open a file descriptor for your file and the pass read the '-u' option and your fd number

i would add that instead of setting IFS inside the loop and separating your line, you can simply do the same at the read line and use your separate naming convention:
Code:
while IFS=$'\t' read -r server mount owner group size
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash and running remote commands ulto Programming 7 02-05-2015 09:56 PM
[SOLVED] Trouble reading input inside bash while loop Ineed$ Programming 5 01-21-2012 06:27 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
flash animation text input field shows nothing kyuso Linux - Software 1 09-19-2006 10:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:00 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