0

I'm trying to automate git cloning for a class and I'm having trouble with formatting the string in bash.

#!/bin/sh
students=()
while IFS=,  read -r lastname firstname; do
    #students+="$lastname$firstname"
    student=$lastname$firstname
    echo $student
    echo $lastname
    git clone "...$student/unit02.git" $student
done < studentNames.csv | tr -d '" '

The echo will produce the correct format, the names without quotation marks. When cloning, however, both uses of the $student variable returns back to unformatted.

1 Answer 1

1

Perhaps try:

while IFS=,  read -r lastname firstname; do
<logic>
done < <( tr -d '" ' < studentNames.csv )

When you write < studentNames.csv | tr -d '" ' you aren't passing a filtered file into the while loop, you're passing an unfiltered file into the while loop and then filtering all the output. The echo statements print the right values because they're being filtered on the spot, but the values in the variables aren't correct.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thank you this makes a lot of sense. Do you know why this error happens? I've seen this syntax before in all of the websites I've seen but my bash doesn't seem to like it. $ ./gitclone.sh ./gitclone.sh: line 9: syntax error near unexpected token <' ./gitclone.sh: line 9: done < <( tr -d '" ' < studentNames.csv )'
It's a process substitution. Your shebang is #!/bin/sh instead of #!/bin/bash, if you can switch it then it should work. If you have to use /bin/sh then I can edit the answer. See the manual page here: gnu.org/software/bash/manual/html_node/…
great thank you. I fixed that but running into other issues I can probably figure out. I really appreciate it.
@AaronLimbat: side note: /bin/sh is not bash (well, on some systems it is, but on your system it isn't). Also (probably not important here) note that "Git bash" has nothing to do with Git, it's just a port of Bash for Windows systems.

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.