我想将我的n号csv数据传递给命令" esusers useradd username -r role -p password"。我怎样才能在linux机器上执行此操作。我已经完成了这个窗口,但无法在linux机器上做任何人可以帮助我摆脱这个。我的输入也会包含标题。
答案 0 :(得分:1)
csv文件user.csv :
user1,role1,pass1
user2,role2,pass2
user3,role3,pass3
用于迭代csv文件的bash脚本(scripts.sh):
#!/bin/bash
# Check parameters
if [ "$#" -ne 1 ]; then
>&2 echo "Illegal number of parameters"
exit 1
fi
# Check file
if [ ! -f "${1}" ]; then
>&2 echo "File ${1} not found"
exit 1
fi
FILE="${1}"
while read line; do
USER=`echo ${line} | cut -d"," -f1`
ROLE=`echo ${line} | cut -d"," -f2`
PASS=`echo ${line} | cut -d"," -f3`
echo "adding user ${USER} (role: ${ROLE}) with password: ${PASS}"
esusers useradd "${USER}" -r "${ROLE}" -p "${PASS}"
done < ${FILE}
然后,使用chmod +x script.sh
将执行模式添加到脚本
并使用csv文件作为参数./script.sh user.csv
$ ./script.sh user.csv
adding user user1 (role: role1) with password: pass1
adding user user2 (role: role2) with password: pass2
adding user user3 (role: role3) with password: pass3