Bash 脚本说找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12716633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script says command not found
提问by MarioDS
I try to run the following bash script to create a bunch of users, groups, home dirs for the users and correct permissions for all of these. The OS is CentOS.
我尝试运行以下 bash 脚本来为用户创建一堆用户、组、主目录以及所有这些的正确权限。操作系统是 CentOS。
When I try to run the following, which I though should work, it returns "command not found" when running via terminal. it only gets as far as creating the /homedirs directory, nothing more. I'm a total noob at bash scripting so forgive me if this looks ugly.
当我尝试运行以下命令时,虽然我应该可以工作,但在通过终端运行时它会返回“未找到命令”。它只会创建 /homedirs 目录,仅此而已。我对 bash 脚本完全是个菜鸟,所以如果这看起来很丑,请原谅我。
mkdir /homedirs; chmod 775 /homedirs;
for iYear in {1..3} do
sYear = $iYear"ti"
sYearDir = "/homerirs/"$sYear
groupadd $sYear; mkdir $sYearDir; chgrp $sYear $sYearDir; chmod 750 $sYearDir
for sClass in {a,b} do
sClassDir = $sYearDir/$sClass
mkdir $sClassDir
sClassGrp = $sYear$sClass
groupadd $sClassGrp; chgrp $sClassGrp $sClassDir; chmod 750 $sClassDir
for iUser in {1..3} do
sUserName = "i"$iYear$sClass"g"$iUser
sUserDir = $sClassDir/$sUserName
useradd -d $sUserDir -g $sClassGrp -G $sYear -m $sUserName
chown $sUserName $sUserDir; chmod 750 $sUserDir
done
done
done
回答by Basile Starynkevitch
You may need to set your PATHand you really should read the advanced bash scripting guide. See also this answer.
你可能需要设置你的PATH,你真的应该阅读高级 bash 脚本指南。另请参阅此答案。
I also suggest to debug your script by starting it with #!/bin/bash -vxas its first line. And you should make it executable with chmod u+xat least.
我还建议通过将脚本#!/bin/bash -vx作为第一行开始来调试脚本。你chmod u+x至少应该让它可执行。
Perhaps groupaddmight not be available on your system.
也许groupadd在您的系统上可能不可用。
回答by tripleee
The error message is caused by the spaces around the equals signs. A token with whitespace after it is interpreted as a command name; so what you intended as variable names causes the Command not founderrors.
错误消息是由等号周围的空格引起的。后有空格的标记被解释为命令名称;所以你想要的变量名会导致Command not found错误。
回答by DannyK
best thing to do is add the full path before your executables:
最好的办法是在可执行文件之前添加完整路径:
change useradd to /usr/sbin/useradd
将 useradd 更改为 /usr/sbin/useradd
change groupadd to /usr/sbin/groupadd
将 groupadd 更改为 /usr/sbin/groupadd
will cure the command not found.
将修复未找到的命令。
remember this programs will probably need to run as root to work.
请记住,此程序可能需要以 root 身份运行才能工作。

