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.
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:
... for the short version (in foo.example.org this command returns just "foo"). You can use:
... 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:
set -- $GLIST
while [ "$1" ]; do
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:
... 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