Friday, 20 June 2008

Bash: using the read command

While I normally use awk or sed to pull a piece of information I want out of a line of text, it doesn't work as well when you need multiple pieces of information. Well it works fine for displaying multiple pieces of information, but if you want them in variables to use later?

I once wrote a script that used declare to do the equivalent of this:
declare `w | head -1 | 
awk '{ print "DAYS=" $3 " " "USERS=" $6 }'`
# now do something with $DAYS and $USERS

Ewww... Now I know there's a better way: the "read" command. You could do something like this:
w | head -1 |
(read -a FIELDS
#do something with ${FIELDS[2]} and ${FIELDS[5]})

You can also do like so:
ls -l | tail -1 |
(read perms dunno user group \
size month date year file
echo "$user: $file")

You can even loop over all the lines of input. Let's take the /etc/passwd file as an example. Since that file uses a colon as a separator, we first need to set the IFS variable which determines what characters are used as field separators.
cat /etc/passwd | (IFS=:
while read user pass uid gid desc theRest; do
echo "$user: $desc"
done)

As pointed out here, this is also one way to deal with a file containing a list of filenames that may have spaces in them:
cat list | while read f; do
ls -l "$f"
done

No comments: