InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Keeping Your Data with Your Script in bash
Bash

Keeping Your Data with Your Script in bash

InfinityCoder January 12, 2017

You need input to your script, but don’t want a separate file.

Use a here-document, with the << characters, redirecting the text from the command line rather than from a file.

When put into a shell script, the script file then contains the data along with the script.
Here’s an example of a shell script in a file we call ext:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat ext
#
# here is a "here" document
#
grep $1 <<EOF
mike x.123
joe x.234
sue x.555
pete x.818
sara x.822
bill x.919
EOF
$

It can be used as a shell script for simple phone number lookups:

1
2
3
$ ext bill
bill x.919
$

or:

1
2
3
$ ext 555
sue x.555
$

The grep command looks for occurrences of the first argument in the files that are named, or if no files are named it looks to standard input.
A typical use of grep is something like this:

1
$ grep somestring file.txt

or:

1
$ grep myvar *.c

In our ext script we’ve parameterized the grep by making the string that we’re searching for be the parameter of our shell script ($1).

Whereas we often think of grep as searching for a fixed string through various different files, here we are going to vary what we search for, but search through the same data every time.
We could have put our phone numbers in a file, say phonenumbers.txt, and then used that filename on the line that invokes the grep command:

1
grep $1 phonenumbers.txt

However, that requires two separate files (our script and our datafile) and raises the question of where to put them and how to keep them together.
So rather than supplying one or more filenames (to search through), we set up a here-document and tell the shell to redirect standard input to come from that (temporary) document.
The << syntax says that we want to create such a temporary input source, and the EOF is just an arbitrary string (you can choose what you like) to act as the terminator of the temporary input.

It is not part of the input, but acts as the marker to show where it ends. The regular shell script (if any) resumes after the marker.
We also might add -i to the grep command to make our search is case-insensitive. Thus, using grep -i $1 <<EOF would allow us to search for “Bill” as well as “bill.”

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Testing with Regular Expressions in bash
Sometimes even the extended pattern matching of the extglob option …

Testing with Regular Expressions in bash

Finding bash Portably for #! in bash
You need to run a bash script on several machines, …

Finding bash Portably for #! in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap