Properly restore IFS
Alkis Georgopoulos <[email protected]> Sun, 24 Jan 2010 20:11:55 +0200
| Newsgroups | gmane.linux.terminal-server.devel |
|---|---|
| Message-ID | <1264356715.1699.69.camel@alkis> |
Following some discussion with vagrantc and stgraber on IRC, I'm going
to commit a non-obvious change to IFS saving/restoring, so I'm sending a
script to clarify it and for future reference.
#!/bin/sh
# This script demonstrates a better way to save/restore $IFS.
# An unset IFS is not the same as a null IFS, for example:
# $ IFS='' && var=' ' && echo x${var}x
# x x
# $ unset IFS && var=' ' && echo x${var}x
# x x
# So I propose to use this line to save it:
# oldifs="${IFS-not set}"
# and this line to restore it:
# [ "$oldifs" = "not set" ] && unset IFS || IFS="$oldifs"
do_something_with_IFS()
{
# Save $IFS, or "not set" if it's unset.
# Note the '-' below, it isn't the regular ':-' syntax, it means
# "use alternate value ONLY if IFS is unset, not if it's null".
oldifs="${IFS-not set}"
# Modify IFS and do something with it
IFS=':'
echo "...changing, using, and restoring IFS..."
# Now restore IFS
# This is an "if ... then ... else" construct but in one line.
[ "$oldifs" = "not set" ] && unset IFS || IFS="$oldifs"
}
report_IFS()
{
if [ "${IFS-not set}" = "not set" ]; then
echo "IFS is unset"
elif [ -z "$IFS" ]; then
echo "IFS is null"
else
echo -n "IFS="
echo -n "$IFS" | hexdump -e '8/1 "%02X ""\t"" "' -e '8/1 "%c""\n"'
fi
}
# Testing with various IFS values.
# First, try with the system default:
report_IFS; do_something_with_IFS; report_IFS; echo
unset IFS
report_IFS; do_something_with_IFS; report_IFS; echo
IFS=""
report_IFS; do_something_with_IFS; report_IFS; echo
IFS=" \t\n"
report_IFS; do_something_with_IFS; report_IFS; echo
# And, on this crazy case, the new code has problems.
# But setting IFS to "not set" is a bug on its own,
# as it contains the letter "t" twice. :-)
IFS="not set"
report_IFS; do_something_with_IFS; report_IFS; echo
------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_____________________________________________________________________
Ltsp-developer mailing list. To un-subscribe, or change prefs, goto:
https://lists.sourceforge.net/lists/listinfo/ltsp-developer
For additional LTSP help, try #ltsp channel on irc.freenode.net