"The Linux Gazette...making Linux just a little more fun!"


(?) The Answer Guy (!)


By James T. Dennis, [email protected]
Starshine Technical Services, http://www.starshine.org/


(?) Shell Scripting: Getting Host and User Names

From Darby Gilbert on Sat, 06 Feb 1999

(?) I am trying to write a batch file that will pick up the computer name for the naming convention. On NT 4.0 computers, it is no problem. Is there a way to write a batch file that will pick up the computer name and/or user name from the computer so that it will use it to name a file that is produced from the batch file? I have been trying all sorts of different things and also searching the web trying to find answers when I came accross your page. Any help would be greatly appreciated. Thank you.

Darby Gilbert

(!) Under Linux these (simple interpreted text programs) are called shell scripts (they are technically not "batch files" though the concept is the same).
To get the current "computer name" use the 'hostname' command. To assign that to a shell or environment variable use a command like:
THISHOST=$(hostname)
... for the short version (in foo.example.org this command returns just "foo"). You can use:
THISHOST=$(hostname -f)
or:
THISHOST=$(hostname --long)
... to get the "full" or "long" name (the host.domain string).
To get information about the current user (the one running the script) we use the 'id' command. Now, if we just use the command with no options it gives us output like:
	uid=500(jimd) gid=100(users)
	groups=100(users),10(wheel),11(test),17(staff),
	60(web),40(game)
(except that it's all on one line). This is informative for interactive use --- but far too ugly for elegant script parsing. So we use options to get just what we want:
USERNAME=$(id -un) UID=$(id -u) PRIMARYGROUP=$(id -gn) PRIMARYGID=$(id -g) GROUPLIST=$(id -Gn) GIDLIST=$(id -G)
In other words '/usr/bin/id' takes options -u (user) -g (primary group), -G (list of groups) and -n (names, not numeric IDs).
So you could construct a crude e-mail address for your user by using:
MYEMAIL="`id -un`@`hostname -f`"
... here I've used "backticks" (accent characters) which are the more common form of the "command substitution operator." Normally I use the $() form which is easier to read and nestable. I use it here only to demonstrate that they are the same (under bash and recent Korn shells at any rate).
Here's a simple shell script that takes your list of groups and walks through them one at a time:
#!/bin/bash
GLIST=$(/usr/bin/id -Gn)
set -- $GLIST
while [ "$1" ]; do
echo $1
shift
done
In this case I use a special form of the 'set' built-in command: which resets my list of command line arguments to the value specified. I could do that with just:
set $GLIST
... which sets $1 to the first string in $GLIST and $2 to the next one, etc. That would be pretty safe in this case (since I've never seen anyone create a group name starting with a dash). However it is better shell scripting practice to use the set's -- ("dash, dash") option which signifies the end of all options to the 'set' command forcing it to consider the rest of the command line items to be "arguments" (rather than options).
This is probably a bit confusing if you don't know about the 'set' command. Under bash and Korn shell (at least) you can use command like set -o noclobber (or set -C) to prevent the overwriting of existing files with shell redirection operators and set -o noglob (set -f) to disable filename expansion (the conversion by the shell of *.txt into a list of files that match that pattern). There are many other features supported by the typical Unix shell (Bourne family).
This discussion has focused entirely on Bourne shells. I don't use csh/tcsh much and don't recommend it for scripting (in which I'm in good company; see:
Csh Programming Considered Harmful
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot../../index.html


Copyright © 1999, James T. Dennis
Published in The Linux Gazette Issue 38 March 1999


[ Answer Guy Index ] 1 2 3 4 5 6 7 8 9 10 11
12   14   16 17 18 19   21 22
23 24   26   28 29 30 31 32  


[ Table Of Contents ] [ Front Page ] [ Previous Section ] [ Next Section ]