Table of Contents for
Linux Shell Scripting Bootcamp

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Linux Shell Scripting Bootcamp by James Kent Lewis Published by Packt Publishing, 2017
  1. Cover
  2. Table of Contents
  3. Linux Shell Scripting Bootcamp
  4. Linux Shell Scripting Bootcamp
  5. Credits
  6. About the Author
  7. Acknowledgement
  8. About the Reviewer
  9. www.PacktPub.com
  10. Customer Feedback
  11. Preface
  12. What you need for this book
  13. Who this book is for
  14. Conventions
  15. Reader feedback
  16. Customer support
  17. 1. Getting Started with Shell Scripting
  18. Demonstrating the use of scripts
  19. Summary
  20. 2. Working with Variables
  21. Validating parameters using conditional statements
  22. Comparison operators for strings
  23. Environment variables
  24. Summary
  25. 3. Using Loops and the sleep Command
  26. Screen manipulation
  27. Indenting your code
  28. Using the for statement
  29. Leaving a loop early
  30. The sleep command
  31. Watching a process
  32. Creating numbered backup files
  33. Summary
  34. 4. Creating and Calling Subroutines
  35. File redirection
  36. Command piping
  37. Subroutines
  38. Using parameters
  39. Making a current backup of your work
  40. Summary
  41. 5. Creating Interactive Scripts
  42. Summary
  43. 6. Automating Tasks with Scripts
  44. Summary
  45. 7. Working with Files
  46. Reading files
  47. Reading and writing files
  48. Reading and writing files interactively
  49. File checksums
  50. File encryption
  51. Summary
  52. 8. Working with wget and curl
  53. wget and recursion
  54. wget options
  55. curl
  56. Summary
  57. 9. Debugging Scripts
  58. Automatic backups
  59. More syntax errors
  60. Logic errors
  61. Using set to debug scripts
  62. Summary
  63. 10. Scripting Best Practices
  64. ssh and scp
  65. Find and use a good text editor
  66. Environment variables and aliases
  67. ssh prompt
  68. Testing an archive
  69. Progress indicator
  70. Creating new commands from a template
  71. Alerting the user
  72. Summary
  73. Index

Chapter 8. Working with wget and curl

This chapter will show how to use wget and curl to gather information directly from the internet.

The topics covered in this chapter are:

  • Show how to get information using wget.
  • Show how to get information using curl.

Scripts that can gather data in this way can be very powerful tools to have at your disposal. As you will see from this chapter, you can get stock quotes, lake levels, just about anything automatically from web sites anywhere in the world.

Introducing the wget program

You may have already heard about or even used the wget program. It is a command line utility that can be used to download files from the Internet.

Here is a screenshot showing wget in its most simplest form:

Introducing the wget program

wget options

In the output you can see that wget downloaded the index.html file from my jklewis.com website.

This is the default behavior of wget. The standard usage is:

  wget [options] URL

where URL stands for Uniform Resource Locator, or address of the website.

Here is just a short list of the many available options with wget:

Parameter

Explanation

-o

log file, messages will be written here instead of to STDOUT

-a

same as -o excepts it appends to the log file

-O

output file, copy the file to this name

-d

turn debugging on

-q

quiet mode

-v

verbose mode

-r

recursive mode

Let's try another example:

wget options

The -o option was used in this case. The return code was checked and a code of 0 means no failure. There was no output because it was directed to the log file which was displayed by the cat command.

The -o option, write output to file, was used in this case. There was no output displayed because it was directed to the log file which was then shown by the cat command. The return code from wget was checked and a code of 0 means no failure.

Notice that this time it named the downloaded file index.html.1. This is because index.html was created in the previous example. The author of this application did it this way to avoid overwriting a previously downloaded file. Very nice!

Take a look at this next example:

wget options

Here we are telling wget to download the file given (shipfire.gif).

In this next screenshot we show how wget will return a useful error code:

wget options

wget return codes

This error occurred because there is no file named shipfire100.gif in the base directory on my website. Notice how the output shows a 404 Not Found message, this is seen very often on the Web. In general, it means a requested resource was not available at that time. In this case the file isn't there and so that message appears.

Note too how wget returned an 8 error code. The man page for wget shows this for the possible exit codes from wget:

Error codes

Explanation

0

No problems occurred.

1

Generic error code.

2

Parse error. For instance when parsing command-line options, the .wgetrc or .netrc files

3

File I/O error.

4

Network failure.

5

SSL verification failure.

6

Username/password authentication failure.

7

Protocol errors.

8

Server issued an error response.

A return of 8 makes pretty good sense. The server could not find the file and so returned a 404 error code.

wget configuration files

Now is a good time to mention the different wget configuration files. There are two main files, /etc/wgetrc is the default location of the global wget startup file. In most cases you probably should not edit this unless you really want to make changes that affect all users. The file $HOME/.wgetrc is a better place to put any options you would like. A good way to do this is to open both /etc/wgetrc and $HOME/.wgetrc in your text editor and then copy the stanzas you want into your $HOME./wgetrc file.

For more information on the wget config files consult the man page (man wget).

Now let's see wget in action. I wrote this a while back to keep track of the water level in the lake I used to go boating in:

Chapter 8 - Script 1

#!/bin/sh
# 6/5/2017
# Chapter 8 - Script 1

URL=http://www.arlut.utexas.edu/omg/weather.html
FN=weather.html
TF=temp1.txt                 # temp file
LF=logfile.txt               # log file

loop=1
while [ $loop -eq 1 ]
do
 rm $FN 2> /dev/null         # remove old file
 wget -o $LF $URL
 rc=$?
 if [ $rc -ne 0 ] ; then
  echo "wget returned code: $rc"
  echo "logfile:"
  cat $LF

  exit 200
 fi

 date
 grep "Lake Travis Level:" $FN > $TF
 cat $TF | cut  -d ' ' -f 12 --complement

 sleep 1h
done

exit 0

This output is from June 5, 2017. It's not much to look at but here it is:

Chapter 8 - Script 1

You can see from the script and the output that it runs once every hour. In case you were wondering why anyone would write something like this, I needed to know if the lake level went below 640 feet as I would have had to move my boat out of the marina. This was during a severe drought back in Texas.

There are a few things to keep in mind when writing a script like this:

  • When first writing the script perform the wget once manually and then work with the downloaded file.
  • Do not use wget several times in a short period of time or else you may get blocked by the site.
  • Keep in mind that HTML programmers like to change things all the time and so you may have to adjust your script accordingly.
  • When you finally get the script just right be sure to activate the wget again.