Sending awk variables into curl in a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sending awk variables into curl in a bash script
# 1  
Old 09-16-2015
Sending awk variables into curl in a bash script

Hello experts!

I have a file1 with the following format (yr,day, month, hour,minute):
Code:
201201132435
201202141210
201304132030
201410100110
...

What i want to do is to assign variables and then use them in the curl command to download the text of each event from a web page.

What I have done so far is:
Code:
#!/bin/sh
#
yr=`awk '{$1=substr($1, 6, 2)}1'  file1`
mo=`awk '{$1=substr($1, 8, 2)}1'  file1`
da=`awk '{$1=substr($1, 10, 2)}1'  file1`
ho=`awk '{$1=substr($1, 12, 2)}1'  file1`
mi=`awk '{$1=substr($1, 14, 2)}1' file1 `
#
curl -L http://webpage/${yr}/${mo}/track${yr}${mo}${da}${ho}${mi}$_infos.html

but it seems that the variables are not evolving gradually all together but separately
# 2  
Old 09-16-2015
Quote:
Originally Posted by phaethon
[...]
but it seems that the variables are not evolving gradually all together but separately
Code:
yr=`awk '{$1=substr($1, 6, 2)}1'  file1`

yr will not hold just one year, but every year for each line in file1. The same for each of your variables mo, da, ho and mi. Nonetheless, the characters extracted are off: substr($1, 6, 2) doesn't return the representation of a year.
Code:
$ awk '{$1=substr($1, 6, 2)}1' file1 
11
21
41
01

This will do:
Code:
$ awk '{$1=substr($1, 1, 4)}1' file1
2012
2012
2013
2014

Perhaps, try:
Code:
while read d; do
    yr=${d:0:4}
    mo=${d:4:2}
    da=${d:6:2}
    ho=${d:8:2}
    mi=${d:10:2}
    curl -L http://webpage/${yr}/${mo}/track${yr}${mo}${da}${ho}${mi}_infos.html
    # if yr is supposed to be yyyy
    # curl -L http://webpage/${yr}/${mo}/track${d}_infos.html
    
done < file1

If yr is supposed to be represented with only the last two digits, replace yr=${d:0:4} to yr=${d:2:2}

Of course, the whole process can be reduced to:

Code:
while read d; do
    #curl -L http://webpage/${d:0:4}/${d:4:2}/track${d}_infos.html
    # or
    #curl -L http://webpage/${d:2:2}/${d:4:2}/track${d:2}_infos.html
done < file1


Last edited by Aia; 09-17-2015 at 12:44 AM.. Reason: Add an alternative
This User Gave Thanks to Aia For This Post:
# 3  
Old 09-17-2015
What Aia suggested is fine if you're using a system where /bin/sh is not a pure Bourne shell (as it is on Solaris systems) or a 1988 Korn shell (as it is on many AIX systems). But, in addition to what Aia already commented on, there is nothing in your script that sets the shell variable _infos, so, unless that variable is being inherited from an exported variable in the environment of your script, the $_infos in your invocations of curl will be replaced by an empty string by the shell each time it invokes curl.

When posting questions to these forums, it always helps us provide answers that will work better in your environment if you tell us what operating system and shell you're using when you post questions. And, if you're using shell variables that aren't defined in your script, please tell us where those variables are being initialized.

If you are using a Solaris system or an AIX system or an HP/UX system, you're likely to get a bunch of syntax errors from Aia's suggestion, and we'll need to know which OS you're using, whether we can use a shell other than /bin/sh on your system (such as /usr/xpg4/bin/sh on a Solaris system or /bin/ksh93 on AIX) so we'll know if we can just use a different shell or if we need to convert the shell substring expansion into a single awk script (if you're stuck with a pure Bourne shell) or into POSIX conforming variable expansions in other shells.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 09-17-2015
Thank you Aia it perfectly worked! no words! Don Cragun thank you for the suggestion. The Aia's code worked on my opensuse 13.2 system because "_infos" is not a variable but plain text.

