普通用户执行shell script,为什么不能获取 $UID--已解决

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
marlgl
帖子: 6
注册时间: 2007-07-09 16:56

普通用户执行shell script,为什么不能获取 $UID--已解决

#1

帖子 marlgl » 2008-02-29 11:08

部分代码:
LOG_DIR=/var/log # Variables are better than hard-coded values.
ROOT_UID=0 # Only users with $UID 0 have rot privileges.
LINES=50 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non-root exit error.

# Run as root, of course.
# Insert code here to print error message and exit if not root.
echo $ROOT_UID
echo $UID

if [ "$UID" -ne "$ROOT_UID" ]
hen
echo "Must be root to run this script."
exit $E_NOTROOT
else
echo "Current UID is: $UID."
exit $E_NOTROOT
fi


执行结果:
mg@mg-laptop:~/Work/Learning/Shell/AdvancedBashScriptingGuide$ sh cleanup
0

Current UID is: .

正常结果应该是1000:
mg@mg-laptop:~/Work/Learning/Shell/AdvancedBashScriptingGuide$ echo $UID
1000

为什么不能获取 $UID ?


mg@mg-laptop:~/Work/Learning/Shell/AdvancedBashScriptingGuide$ cat cleanup
#!/bin/bash
# Proper header for a Bash script.

# Cleanup, version 3

# Warning:
# ---------
# This script uses quite a number of features that will be explained
#+ later on.
# By the time you've finished the first half of the book,
#+ there should be nothing mysterious about it.

LOG_DIR=/var/log # Variables are better than hard-coded values.
ROOT_UID=0 # Only users with $UID 0 have rot privileges.
LINES=50 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Non-root exit error.

# Run as root, of course.
# Insert code here to print error message and exit if not root.
echo $ROOT_UID
echo $UID
# current_uid=[id -- -u]
# echo $current_uid

if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
else
echo "Current UID is: $UID."
exit $E_NOTROOT
fi

if [ -n "$1" ]; then
# Test if command line argument present (non-empty).
lines=$1
else
lines=$LINES # Default, if not specified on command line.
fi


# Stephane Chazelas suggests the following,
#+ as a better way of checking command line arguments,
#+ but this is still a bit advanced for this stage of the tutorial.
#
# E_WRONGARGS=65 # Non-numberical argument (bad arg format)
#
# case "$1" in
# "" ) lines=50;;
# *[!0-9]*) echo "Usage: 'basename $0' file-to-cleanup"; exit $E_WRONGARGS;;
# * ) lines=$1;;
# esac
#
#* Skip ahead to "Loops" chapter to decipher all this.


cd $LOG_DIR

if [ 'pwd' != "$LOG_DIR" ] # or if [ "$PWD" != "$LOG_DIR" ]
# Not in /var/log?
then
echo "Can't change to $LOG_DIR."
exit $E_XCD
fi # Doublecheck if in right directory, before messing with log file.

# far more efficient is:
#
# cd /var/log || {
# echo "Cannot change to necessary directory." >$2
# exit $E_XCD;
# }


tail -$lines messages > mesg.temp # Saves last section of message log file.
mv mesg.temp messages # Becomes new log directory.


#cat /dev/null > messages
#* No longer needed, as the above method is safer.

cat /dev/null > wtmp # ': > wtmp' and '> wtmp' have the same effect.
echo "Logs cleaned up."


exit 0 # The right and proper method of "exiting" from a script.
# A zero return value from the script upon exit indicates success to the shell.
上次由 marlgl 在 2008-02-29 12:50,总共编辑 4 次。
头像
windwiny
帖子: 2254
注册时间: 2007-03-13 17:26

#2

帖子 windwiny » 2008-02-29 11:11

UID 是bash的东西吧

在脚本里用 `id -u`
头像
marlgl
帖子: 6
注册时间: 2007-07-09 16:56

#3

帖子 marlgl » 2008-02-29 11:23

#!/bin/bash
# Proper header for a Bash script.

# Cleanup, version 3

我是在bash里
我copy Advanced Bash-Scripting Guide 中的例子, 为什么不行?
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#4

帖子 BigSnake.NET » 2008-02-29 12:32

试了一下可以的
你的脚本出什么错误信息
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
marlgl
帖子: 6
注册时间: 2007-07-09 16:56

#5

帖子 marlgl » 2008-02-29 12:39

得不到 $UID (1000) 的值


谢谢上面两位的解答

问题找到了: bash cleanup 而不是 sh cleanup

低级错误
上次由 marlgl 在 2008-02-29 12:49,总共编辑 1 次。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#6

帖子 BigSnake.NET » 2008-02-29 12:42

marlgl 写了:得不到 $UID (1000) 的值
整个输出贴出来..
你怎么知道得不到呢?
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
marlgl
帖子: 6
注册时间: 2007-07-09 16:56

#7

帖子 marlgl » 2008-02-29 12:51

谢谢

问题找到了: bash cleanup 而不是 sh cleanup

低级错误
langzi289
帖子: 3
注册时间: 2008-04-10 14:21

#8

帖子 langzi289 » 2008-04-10 14:24

#!/bin/bash


LOG_DIR=/var/log
ROOT_UID=0
LINES=50
E_XCD=66
E_NOTROOT=67


echo $ROOT_UID
echo $UID

if ["$UID" -ne "$ROOT_UID"]
then
echo "Must be root to run this script."
exit $E_NOTROOT

else

echo "Current UID is :$UID."
exit $E_NOTROOT


我是这样写的,但执行结果为:0
0
root: line 14: [0: command not found
Current UID is :0.

14行为:if ["$UID" -ne "$ROOT_UID"]
我用的是as4请大家帮助,我是初学者。
并且问下LINES=50
E_XCD=66
E_NOTROOT=67这里定义的数字是什么意思,是shell自带的吗?
谢谢大侠帮组下。
langzi289
帖子: 3
注册时间: 2008-04-10 14:21

接上

#9

帖子 langzi289 » 2008-04-10 14:34

我在我的as4系统建立个系统帐号,并给此脚本该帐号有执行权限,但输出的结果是:
0
12353
./root: line 14: [12353: command not found
Current UID is :12353.

应该至少的输出Must be root to run this script才对呀,希望得到帮助、
langzi289
帖子: 3
注册时间: 2008-04-10 14:21

#10

帖子 langzi289 » 2008-07-23 16:21

LOG_DIR=/var/log
ROOT_UID=0
LINES=50
E_XCD=66
E_NOTROOT=67

echo $ROOT_UID
echo $UID

if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
else
echo "Current UID is :$UID."
exit $E_NOTROOT
fi


我操现在可以了。
回复