Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: Too many arguments

Tags:

bash

arguments

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
like image 741
LatinUnit Avatar asked Dec 27 '22 10:12

LatinUnit


1 Answers

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 shell whenever 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.)

like image 51
Tanktalus Avatar answered Jan 05 '23 17:01

Tanktalus