茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1674641 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00058.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]Shell Scripting: Getting Host and User Names

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Shell Scripting: Getting Host and User Names

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 $GLISTand $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

原文出處:The Answer Guy 38: Shell Scripting: Getting Host and User Names
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|