0

I have below SH script which reads test.csv file but it is not printing command what I am expecting. Please help

read header
while IFS="," read -r user role
do
    echo "btp assign security_role-collection $role --to-user $user"
    
done
} < test.csv

test.csv file records as below

user,role
[email protected],testrole1
[email protected],testrole2

Results what I am getting is:

 --to-user [email protected] testrole1
 --to-user [email protected] testrole2
btp assign security_role-collection  --to-user

but I am expecting below results instead. What am I doing wrong ?

btp assign security_role-collection  testrole1 --to-user [email protected]
btp assign security_role-collection  testrole2 --to-user [email protected]
3
  • 3
    Your input file has dos line endings. Remove them. Commented Oct 7, 2021 at 15:58
  • 1
    You also might have an empty line at the end. Commented Oct 7, 2021 at 16:46
  • Removing line ending has removed last line of output but still it does not explain why $"btp assign security_role_collection and role value is not printed on screen and email is printed first. Commented Oct 7, 2021 at 16:55

1 Answer 1

1

When you read this line [email protected],testrole1 and the line has \r\n line endings, the value of $role is testrole1\r

The string you create looks like

btp assign security_role-collection testrole1\r --to-user [email protected]
.............................................^^

And when you print it, the carriage return moves the cursor to column 1.

You want

{
read header
while IFS="," read -r user role
do
    echo "btp assign security_role-collection ${role%$'\r'} --to-user $user"
    # ........................................^^^^^^^^^^^^^
done
} < test.csv

or

{
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)
# ..^^^^^^^^^^^^^^^^^^^^^^^^^
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. It is working.
Or IFS=$',\r'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.