Linux Blog

Shell Script Input Parameters

Filed under: Shell Script Sundays — TheLinuxBlog.com at 8:00 am on Sunday, March 1, 2015

When writing shell scripts, it is often useful to have some type of input parameters. There are a few ways to do this, and it turns out I’ve written about two of the main ways before. The first is the Shell Script to get User Input post which is essentially using read, and the second is Creating Script Parameters with GetOpts. I’ll also cover a quick and dirty way of getting input into a shell script. From this you’ll be able to see the differences and decide what input method is best for your situation.

Read Line

The User Input method prompts a user for input using read. This is useful for prompting a user once inside your script, but it can also be used to read input from a file with input redirection. Here is an example read line script and usage:

1
2
3
while read inputline;
do echo Read: $inputline;
done;

To use this method, we’ll create a test.txt file with seq:

seq 1 3 >> test.txt

Then create the testinput.sh script and chmod it from above:

thelinuxblog.com:~ owen$ echo "while read inputline; do echo Read: \$inputline; done" > testinput.sh; chmod 755 testinput.sh;

Now we can use input redirection to use the test.txt file for input:

1
2
3
4
thelinuxblog.com:~ owen$ ./testinput.sh < test.txt 
Read: 1
Read: 2
Read: 3

So, from this you see that it echo’s back each line from text.txt and prepends “Read:” to it.


getopts

Using getopts you can achieve a similar result, but the usage would be a little different.

This example has one option “-e” and echo the input out.

1
2
3
4
5
while getopts ":e:" ARG;
do case "${ARG}" in
e) echo Read: "${OPTARG}";;
esac;
done

Save that to testinput.sh and chmod it. Now, when we run with the -e test we get:

1
2
thelinuxblog.com:~ owen$ ./testinput.sh -e test
Read: test

So, to use our test.txt input file we actually have to use a loop:

1
2
3
4
thelinuxblog.com:~ owen$ for i in `cat test.txt`; do ./testinput.sh -e $i; done; 
Read: 1
Read: 2
Read: 3

As you can see, this isn’t ideal, so I’d avoid using this way when possible for reading from a file, but for options or switches getopts works great. You could get the best of both worlds by using a combination of switches and read. An example of this would be to have have a -i (for inputfile) and use readline to process that file, and another switch for a single action.

Special Variable Types

The quick and dirty shell script input method is special variable types such as $1, ($2, $3, etc.) You can acquire input this way, but it requires some additional processing to be safe. It is however a quick way to get input if you’re in a hurry or just don’t care. For an example we’ll use a script like above:

1
2
INPUT=$1
echo Read: $INPUT;

This produces similar results as a single readline:

1
2
thelinuxblog.com:~ owen$ ./testinput3.sh TEST
Read: TEST

but it will not process lines from a file without a loop like the getopts example:

1
2
3
4
thelinuxblog.com:~ owen$ ./testinput3.sh < test.txt 
Read:
 
thelinuxblog.co:~ owen$ for i in `cat test.txt`; do ./testcmd.sh $i; done;

As you can see, this is not ideal.

The special variable types do have some more useful tricks up their sleeves.

$# is used for the number of arguments passed which is good for checking to see if the correct number has been passed, or for using that number to iterate a loop.

$0 gives the name of the called file.

Check out the TLDP for information on these.

Man Pages for commands in this post »

bash

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>