Last edited by phaethon; 09-17-2015 at 06:15 AM..
# 5  
Old 09-17-2015
With a recent shell, this might work:
Code:
while { read -n4 A; read -n2 B; read -n2 C; read -n2 D; read -n2 E; read REST; }; do echo $A $B $C $D $E; done < file
2012 01 13 24 35
2012 02 14 12 10
2013 04 13 20 30
2014 10 10 01 10

This User Gave Thanks to RudiC For This Post:
# 6  
Old 09-17-2015
It looks like Aia guessed correctly by ignoring the last dollar-sign in your original post:
Code:
curl -L http://webpage/${yr}/${mo}/track${yr}${mo}${da}${ho}${mi}$_infos.html

When the shell processes $_infos.html, it treats _infos as a variable name, and, if it is unset, expands $_infos.html to the string .html; not to the string _infos.html.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-17-2015
my fault the "$_infos" is wrong
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl not accepting spaces in script via variables

Hi All, I'm trying to run a script which issues rest commands via curl to an endpoint. If I put spaces in fields via something like insomnia, it works, but when I try from an input file, it's failing with a json error. while IFS=, read mname oname <------ my input file... (10 Replies)
Discussion started by: say170
10 Replies

2. Shell Programming and Scripting

Curl , download file with user:pass in bash script

Hello, My question is about curl command. (ubuntu14.04) In terminal, I am able to download my mainfile with: curl -u user1:pass1 http://11.22.33.44/******* When I convert it into bash script like this: #!/bin/bash cd /root/scripts computer_ip=11.22.33.44 curl -u $1:$2... (8 Replies)
Discussion started by: baris35
8 Replies

3. Shell Programming and Scripting

Using curl in bash script

Hello. I have pick up a script from internet to track errors from curl command. #!/bin/bash # URL_TO_TEST="http://www.xxxxxx.yyy" MY_VAR=curl_init("$URL_TO_TEST") ; curl_setopt($MY_VAR, CURLOPT_HEADER, 1); curl_setopt($MY_VAR, CURLOPT_RETURNTRANSFER, 1); curl_setopt($MY_VAR,... (2 Replies)
Discussion started by: jcdole
2 Replies

4. Shell Programming and Scripting

Curl/http 503 error with bash script

I am trying to use REST API and curl in a bash script to generate a http report. The curl command at the end of the script should generate a html file but instead I get an error "HTTP/1.1 503 Service Unavailable". This is the script #!/bin/bash export... (7 Replies)
Discussion started by: kieranfoley
7 Replies

5. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

6. Shell Programming and Scripting

Bash for loop with awk and variables

I'm relatively new to bash scripting and am trying to use awk inside a bash for loop but am having a problem with the variables. for i in $(seq 30 5 60) do # Define variables up and down in AWK eval $(awk 'BEGIN{ print "up=$((i+1))"}' < /dev/null) eval $(awk 'BEGIN{ print... (2 Replies)
Discussion started by: lily-anne
2 Replies

7. UNIX for Dummies Questions & Answers

curl - sending headers, no-cache

Hello, I have question about curl - how can I disable caching on server side? I actually am not on unix/linux but I use win32gnu curl. I look for some good web- manual and I would like to find some good site which describes how looks headers of various Browsers (Safari, Opera). I have FF and... (0 Replies)
Discussion started by: webhope
0 Replies

8. Shell Programming and Scripting

using awk compare two variables in bash

ok this is probably going to turn out to be something really stupid but i've tried to use the following command in a script but the output is just a blank screen and i have to use Ctrl c to exit it. awk 'BEGIN {printf "%.2f\n", '${bashArray}'>='$Variable' {print '${bashArray}'}}' the command... (2 Replies)
Discussion started by: zagreus360
2 Replies

9. Shell Programming and Scripting

Bash script idea using cUrl -- possible?

I have an alias already in my .bash_profile to download files using cUrl's -o (output to file, user provides the file name) option. I find I'm using it quite a bit, so I wanted to write a script to run "curl -o", taking the necessary inputs - file name and URL from which to download - and then... (3 Replies)
Discussion started by: SilversleevesX
3 Replies

10. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies
Login or Register to Ask a Question