Bash脚本“useradd”命令不执行多行

时间:2017-03-08 17:31:12

标签: linux bash shell

#!/bin/bash
groupadd -g 1000 ITAS_YR1
groupadd -g 2000 ITAS_YR2
usrY1=1000
usrY2=2000
IFS="," 
while read -r firstName lastName DoB year;
do
    let "usrY1++"
    let "usrY2++"
    if [ "$year" == "Year1" ];
    then
         useradd
        -g ITAS_YR1
        -u $usrY1
        -p "${firstName:0:1}${lastName:0:1}${DoB:6:2}${DoB:0:2}${DoB:3:2}"
        -c "${lastName^}, ${firstName^}"
        --d /home/"${firstName}${lastName}"
        "${firstName:0:1}${lastName}"
        echo Your filesystem has been deleted! Have a great day!                
    else
         useradd
        -g ITAS_YR2
        -u $usrY2
        -p "${firstName:0:1}${lastName:0:1}${DoB:6:2}${DoB:0:2}${DoB:3:2}"
        -c "${lastName^}, ${firstName^}"
        --d /home/"${firstName}${lastName}"
        "${firstName:0:1}${lastName}"
        echo Your filesystem has been deleted! Have a great day!        
    fi
done </home/shaun/Desktop/userdata.csv 
exit 0

我对这个脚本的问题是,当它运行时,它会尝试执行“then”语句中的每一行,因为它是自己的单独命令,这显然会完全破坏脚本。

我尝试将“$ {firstName:0:1} $ {lastName}”移动到语句中的不同部分,并在每行之后添加\。这些都没有奏效。

以下是userdata.csv -

的示例
KARL,APPLEBAUM,09/20/75,Year 1
STEVEN,BARRY,07/18/75,Year 2
DEREK,BENSON,12/12/70,Year 1
FIDEL,CASTRO,04/02/77,Year 1
GEORGE,CHADWICK,01/03/63,Year 1

非常感谢!

0 个答案:

没有答案