繁体   English   中英

使用while循环语法的Linux shell脚本错误

[英]Linux shell scripting error with while loop syntax

#!/bin/bash
initial="/"
usrname=`cut -d ":" -f1 users.txt`
password=`cut -d '"' -f3 users.txt|cut -d ":" -f2`
comment=`cut -d '"' -f2,3 users.txt|cut -d ":" -f1`
path=`cut -d "/" -f2,3 users.txt`
totalpath="$initial$path"
while read line
  do
  useradd -p "$password" -c "$comment" -m -d "$totalpath" "$usrname"
done < "users.txt"

我正在尝试使用文本文件添加多个用户。 我收到“意外令牌'完成'附近的语法错误”。 我存储的所有变量都完美无缺。 我用echo命令测试了它。 但是,当我运行循环时,弹出错误消息并且不会创建用户。

供您参考,这是我的文本文件的样子:

acdeng:"ADLER CHARLES DAVID",00-9388,x0753,Engineering:acc944:/home/Engineering
ardsal:"ANSELL ROBERT D",14-2675,x1624,Sales:acc944:/home/Sales

如上所述,脚本似乎为我运行没有任何语法错误。

但是,我会注意到你似乎没有解析文件的每一行。 usrname等人的初始分配是提取多行切片,然后在循环的每次迭代中按原样使用。 你永远不会在循环中引用$line ,所以在每次迭代中你都会得到与useradd相同的调用。

我建议在useradd前添加echo ,这样你就可以看到你实际发出的命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM