36.2. Shell Wrappers. Comtrol DeviceMaster Software Development Kit
Comtrol DeviceMaster Software Development Kit provides powerful tools and libraries enabling developers to create custom applications quickly and efficiently. With its advanced features, developers can easily integrate diverse devices into their systems, unlocking a world of possibilities.
Advertisement
Advertisement
Advanced Bash-Scripting Guide else
# interactive
...
fi
Alternatively, the script can test for the presence of option "i" in the $- flag.
case $- in
*i*) # interactive shell
;;
*) # non-interactive shell
;;
# (Courtesy of "UNIX F.A.Q.," 1993)
However, John Lange describes an alternative method, using the -t test operator.
# Test for a terminal!
fd=0 # stdin
# As we recall, the -t test option checks whether the stdin, [ -t 0 ],
#+ or stdout, [ -t 1 ], in a given script is running in a terminal.
if [ -t "$fd" ] then
echo interactive else
echo non-interactive fi
# But, as John points out:
# if [ -t 0 ] works ... when you're logged in locally
# but fails when you invoke the command remotely via ssh.
# So for a true test you also have to test for a socket.
if [[ -t "$fd" || -p /dev/stdin ]] then
echo interactive else
echo non-interactive fi
Scripts may be forced to run in interactive mode with the -i option or with a
#!/bin/bash -i
header.
Be aware that this can cause erratic script behavior or show error messages even when no error is present.
36.2. Shell Wrappers
A wrapper is a shell script that embeds a system command or utility, that accepts and passes a set of parameters to that command. [125] Wrapping a script around a complex command-line simplifies invoking it.
This is expecially useful with sed and awk.
A sed or awk script would normally be invoked from the command-line by a
sed -e 'commands'
or
awk 'commands'
. Embedding such a script in a Bash script permits calling it more simply, and makes it
of sed commands to awk. As a saved executable file, you can then repeatedly invoke it in its original form or modified, without the inconvenience of retyping it on the command-line.
Chapter 36. Miscellany 503
Advanced Bash-Scripting Guide
Example 36-1. shell wrapper
#!/bin/bash
# This simple script removes blank lines from a file.
# No argument checking.
#
# You might wish to add something like:
#
# E_NOARGS=85
# if [ -z "$1" ]
# then
# echo "Usage: `basename $0` target-file"
# exit $E_NOARGS
# fi sed -e /^$/d "$1"
# Same as
# sed -e '/^$/d' filename
# invoked from the command-line.
# The '-e' means an "editing" command follows (optional here).
# '^' indicates the beginning of line, '$' the end.
# This matches lines with nothing between the beginning and the end --
#+ blank lines.
# The 'd' is the delete command.
# Quoting the command-line arg permits
#+ whitespace and special characters in the filename.
# Note that this script doesn't actually change the target file.
# If you need to do that, redirect its output.
exit
Example 36-2. A slightly more complex shell wrapper
#!/bin/bash
# subst.sh: a script that substitutes one pattern for
#+ another in a file,
#+ i.e., "sh subst.sh Smith Jones letter.txt".
# Jones replaces Smith.
ARGS=3 # Script requires 3 arguments.
E_BADARGS=85 # Wrong number of arguments passed to script.
if [ $# -ne "$ARGS" ] then
echo "Usage: `basename $0` old-pattern new-pattern filename"
exit $E_BADARGS fi old_pattern=$1 new_pattern=$2 if [ -f "$3" ] then
file_name=$3
Chapter 36. Miscellany 504
Advanced Bash-Scripting Guide else
echo "File \"$3\" does not exist."
exit $E_BADARGS fi
# -----------------------------------------------
# Here is where the heavy work gets done.
sed -e "s/$old_pattern/$new_pattern/g" $file_name
# -----------------------------------------------
# 's' is, of course, the substitute command in sed,
#+ and /pattern/ invokes address matching.
# The 'g,' or global flag causes substitution for EVERY
#+ occurence of $old_pattern on each line, not just the first.
# Read the 'sed' docs for an in-depth explanation.
exit $? # Redirect the output of this script to write to a file.
Example 36-3. A generic shell wrapper that writes to a logfile
#!/bin/bash
# logging-wrapper.sh
# Generic shell wrapper that performs an operation
#+ and logs it.
DEFAULT_LOGFILE=logfile.txt
# Set the following two variables.
OPERATION=
# Can be a complex chain of commands,
#+ for example an awk script or a pipe . . .
LOGFILE= if [ -z "$LOGFILE" ] then # If not set, default to ...
LOGFILE="$DEFAULT_LOGFILE" fi
# Command-line arguments, if any, for the operation.
OPTIONS="$@"
# Log it.
echo "`date` + `whoami` + $OPERATION "$@"" >> $LOGFILE
# Now, do it.
exec $OPERATION "$@"
# It's necessary to do the logging before the operation.
# Why?
Example 36-4. A shell wrapper around an awk script
#!/bin/bash
# pr-ascii.sh: Prints a table of ASCII characters.
START=33 # Range of printable ASCII characters (decimal).
END=127 # Will not work for unprintable characters (> 127).
echo " Decimal Hex Character" # Header.
Chapter 36. Miscellany 505
Advanced Bash-Scripting Guide echo " ------- --- ---------" for ((i=START; i<=END; i++)) do
echo $i | awk '{printf(" %3d %2x %c\n", $1, $1, $1)}'
# The Bash printf builtin will not work in this context:
# printf "%c" "$i" done exit 0
# Decimal Hex Character
# ------- --- ---------
# 33 21 !
# 34 22 "
# 35 23 #
# 36 24 $
#
# . . .
#
# 122 7a z
# 123 7b {
# 124 7c |
# 125 7d }
# Redirect the output of this script to a file
#+ or pipe it to "more": sh pr-asc.sh | more
Example 36-5. A shell wrapper around another awk script
#!/bin/bash
# Adds up a specified column (of numbers) in the target file.
# Floating-point (decimal) numbers okay, because awk can handle them.
ARGS=2
E_WRONGARGS=85 if [ $# -ne "$ARGS" ] # Check for proper number of command-line args.
then
echo "Usage: `basename $0` filename column-number"
exit $E_WRONGARGS fi filename=$1 column_number=$2
# Passing shell variables to the awk part of the script is a bit tricky.
# One method is to strong-quote the Bash-script variable
#+ within the awk script.
# $'$BASH_SCRIPT_VAR'
# ^ ^
# This is done in the embedded awk script below.
# See the awk documentation for more details.
# A multi-line awk script is here invoked by
# awk '
# ...
# ...
Chapter 36. Miscellany 506
Advanced Bash-Scripting Guide
# ...
# '
# Begin awk script.
# ----------------------------awk '
{ total += $'"${column_number}"'
}
END {
print total
}
' "$filename"
# -----------------------------
# End awk script.
# It may not be safe to pass shell variables to an embedded awk script,
#+ so Stephane Chazelas proposes the following alternative:
# ---------------------------------------
# awk -v column_number="$column_number" '
# { total += $column_number
# }
# END {
# print total
# }' "$filename"
# --------------------------------------exit 0
For those scripts needing a single do-it-all tool, a Swiss army knife, there is Perl. Perl combines the
everything ranging from object-oriented programming up to and including the kitchen sink. Short Perl scripts lend themselves to embedding within shell scripts, and there may be some substance to the claim that Perl can totally replace shell scripting (though the author of the ABS Guide remains skeptical).
Example 36-6. Perl embedded in a Bash script
#!/bin/bash
# Shell commands may precede the Perl script.
echo "This precedes the embedded Perl script within \"$0\"." echo "===============================================================" perl -e 'print "This line prints from an embedded Perl script.\n";'
# Like sed, Perl also uses the "-e" option.
echo "===============================================================" echo "However, the script may also contain shell and system commands." exit 0
It is even possible to combine a Bash script and Perl script within the same file. Depending on how the script is invoked, either the Bash part or the Perl part will execute.
Chapter 36. Miscellany 507
Advanced Bash-Scripting Guide
Example 36-7. Bash and Perl scripts combined
#!/bin/bash
# bashandperl.sh
echo "Greetings from the Bash part of the script, $0."
# More Bash commands may follow here.
exit
# End of Bash part of the script.
# =======================================================
#!/usr/bin/perl
# This part of the script must be invoked with
# perl -x bashandperl.sh
print "Greetings from the Perl part of the script, $0.\n";
# Perl doesn't seem to like "echo" ...
# More Perl commands may follow here.
# End of Perl part of the script.
bash$
bash bashandperl.sh
Greetings from the Bash part of the script.
bash$
perl -x bashandperl.sh
Greetings from the Perl part of the script.
It is, of course, possible to embed even more exotic scripting languages within shell wrappers. Python, for example ...
Example 36-8. Python embedded in a Bash script
#!/bin/bash
# ex56py.sh
# Shell commands may precede the Python script.
echo "This precedes the embedded Python script within \"$0.\"" echo "===============================================================" python -c 'print "This line prints from an embedded Python script.\n";'
# Unlike sed and perl, Python uses the "-c" option.
python -c 'k = raw_input( "Hit a key to exit to outer script. " )' echo "===============================================================" echo "However, the script may also contain shell and system commands." exit 0
Wrapping a script around mplayer and the Google's translation server, you can create something that talks back to you.
Example 36-9. A script that speaks
Chapter 36. Miscellany 508
Download
Advertisement
Key features
Rapid Application Development: Streamline development with pre-built libraries and intuitive tools.
Seamless Device Integration: Effortlessly connect and control a wide range of devices, from sensors to actuators.
Enhanced Security: Safeguard sensitive data and ensure system integrity with robust security measures.
Frequently asked questions
The kit supports a wide range of devices, including sensors, actuators, PLCs, and more.
Yes, the kit includes tools and libraries specifically designed for HMI development.
Yes, the kit comes with comprehensive documentation and online support resources.