Bash:参数太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8332742/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:07:22  来源:igfitidea点击:

Bash: Too many arguments

basharguments

提问by LatinUnit

I've coded the following script to add users from a text file. It works, but I'm getting an error that says "too many arguments"; what is the problem?

我编写了以下脚本以从文本文件添加用户。它有效,但我收到一个错误,提示“参数太多”;问题是什么?

#!/bin/bash

file=users.csv

while IFS="," read USRNM DOB SCH PRG PST ENROLSTAT ; do

if [ $ENROLSTAT == Complete ] ;
then
useradd $USRNM -p $DOB

else

echo "User $USRNM is not fully enrolled"
fi

done < $file

#cat users.csv | head -n 2 | tail -n 1

回答by Tanktalus

Use quotes. Liberally.

使用引号。宽松。

if [ "$ENROLSTAT" = Complete ]

(It's a single equal sign, too.) My greatest problem in shell programming is always hidden spaces. It's one of the reasons I write so much in Perl, and why, in Perl, I tell everyone on my team to avoid the shellwhenever running external programs. There is just so much power in the shell, with so many little things that can trip you up, that I avoid it where possible. (And not where not possible.)

(它也是一个等号。)我在 shell 编程中最大的问题总是隐藏空间。这是我用 Perl 写这么多的原因之一,也是为什么在 Perl 中,我告诉团队中的每个人在运行外部程序时都避免使用 shell。外壳中的力量如此之大,有如此多的小事情会让你绊倒,所以我尽可能避免使用它。(而不是在不可能的地方。)