简体   繁体   English

另一个文件 bash 脚本中的“adduser”有什么问题

[英]What's the issue in my “adduser” from another file bash script

I am asked to add some users with all their info (password, id,group id...) present in a given file.我被要求添加一些用户,他们的所有信息(密码、ID、组 ID ...)都存在于给定文件中。 Whenever I run my bash "gedit" script, it gives me a message that the "useradd command not found".每当我运行我的 bash“gedit”脚本时,它都会给我一条消息“找不到 useradd 命令”。 If anyone could help me please, this is my code:如果有人可以帮助我,这是我的代码:

 #!/bin/bash
choice=0
while [ $choice != "1" -a $choice != "2" -a $choice != "3" ]
do 
  echo "What do you want to do"
  echo "1-Add Users"
  echo "2-Remove users"
  echo "3-Update users"
  echo -n "Enter choice : "
  read choice
done

x=0
while [ $x -ne 1 ]
do
  echo -n "Donner le fichier :"
  read fichier
  if test -f $fichier              # check if file ficher exists
  then
    x=1
  else
  echo "File not found"  
  x=0
  fi
done

if [ $choice == "1" ]
  then

 FILE="user"
   USERNAME=$(cut -d ":" -f 1 $FILE)
   PASSWORD=$(cut -d ":" -f 2 $FILE)
   USER_ID=$(cut -d ":" -f 3 $FILE)
   GROUP_ID=$(cut -d ":" -f 4 $FILE)
   USER_INFO=$(cut -d ":" -f 5 $FILE)
   HOME_DIRECTORY=$(cut -d ":" -f 6 $FILE)
   SHELL=$(cut -d ":" -f 7 $FILE)
   MIN=$(cut -d ":" -f 8 $FILE)
   MAX=$(cut -d ":" -f 9 $FILE)
   INACTIVE=$(cut -d ":" -f 10 $FILE)
   WARNING=$(cut -d ":" -f 11 $FILE)
   
   useradd -m -c "${USERNAME}" "${PASSWORD}" "${USER_ID}" "${GROUP_ID}" "${USER_INFO}" "${HOME_DIRECTORY}" "${SHELL}" "${MIN}" "${MAX}" "${INACTIVE}" "${WARNING}"
fi

To call the script i am using chmod u+x project (which is the name of the gedit file)要调用脚本,我正在使用 chmod u+x 项目(这是 gedit 文件的名称)

The displayed error message is:./project: line 43: useradd: command not found显示的错误信息是:./project: line 43: useradd: command not found

This is the content of the input file "user":这是输入文件“user”的内容:

charbel:password:1001:1001:Charbel Haddad:/home/charbel:/bin/bash:0:30:15:7:y
assil:p@ssw0rd:1002:1002:Assil:/home/assel:/bin/bash:0:30:10:5:n
marwan:p@ssw0rd:1003:1003:Marwan Ghantous:/home/marwan:/bin/bash:0:50:30:7:n
michel:password:1004:1004:Michel:/home/michel:/bin/bash:1:30:10:5:y

Your script would attempt to extract all the user names into one variable, all the home directories into one variable, etc. You want to loop over the input file and handle one user name and all the other fields from that line at a time.您的脚本将尝试将所有用户名提取到一个变量中,将所有主目录提取到一个变量中,等等。您希望循环输入文件并一次处理一个用户名和该行中的所有其他字段。

...
if [ $choice = "1" ]   # notice also single = not ==
then
  while IFS=":" read -r username password user_id group_id \
              user_info home_dir shell min max inactive warning
  do
      useradd -m -c "$username" "$password" "$user_id" "$group_id" \
                    "$user_info" "$home_dir" "$shell" \
                    "$min" "$max" "$inactive" "$warning"
  done <"$fichier"
fi

Notice also how read -r can split a line into fields for you, using the value of IFS as the field separator, and how we avoid using upper case for our private variables.还要注意read -r如何为您将一行拆分为字段,使用IFS的值作为字段分隔符,以及我们如何避免对私有变量使用大写字母。 (I'm speculating that you forgot to tell us that you managed to overwrite PATH too.) (我猜你忘了告诉我们你也设法覆盖了PATH 。)

As an aside, the while loop earlier in the script could also be simplified radically:顺便说一句,脚本中前面的while循环也可以从根本上简化:

while true
do
  read -p "Donner le fichier :" -r fichier
  test -f "$fichier" && break
  echo "$0: $fichier: file not found" >&2
done

Notice how we print the error message to standard error, and include the name of the script and the name of the file we could not find, and also how read allows for the printing of a prompt with -p , and how we basically always quote variables with file names .请注意我们如何将错误消息打印到标准错误,并包括脚本名称和我们找不到的文件的名称,以及read如何允许使用-p打印提示,以及我们基本上总是引用带有文件名的变量 As a rule, always use read -r unless you specifically want the shell to do funny things with backslashes (this is a legacy behavior for compatibility with the original Bourne shell).通常,除非您特别希望 shell 使用反斜杠做有趣的事情,否则始终使用read -r (这是与原始 Bourne shell 兼容的遗留行为)。

In some more detail, when you read the whole file in one go, you would run a command line更详细地说,当您在 go 中读取整个文件时,您将运行命令行

useradd -m -c "charbel
assil
marwan
michel" "password 
p@ssw0rd
p@ssw0rd
password"

... etc instead of running ...等而不是运行

useradd -m -c "charbel" "password" "1001" "1001" "Charbel Haddad" "/home/charbel" "/bin/bash" "0" "30" "15" "7" "y"

separately and then分别然后

useradd -m -c "assil" "p@ssw0rd" "1002" "1002" "Assil" "/home/assel" "/bin/bash" "0" "30" "10" "5" "n"

etc with one line of input from the file at a time, which is what the while IFS=":" read -r loop above does.等等,一次从文件中输入一行,这就是上面的while IFS=":" read -r循环所做的。

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

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