分页: 1 / 2
Shell脚本面试题……
发表于 : 2010-07-16 17:23
由 byZh
一个文本文件内容如下:
代码: 全选
user1 abcd
user2 g23d
user3 vgq2
…… ……
根据文件内容批量创建用户,第一列为用户名,第二列为对应用户的密码
Re: Shell脚本面试题……
发表于 : 2010-07-16 18:59
由 adam8157
这个...一行一行读, 交给swk分出名字和密码 再交给useradd....就ok了吧....
Re: Shell脚本面试题……
发表于 : 2010-07-16 20:54
由 byZh
adaml 写了:这个...一行一行读, 交给swk分出名字和密码 再交给useradd....就ok了吧....
写出来在自己的机器上实现一下
Re: Shell脚本面试题……
发表于 : 2010-07-17 7:29
由 ChenFengyuan
perl -ne '($user,$pass)=split;system("useradd $user -p $pass")'

Re: Shell脚本面试题……
发表于 : 2010-07-18 9:46
由 tusooa
楼上是perl脚本了。
代码: 全选
#!/bin/bash
while read line; do
username="${line% *}"
password="${line#$username }"
useradd -p "$password" "$username"
done < file
总共一个外部命令,而且是必须要用的。
Re: Shell脚本面试题……
发表于 : 2010-07-18 18:47
由 byZh
tusooa 写了:楼上是perl脚本了。
代码: 全选
#!/bin/bash
while read line; do
username="${line% *}"
password="${line#$username }"
useradd -p "$password" "$username"
done < file
总共一个外部命令,而且是必须要用的。
我运行了一下,不行!
代码: 全选
useradd:无效的用户名“use1 123”
useradd:无效的用户名“use2 234”
Re: Shell脚本面试题……
发表于 : 2010-07-18 21:05
由 tusooa
1楼给的文件里的username和password的分隔符是两个空格,而你输入只输入了1个!当然要出错!
Re: Shell脚本面试题……
发表于 : 2010-07-20 12:03
由 kokerjie
#!/bin/bash
while read line; do
username=`echo $line | cut -d ' ' -f1`
password=`echo $line | cut -d ' ' -f2`
useradd -p "$password" "$username"
done < file
这样写,就可以不管多少空格都可以
Re: Shell脚本面试题……
发表于 : 2010-07-20 12:05
由 kokerjie
代码: 全选
#!/bin/bash
while read line; do
username=`echo $line | cut -d ' ' -f1`
password=`echo $line | cut -d ' ' -f2`
useradd -p "$password" "$username"
done < file
这样写,就可以不管多少空格都可以
Re: Shell脚本面试题……
发表于 : 2010-07-22 21:24
由 byZh
偶知道这个不算什么难题,但是……
还有很多东西需要我去学习,多谢楼上那么多位!
Re: Shell脚本面试题……
发表于 : 2010-07-23 16:07
由 yangsp805
Re: Shell脚本面试题……
发表于 : 2010-07-23 16:10
由 yangsp805
代码: 全选
#!/bin/bash
while read line; do
username=`echo $line | cut -d ' ' -f1`
password=`echo $line | cut -d ' ' -f2`
useradd -p "$password" "$username"
done < file
Re: Shell脚本面试题……
发表于 : 2010-07-23 23:49
由 TheRedIsEast
这里给出的passwd是加密后的吗?
Re: Shell脚本面试题……
发表于 : 2010-07-26 15:10
由 zhouxianglh

要好好学啊
Re: Shell脚本面试题……
发表于 : 2010-07-27 19:45
由 ly50247
awk '{system("useradd " $1 " -p " $2) }' file.txt
这样可以吗