Os usuários criam script a partir do arquivo CSV

3

Estou tentando fazer esse script funcionar, mas o que recebo é:

14: Syntax error: "(" unexpected (expecting "fi").

Espero que vocês possam resolver o meu problema porque já faz um tempo que estou investigando o erro ... Se você precisar do arquivo .csv , me avise.

Aqui está o script:

#!/bin/bash
filein="proyecto3.csv"
IFS=$'\n'
if [ ! -f "$filein" ]
then
echo "Cannot find file $filein"
else
  groups=('cut -d: -f 6 "$filein" | sed 's/ //'')
  fullnames=('cut -d: -f 1 "$filein"')
  userid=('cut -d: -f 2 "$filein"')
  usernames=('cut -d: -f 1 "$filein" | tr [A-Z] [a-z] | awk '{print substr($1,1,1) $2}'')
fi
for group in ${groups[*]}
do
grep -q "^$group" /etc/group ; let x=$?
if [ $x -eq 1 ]
then
groupadd "$group"
fi
done
  x=0
  created=0
for user in ${usernames[*]}
do
useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null
if [ $? -eq 0 ]
then
let created=$created+1
fi
echo "${usernames[$x]}" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."
fi
    
por Elio Basciani 24.05.2018 / 03:13

1 resposta

5

Simplesmente remova a última linha do seu script (também ajuda a fazer um recuo adequado para ver seus erros). Além disso, você esqueceu de terminar seu último loop com um feito:

#!/bin/bash
filein="proyecto3.csv"
IFS=$'\n'

if [ ! -f "$filein" ]
then
    echo "Cannot find file $filein"
else
    groups=('cut -d: -f 6 "$filein" | sed 's/ //'')
    fullnames=('cut -d: -f 1 "$filein"')
    userid=('cut -d: -f 2 "$filein"')
    usernames=('cut -d: -f 1 "$filein" | tr [A-Z] [a-z] | awk '{print substr($1,1,1) $2}'')
fi

for group in ${groups[*]}
do
    grep -q "^$group" /etc/group ; let x=$?
    if [ $x -eq 1 ]
    then
        groupadd "$group"
    fi
done

x=0 #not sure why you reset x here to zero !?
created=0

for user in ${usernames[*]}
do
    useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null
    if [ $? -eq 0 ]
    then
        let created=$created+1
    fi
done

echo "${usernames[$x]}" | passwd --stdin "$user" > /dev/null
echo "Complete. $created accounts have been created."

Até que eu possa realmente aconselhá-lo a usar algo como shellcheck em seu script (você pode obtê-lo nos repositórios normais do universo do Ubuntu).

sudo apt update
sudo apt install shellcheck

O resultado é muito mais que você pode fazer melhor no seu script:

$ shellcheck test.sh

In test.sh line 9:
    groups=('cut -d: -f 6 "$filein" | sed 's/ //'')
            ^-- SC2006: Use $(..) instead of legacy '..'.


In test.sh line 10:
    fullnames=('cut -d: -f 1 "$filein"')
               ^-- SC2006: Use $(..) instead of legacy '..'.


In test.sh line 11:
    userid=('cut -d: -f 2 "$filein"')
    ^-- SC2034: userid appears unused. Verify it or export it.
            ^-- SC2006: Use $(..) instead of legacy '..'.


In test.sh line 12:
    usernames=('cut -d: -f 1 "$filein" | tr [A-Z] [a-z] | awk '{print substr($1,1,1) $2}'')
               ^-- SC2006: Use $(..) instead of legacy '..'.
                                            ^-- SC2060: Quote parameters to tr to prevent glob expansion.
                                                  ^-- SC2060: Quote parameters to tr to prevent glob expansion.


In test.sh line 29:
    useradd -n -c ${fullnames[$x]} -g "${groups[$x]}" $user 2> /dev/null
                  ^-- SC2086: Double quote to prevent globbing and word splitting.
                                                      ^-- SC2086: Double quote to prevent globbing and word splitting.


In test.sh line 30:
    if [ $? -eq 0 ]
         ^-- SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

Como alternativa, use a ferramenta verificação de linha on-line ...

    
por Videonauth 24.05.2018 / 06:29