免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3540 | 回复: 2
打印 上一主题 下一主题

[文本处理] 为啥报 readonly variable [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-06 14:16 |只看该作者 |倒序浏览
写了一个脚本,用于将一台服务器上的USER信息复制到另一台上的。(一个suse,一个rhel)

while read USER UID GID DIR SH PASSWD
do
useradd -m -d $DIR -u $UID -g $GID -s $SH $USER
echo -e "$PASSWD\n$PASSWD\n" |passwd $USER
done < ftp_userpassword.txt

more [root@lxbftp01 ~]# more ftp_userpassword.txt
user1 1001    100     /ftp/user1   /bin/bash       passwd1
user2   1002    100     /ftp/user2       /bin/bash       passwd2
user3   1003    100     /ftp/user3      /bin/bash       passwd3


结果却是一直报错
./add_ftpuser.sh
./add_ftpuser.sh: line 5: UID: readonly variable
useradd: UID 0 is not unique
passwd: Unknown user name 'user1'.
./add_ftpuser.sh: line 5: UID: readonly variable
useradd: UID 0 is not unique
passwd: Unknown user name 'user2'.
./add_ftpuser.sh: line 5: UID: readonly variable
useradd: UID 0 is not unique
passwd: Unknown user name 'user3'.

奇怪的是,UID和GID都是数字,但是一个报错,一个正常,用bash -x 方式执行,可以看到gid都被正常赋值
+ read USER UID GID DIR SH PASSWD
add_ftpuser.sh: line 5: UID: readonly variable
+ useradd -m -d /ftp/user1 -u 0 -g 100 -s /bin/bash user1
useradd: UID 0 is not unique

请高手指点

论坛徽章:
4
技术图书徽章
日期:2013-09-23 10:22:37狮子座
日期:2013-10-15 23:31:54卯兔
日期:2013-11-11 17:33:15金牛座
日期:2013-11-15 17:25:28
2 [报告]
发表于 2013-05-06 14:22 |只看该作者
本帖最后由 nbrr 于 2013-05-06 14:27 编辑

看abs, bash internal variables
http://tldp.org/LDP/abs/html/internalvariables.html
This is the current user's real id, even if she has temporarily assumed another identity through su. $UID is a readonly variable, not subject to change from the command line or within a script,

你可以换个名字
while read ftpUSER ftpUID ftpGID ftpDIR ftpSH ftpPASSWD

论坛徽章:
0
3 [报告]
发表于 2013-05-06 14:38 |只看该作者
nbrr 发表于 2013-05-06 14:22
看abs, bash internal variables
http://tldp.org/LDP/abs/html/internalvariables.html
This is the cur ...


原来是和原有的变量重名了,多谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP