Sponsored Content
Top Forums Shell Programming and Scripting Tip: template for a safe and portable script Post 303032522 by MadeInGermany on Tuesday 19th of March 2019 02:47:02 PM
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?
 
getusershell(3C)					   Standard C Library Functions 					  getusershell(3C)

NAME
getusershell, setusershell, endusershell - get legal user shells SYNOPSIS
#include <unistd.h> char *getusershell(void); void setusershell(void); void endusershell(void); DESCRIPTION
The getusershell() function returns a pointer to a legal user shell as defined by the system manager in the file /etc/shells. If /etc/shells does not exist, the following locations of the standard system shells are used in its place: /bin/bash /bin/csh /bin/jsh /bin/ksh /bin/ksh93 /bin/pfcsh /bin/pfksh /bin/pfsh /bin/sh /bin/tcsh /bin/zsh /sbin/jsh /sbin/pfsh /sbin/sh /usr/bin/bash /usr/bin/csh /usr/bin/jsh /usr/bin/ksh /usr/bin/ksh93 /usr/bin/pfcsh /usr/bin/pfksh /usr/bin/pfsh /usr/bin/sh /usr/bin/tcsh /usr/bin/zsh /usr/sfw/bin/zsh /usr/xpg4/bin/sh The getusershell() function opens the file /etc/shells, if it exists, and returns the next entry in the list of shells. The setusershell() function rewinds the file or the list. The endusershell() function closes the file, frees any memory used by getusershell() and setusershell(), and rewinds the file /etc/shells. RETURN VALUES
The getusershell() function returns a null pointer on EOF. BUGS
All information is contained in memory that may be freed with a call to endusershell(), so it must be copied if it is to be saved. NOTES
Restricted shells should not be listed in /etc/shells. SunOS 5.11 1 Nov 2007 getusershell(3C)
All times are GMT -4. The time now is 02:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy