Reputation: 301
I'm currently writing a script that will ultimately parse some baseball player ID numbers from MLB's Gameday data. I have set up my script so far to input today's date into the URL and to accept a team name as a command line argument.
The next step is to pass the URL to curl to get a list of today's games and the find the appropriate one by using grep with the team name.
When I write this on the command line:
curl -s http://gd2.mlb.com/components/game/mlb/year_2017/month_05/day_20/ | grep tormlb
It works.
However, when I write this in my script:
mlb_dir=$(curl -s $url | grep $team)
echo $mlb_dir
I am returned a mess of HTML without any line breaks. $url is equivalent to the url in the code block above and $team is set to "tormlb" just like above.
I am confused how I can get the result I want when I use the command line but not when I run my script. Any clue where I am going wrong?
Upvotes: 4
Views: 2457
Reputation: 13016
When you pass a variable to a command, the variable is expanded and then split into arguments by whitespace. echo
then outputs its arguments separated by spaces.
To ensure the variable is treated as a single argument, wrap it in double quotes. You should also do this for the other variables you are using in your code.
mlb_dir=$(curl -s "$url" | grep "$team")
echo "$mlb_dir"
Further information: http://tldp.org/LDP/abs/html/quotingvar.html
Upvotes: 5