0

I am writing a script to RSH into servers from a text file and create a specific user on each system. I work for a university that is currently testing Amazon EC2 for their courses. Is it possible to take colon or comma separated values in a text file like this :

    server.edu:user123:John:[email protected]

and pass them to a BASH script as $server $username etc...

2 Answers 2

1

Yes you can use it like this:

> s='server.edu:user123:John:[email protected]'
> IFS=: read server username email <<< "$s"

> echo "$server"
server.edu
> echo "$username"
user123
> echo "$email"
John:[email protected]

EDIT:: For reading this data from file line by line

while IFS=: read server username email; do
   echo "$server"
   echo "$username"
   echo "$email"
done < file
Sign up to request clarification or add additional context in comments.

1 Comment

+1 But please add the < file variant also, to illustrate how to read this data from a file as the OP asked for.
0

It is possible to read lines of colon-separated data from file into an array directly and index into it:

while IFS=: read -a userdata; do
    printf "server: %s\n"   "${userdata[0]}"
    printf "username: %s\n" "${userdata[1]}"
    printf "name: %s\n"     "${userdata[2]}"
    printf "email: %s\n"    "${userdata[3]}"
done < data_file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.