250 lines
9.0 KiB
HTML
250 lines
9.0 KiB
HTML
<!--startcut ======================================================= -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html>
|
|
<head>
|
|
<META NAME="generator" CONTENT="lgazmail v1.1I.e">
|
|
<TITLE>The Answer Guy 38: Shell Scripting: Getting Host and User Names</TITLE>
|
|
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
|
|
LINK="#3366FF" VLINK="#A000A0">
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<H4>"The Linux Gazette...<I>making Linux just a little more fun!</I>"</H4>
|
|
<P> <hr> <P>
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<center>
|
|
<H1><A NAME="answer">
|
|
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
|
|
border="0" align="middle">
|
|
<font color="#B03060">The Answer Guy</font>
|
|
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
|
|
border="0" align="middle">
|
|
</A></H1>
|
|
<BR>
|
|
<H4>By James T. Dennis,
|
|
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a><BR>
|
|
Starshine Technical Services,
|
|
<A HREF="http://www.starshine.org/">http://www.starshine.org/</A>
|
|
</H4>
|
|
</center>
|
|
|
|
<p><hr><p>
|
|
<!-- endcut ======================================================= -->
|
|
<!-- begin 6 -->
|
|
<H3 align="left"><img src="../../gx/dennis/qbubble.gif"
|
|
height="50" width="60" alt="(?) " border="0"
|
|
>Shell Scripting: Getting Host and User Names</H3>
|
|
|
|
|
|
<p><strong>From Darby Gilbert on Sat, 06 Feb 1999
|
|
</strong></p>
|
|
<!-- ::
|
|
Shell Scripting: Getting Host and User Names
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
:: -->
|
|
<P><STRONG><IMG SRC="../../gx/dennis/qbub.gif" ALT="(?)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
>
|
|
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.
|
|
</STRONG></P>
|
|
<P><STRONG>
|
|
Darby Gilbert
|
|
</STRONG></P>
|
|
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
|
|
HEIGHT="28" WIDTH="50" BORDER="0"
|
|
>
|
|
Under Linux these (simple interpreted text programs)
|
|
are called shell scripts (they are technically not
|
|
"batch files" though the concept is the same).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
To get the current "computer name" use the '<tt>hostname</tt>'
|
|
command. To assign that to a shell or environment
|
|
variable use a command like:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BlockQuote><Code>
|
|
THISHOST=$(hostname)
|
|
</Code></BlockQuote></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... for the short version (in foo.example.org this
|
|
command returns just "foo"). You can use:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
THISHOST=$(hostname -f)
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
or:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
THISHOST=$(hostname --long)
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... to get the "full" or "long" name (the host.domain
|
|
string).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
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:
|
|
</BLOCKQUOTE>
|
|
|
|
<blockquote><pre> uid=500(jimd) gid=100(users)
|
|
groups=100(users),10(wheel),11(test),17(staff),
|
|
60(web),40(game)
|
|
</pre></blockquote>
|
|
<BLOCKQUOTE>
|
|
(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:
|
|
</BLOCKQUOTE>
|
|
<pre><BLOCKQUOTE><BlockQuote>
|
|
USERNAME=$(id -un)
|
|
UID=$(id -u)
|
|
PRIMARYGROUP=$(id -gn)
|
|
PRIMARYGID=$(id -g)
|
|
GROUPLIST=$(id -Gn)
|
|
GIDLIST=$(id -G)
|
|
</BlockQuote></BLOCKQUOTE></pre>
|
|
<BLOCKQUOTE>
|
|
In other words '<TT>/usr/bin/id</TT>' takes options <tt>-u</tt> (user)
|
|
<tt>-g</tt> (primary group), <tt>-G</tt> (list of groups) and <tt>-n</tt>
|
|
(names, not numeric IDs).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
So you could construct a crude e-mail address for your
|
|
user by using:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>
|
|
MYEMAIL="`id -un`@`hostname -f`"
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... here I've used "backticks" (accent characters) which are
|
|
the more common form of the "command substitution operator."
|
|
Normally I use the <tt>$()</tt> form which is easier to read and
|
|
nestable. I use it here only to demonstrate that they are
|
|
the same (under <tt>bash</tt> and recent Korn shells at any rate).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
Here's a simple shell script that takes your list of
|
|
groups and walks through them one at a time:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BLOCKQUOTE><CODE>#!/bin/bash
|
|
<BR>GLIST=$(<TT>/usr/bin/id</TT> -Gn)
|
|
<BR>set -- $GLIST
|
|
<BR>while [ "$1" ]; do
|
|
<br> echo $1
|
|
<br> shift
|
|
<br>done
|
|
</CODE></BLOCKQUOTE></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
In this case I use a special form of the '<tt>set</tt>' built-in
|
|
command: which resets my list of command line arguments
|
|
to the value specified. I could do that with just:
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE><BlockQuote><code>
|
|
set $GLIST
|
|
</code></BlockQuote></BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
... which sets <tt>$1</tt> to the first string in <tt>$GLIST</tt>
|
|
and <tt>$2</tt> 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 <tt>set</tt>'s <tt>--</tt> ("dash, dash") option which signifies
|
|
the end of all <EM>options</EM> to the '<tt>set</tt>' command forcing it to
|
|
consider the rest of the command line items to be
|
|
"arguments" (rather than options).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
This is probably a bit confusing if you don't know about the
|
|
'<tt>set</tt>' command. Under bash and Korn shell (at least) you can
|
|
use command like <tt>set -o noclobber</tt> (or <tt>set -C</tt>) to prevent the
|
|
overwriting of existing files with shell redirection
|
|
operators and <tt>set -o noglob</tt> (<tt>set -f</tt>) to disable filename
|
|
expansion (the conversion by the shell of <tt>*.txt</tt> into a list
|
|
of files that match that pattern). There are many other
|
|
features supported by the typical Unix shell (Bourne family).
|
|
</BLOCKQUOTE>
|
|
<BLOCKQUOTE>
|
|
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:
|
|
</BLOCKQUOTE>
|
|
|
|
<BLOCKQUOTE><dl>
|
|
<dt>Csh Programming Considered Harmful
|
|
<DD><TT>
|
|
<A HREF="http://www.faqs.org/faqs/unix-faq/shell/csh-whynot../../index.html"
|
|
>http://www.faqs.org/faqs/unix-faq/shell/csh-whynot../../index.html</a></TT>
|
|
</dl></BLOCKQUOTE>
|
|
<!-- sig -->
|
|
|
|
<!-- end 6 -->
|
|
<!--startcut ======================================================= -->
|
|
<P> <hr> <P>
|
|
<H5 align="center"><a href="http://www.linuxgazette.com/copying.html"
|
|
>Copyright ©</a> 1999, James T. Dennis
|
|
<BR>Published in <I>The Linux Gazette</I> Issue 38 March 1999</H5>
|
|
<P> <hr> <P>
|
|
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<TABLE WIDTH="96%"><TR VALIGN="center" ALIGN="center">
|
|
<TD ROWSPAN="3" COLSPAN="4" WIDTH="24%"><A HREF="../lg_answer38.html"
|
|
><IMG SRC="../../gx/dennis/answernew.gif"
|
|
ALT="[ Answer Guy Index ]"></A></td>
|
|
<TD WIDTH="6%"><A HREF="1.html">1</A></TD>
|
|
<TD WIDTH="6%"><A HREF="2.html">2</A></TD>
|
|
<TD WIDTH="6%"><A HREF="3.html">3</A></TD>
|
|
<TD WIDTH="6%"><A HREF="4.html">4</A></TD>
|
|
<TD WIDTH="6%"><A HREF="5.html">5</A></TD>
|
|
<TD WIDTH="6%"><A HREF="6.html">6</A></TD>
|
|
<TD WIDTH="6%"><A HREF="7.html">7</A></TD>
|
|
<TD WIDTH="6%"><A HREF="8.html">8</A></TD>
|
|
<TD WIDTH="6%"><A HREF="9.html">9</A></TD>
|
|
<TD WIDTH="6%"><A HREF="10.html">10</A></TD>
|
|
<TD WIDTH="6%"><A HREF="11.html">11</A></TD>
|
|
</TR><TR VALIGN="center" ALIGN="center">
|
|
<TD><A HREF="12.html">12</A></TD>
|
|
<TD> </TD>
|
|
<TD><A HREF="14.html">14</A></TD>
|
|
<TD> </TD>
|
|
<TD><A HREF="16.html">16</A></TD>
|
|
<TD><A HREF="17.html">17</A></TD>
|
|
<TD><A HREF="18.html">18</A></TD>
|
|
<TD><A HREF="19.html">19</A></TD>
|
|
<TD> </TD>
|
|
<TD><A HREF="21.html">21</A></TD>
|
|
<TD><A HREF="22.html">22</A></TD>
|
|
</TR><TR VALIGN="center" ALIGN="center">
|
|
<TD><A HREF="23.html">23</A></TD>
|
|
<TD><A HREF="24.html">24</A></TD>
|
|
<TD> </TD>
|
|
<TD><A HREF="26.html">26</A></TD>
|
|
<TD> </TD>
|
|
<TD><A HREF="28.html">28</A></TD>
|
|
<TD><A HREF="29.html">29</A></TD>
|
|
<TD><A HREF="30.html">30</A></TD>
|
|
<TD><A HREF="31.html">31</A></TD>
|
|
<TD><A HREF="32.html">32</A></TD>
|
|
<TD> </TD>
|
|
</TR></TABLE>
|
|
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
|
|
<P> <hr> <P>
|
|
<!-- begin lgnav ::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<A HREF="../index.html"
|
|
><IMG SRC="../../gx/indexnew.gif" ALT="[ Table Of Contents ]"></A>
|
|
<A HREF="../../index.html"
|
|
><IMG SRC="../../gx/homenew.gif" ALT="[ Front Page ]"></A>
|
|
<A HREF="../lg_bytes38.html"
|
|
><IMG SRC="../../gx/back2.gif" ALT="[ Previous Section ]"></A>
|
|
<A HREF="../lg_tips38.html"
|
|
><IMG SRC="../../gx/fwd.gif" ALT="[ Next Section ]"></A>
|
|
<!-- end lgnav ::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
|
|
</BODY></HTML>
|
|
<!--endcut ========================================================= -->
|