0

Ubuntun 16.04
Bash 4.4.0

I have lines and lines in a csv file that I can use as variables. Example:

"2013","White Woman","Green Orcas Rolling","Felix"
"2014","White Woman","Green Orcas Rolling","Felix"
"2012","White Woman","Green Orcas Rolling","Felix"
"2011","White Woman","Green Orcas Rolling","Felix"

I want to pass these lines into a bash script that will generate a complete url.

Example: bash scriptname.sh "2012","White Woman","Green Orcas Rolling","Felix"

I can replace spaces that are within the variables from inside the script. How can I strip the commas that separate the variables being passed to the script, inside the script, before they interact and set themselves.

Here's an example of my script:

#!/bin/bash

year="${1}"
oinker="${2// /%20}"
poinker="${3// /%20}"
loinker="${4// /%20}"

echo "https://mickeyorileysooo.com/sysbuilder.php?bg=2&year=${year}&oinker=${oinker}&poinker=${poinker}&loinker=${loinker}"

Here is the result:

root@0000 ~ # bash scriptname.sh "2012" "White Woman" "Green Orcas Rolling" "Felix"
https://mickeyorileysooo.com/sysbuilder.php?bg=2&year=2012&oinker=White%20Woman&poinker=Green%20Orcas%20Rolling&loinker=Felix

Here is an updated script with guidance from Barmar:

#!/bin/bash

IFS=,
set -- $*

year="${1}"
oinker="${2// /%20}"
poinker="${3// /%20}"
loinker="${4// /%20}"

echo "https://mickeyorileysooo.com/sysbuilder.php?bg=2&year=${year}&oinker=${oinker}&poinker=${poinker}&loinker=${loinker}"

Now I leave the commas on the command line and run the script:

root@0000 ~ # bash scriptname.sh "2012","White Woman","Green Orcas Rolling","Felix"
https://mickeyorileysooo.com/sysbuilder.php?bg=2&year=2012&oinker=White%20Woman&poinker=Green%20Orcas%20Rolling&loinker=Felix

The commas are now gone and the result is perfect!

https://mickeyorileysooo.com/sysbuilder.php?bg=2&year=2012&oinker=White%20Woman&poinker=Green%20Orcas%20Rolling&loinker=Felix
Vituvo
  • 1,008
  • 1
  • 9
  • 29

1 Answers1

2

If you want to use comma as the delimiter in the script instead of space, set the variable IFS.

IFS=,
set -- $*

After this, $1 will be 2012, $2 will be White Woman, $3 will be Green Orcas Rolling, and $4 will be Felix.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • That is not working correctly. The variables are wrapped with double-quotes. so I made ```IFS=,`` to be ```IFS=","``` and that did not work either. I Appreciate the help. – Vituvo Oct 08 '19 at 19:01
  • I thought the quotes were on the command line, I forgot that this is being read from a file. – Barmar Oct 08 '19 at 19:04
  • ```bash scriptname.sh "2012","White Woman","Green Orcas Rolling","Felix"``` so I will be placing this on the command line and hitting enter. – Vituvo Oct 08 '19 at 19:06
  • If you place that on the command line, the shell removes the quotes before calling the script. – Barmar Oct 08 '19 at 19:06
  • But if you read the line from a file, and then use `bash scriptname.sh "$line"` the quotes are not removed. – Barmar Oct 08 '19 at 19:07
  • I updated my OP. See if this make sense. I need to do a better job with my question it seems. – Vituvo Oct 08 '19 at 23:19
  • I made a stupid mistake, I shouldn't quote `$*`, since that prevents IFS from splitting it. – Barmar Oct 09 '19 at 15:37
  • That was the ticket there. It is now working perfectly. But, take a good look at my question and script. It boggles my mind why you deemed this question to have been already answered in another thread. But so be it. I just hope this helps someone else. I definitely appreciate the time you took to help. – Vituvo Oct 28 '19 at 08:22
  • The question starts by saying that you have these lines in a CSV file that you want to split into fields. Everything else in the question seems like an XY problem. – Barmar Oct 28 '19 at 15:41
  • I don't remember posting anything like that. If you review my edit history, the original first line says: ```I have lines and lines of 4 variables in a csv files that I can quickly pass into a bash script that will generate a complete url.``` I did not change my first line until today. Even with the information I had in my original statement, I don't see the correlation between the 2 threads. I'm just trying to understand this so I don't repeat my mistake. – Vituvo Oct 28 '19 at 16:22