一个选项在 bash 中没有看到参数(即变量),请帮忙?

mol*_*ath 1 command-line bash scripts

#!/bin/bash

counter=2

while [ $counter -lt 19 ]
do



        username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
        psswd= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f2 
        full_name= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f3 
        group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f4 
        second_group= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f5 

        sudo useradd $username -m -g $group -s /bin/bash -c $full_name

        if [ second_group = LPGestionnaires ]
        then 
                usermod -a -G $second_group $user
        fi

        #echo "$username:$psswd" | chpasswd

        ((counter++))
done
echo Execution complete
Run Code Online (Sandbox Code Playgroud)

它所说sudo useradd $username -m -g $group -s /bin/bash -c $full_name的部分是不起作用的部分,我的 -g 选项没有将变量 $group 参数视为参数,当我执行我的脚本时,它返回:useradd: group '-s' does not exist

我正在从正确定位的 .csv 文件中提取数据。

如果有人可以提供帮助,那就太好了!

谢谢!

jan*_*nos 5

您似乎想将head ...命令的结果分配给username此处的变量:

username= head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1 
Run Code Online (Sandbox Code Playgroud)

那是不正确的语法。像这样更正:

username=$(head -n $counter ./user_sheet.csv | tail -n 1 | cut -d ';' -f1)
Run Code Online (Sandbox Code Playgroud)

然后对其他变量也做同样的事情,它们都有同样的问题。

另外,sudo useradd像这样更改命令:

sudo useradd "$username" -m -g "$group" -s /bin/bash -c "$full_name"
Run Code Online (Sandbox Code Playgroud)

命令行参数中使用的变量通常应该用双引号引起来,以避免分词。