Tip: template for a safe and portable script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Tip: template for a safe and portable script
# 1  
Old 03-19-2019
Tip: template for a safe and portable script

In an attempt to finally end this article I start this new thread.
Here is a template for a safe and portable script.
Code:
#!/bin/bash
# /bin/bash exists on most still supported Unixes
#
# tr and date behave better with
if [ -n "$LC_ALL" ]; then export LC_ALL=C; else export LANG=C; fi
#
# Unix optional packages install in
opt_path=/usr/local/bin
solaris_opt_path=/usr/sfw/bin:/opt/csw/bin
#
# set PATH so no PATH is inherited, export it to all children
# Solaris is Posix-compliant in /usr/xpg4/bin and BSD-compliant in /usr/ucb
export PATH=/usr/xpg4/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:${opt_path}:${solaris_opt_path}
#
# HP-UX is Posix-compliant with
export UNIX95=
#
# GREP_OPTIONS can obscure GNU grep
unset GREP_OPTIONS
#
# LD_* can obscure external commands
unset LD_LIBRARY_PATH LD_PRELOAD
#
# no glob file-matching in command args and word lists (for loop)
set -f
#
# prefer builtin commands and use Posix-compliant options
# check with "shellcheck"

Ok this is my wisdom. Perhaps you want to add something from your wisdom?
# 2  
Old 03-19-2019
Jim McNamara mentioned POSIXLY_CORRECT shortly here:

GNU Coding Standards: Non-GNU Standards

Last edited by stomp; 03-20-2019 at 06:28 AM..
This User Gave Thanks to stomp For This Post:
# 3  
Old 03-19-2019
Regarding the du and df:
I have seen an old Unix man page that tells to look up the block size in the /usr/include/**.h files.
Not very user-friendly. In SysV it has always been 512 bytes. I think they wanted to have a small buffer in the kernel driver (and physical sector size was much below 512 bytes at that time).
du -k and df -k seem to be widespread in Unix and are allowed(ignored) in Linux.
But in Solaris, df -k not only changes the values to kilobyte, it also changes the (really bad) SysV-style output to the BSD-style (that BSD Unix and Linux always use).
Being Posix-compliant, e.g. by using my template, df -kP might be quite portable (in Linux even avoids a brain-damaged "split long lines in two even if destination is not a terminal").
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question