19.7. Creating a function file

Let’s create a function file that holds one function. We will load it into the shell, test it, make a change then reload it.

Create a file called functions.main and type in the following:

						$ pg functions.main 
#!/bin/sh 
# functions.main 
# 
# findit: this is front end for the basic find command 
findit() {
# findit 
if [ $# -lt 1 ]; then 
  echo "usage :findit file" 
  return 1 
fi 
find / -name $1 -print 

The above is a script used previously in the book, but now it has been turned into a function. It’s a front end to a basic find command. If no argument is passed to it, a return with a value of 1 is executed meaning errors have occurred. Notice on the error statement that the actual function name is shown, because if we had ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.