|
|||
|
Creating the Underlying Working Scripts The underlying scripts are the actual commands that make changes to your systems. For all of your automation tasks, it is a good idea to do the dirty work with command-line programs. They may be bash or Perl scripts, as long as they do not require direct user interaction. If a command-line program can be completely controlled via command-line arguments and/or sending text into stdin it can be used in any way imaginable. You can run it manually. You can run it from a text-based user interface. You can run it from a web page. You can also run it from a remote system using an access protocol such as SSH. If the command will definitely be run manually, taking data on stdin may be the best option. Any text you want displayed interactively can be sent out on stderr and anything that would need to be parsed by another program can be sent out on stdout. Here is a simple example: #!/bin/bash echo -n "Enter username to add: " >&2 read new_user echo -n "Enter full name: " >&2 read full_name # ... create user ... echo "New account successfully created!" >&2 echo "New UserID: $new_uid" If you run this program interactively, you see the following: Enter username to add: kirk Enter full name: Kirk Bauer New account successfully created! New UserID: 1001 This script is pretty simple to use as it stands, but you may want to execute it from a web interface. Or, you may want to write a wrapper script that adds large numbers of users automatically. This script can be executed non-interactively as follows: #!/bin/bash echo -e 'kirk\nKirk Bauer' | my_adduser 2>/dev/null In this case, I use the echo command to send the two required pieces of information into stdin. The -e switch tells echo to allow escape sequences within the string—the \n for a carriage return in our case. This option is available on the GNU version of echo but is not recognized in many other versions. Fortunately, in this case, we are using the version of echo that is built into the bash shell, so it doesn't really matter. Just remember this, if you changed the command to /bin/echo because you would then be calling your operating system's version of the command. We direct stderr to /dev/null because we don't care about it. All that remains is stdout, which we could parse to retrieve the UserID. If you executed the previous script, you would see only the following: New UserID: 1001 Another way to send data into such a script is to use a here document. Here documents are documents that are inserted directly into a shell script file. Here is an example: #!/bin/bash my_adduser 2>/dev/null << __END__ kirk Kirk Bauer __END__ In this example, we only have the two pieces of data necessary to add a new account. These could easily be passed in as parameters to make the script even easier to use from within other scripts. It can make it more difficult to use interactively, though. This is especially true if your script has twenty questions to ask instead of just two. Here is the same script using parameters: #!/bin/bash usage() { echo "Usage: $0 [username] [full name]" 2>/dev/null exit 99; } [ "$1" == "--help" ] && usage [ $# -ne 2 ] && usage new_user="$1" full_name="$2" # ... create user ... echo "New account successfully created!" >&2 echo "New UserID: $new_uid" Notice that I have included a usage function in this example. If you are going to require certain command-line arguments, it is necessary that you provide usage instructions. Even with only two arguments, you can easily forget what arguments you need and in what order you should specify them. You should display the usage information if there are any errors parsing the command line (such as missing arguments) or if a standard parameter (such as –help) is given. This allows a person to determine how to use any command without any prior knowledge (except, of course, that they can use the –help parameter). As one final option, you can set up your scripts so that they can accept command- line arguments, and if these arguments are not provided, the script can prompt the user. This allows the script to be run in a friendly manner interactively on the shell yet still is easy to use from within shell scripts. #!/bin/bash usage() { echo "Usage: $0 [username] [full name]" 2>/dev/null exit 99; } [ "$1" == "--help" ] && usage new_user="$1" full_name="$2" [ -n "$new_user" ] || { echo -n "Enter username to add: " >&2 read new_user } [ -n "$full_name" ] || { echo -n "Enter full name: " >&2 read full_name } # ... create user ... echo "New account successfully created!" >&2 echo "New UserID: $new_uid" |