我有以下SH脚本读取test.csv文件,但它不是打印命令,我所期待的。请帮帮忙
read header
while IFS="," read -r user role
do
echo "btp assign security_role-collection $role --to-user $user"
done
} < test.csvtest.csv文件记录如下
user,role
test1@yahoo.com,testrole1
test2@yahoo.com,testrole2我得到的结果是:
--to-user test1@yahoo.comollection testrole1
--to-user test2@yahoo.comollection testrole2
btp assign security_role-collection --to-user但我期望的是下面的结果。我做错了什么?
btp assign security_role-collection testrole1 --to-user test1@yahoo.com
btp assign security_role-collection testrole2 --to-user test2@yahoo.com发布于 2021-10-07 17:35:50
当您读取此行test1@yahoo.com,testrole1并且该行的行尾为\r\n时,$role的值为testrole1\r
您创建的字符串如下所示
btp assign security_role-collection testrole1\r --to-user test1@yahoo.com
.............................................^^当您打印它时,回车键将光标移动到第一列。
你想要的
{
read header
while IFS="," read -r user role
do
echo "btp assign security_role-collection ${role%$'\r'} --to-user $user"
# ........................................^^^^^^^^^^^^^
done
} < test.csv或
{
read header
while IFS="," read -r user role
do
echo "btp assign security_role-collection $role --to-user $user"
done
} < <(sed 's/\r$//' test.csv)
# ..^^^^^^^^^^^^^^^^^^^^^^^^^https://stackoverflow.com/questions/69483765
复制相似问题