<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Unix Shell Scripting - Best Practises in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948426#M95026</link>
    <description>Re-opened thru popular demand !</description>
    <pubDate>Wed, 21 Feb 2007 10:45:48 GMT</pubDate>
    <dc:creator>Hunki</dc:creator>
    <dc:date>2007-02-21T10:45:48Z</dc:date>
    <item>
      <title>Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948418#M95018</link>
      <description>What are the Best practises in Unix Shell Scripting . Lets build a list together and also if somebody can share a great shell scripting template.&lt;BR /&gt;&lt;BR /&gt;Thanks,</description>
      <pubDate>Wed, 21 Feb 2007 00:55:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948418#M95018</guid>
      <dc:creator>Hunki</dc:creator>
      <dc:date>2007-02-21T00:55:13Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948419#M95019</link>
      <description>You may want to put this in the languages and scripting forum.  They may have links already to refererence material.&lt;BR /&gt;&lt;BR /&gt;1) Using set -u for uninit vars&lt;BR /&gt;2) Using proper quoting, handling string with blanks and other specials.&lt;BR /&gt;3) Using sh -n to check scripts.&lt;BR /&gt;4) Using set -x -v to debugging.&lt;BR /&gt;5) Using typeset -i&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Feb 2007 01:06:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948419#M95019</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2007-02-21T01:06:09Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948420#M95020</link>
      <description>Hi,&lt;BR /&gt;my best pratice advice is:&lt;BR /&gt;1. Comment your code&lt;BR /&gt;We have all come across a script we wrote 2 years ago, it's now going to take ages to work out again what the script did.&lt;BR /&gt;&lt;BR /&gt;2. Establish the requirements&lt;BR /&gt;Analyse the problem propery, rather than going for a quick fix. Temporary solutions have a nasty habit of turning permanent.&lt;BR /&gt;&lt;BR /&gt;3. Pick the right shell/tool&lt;BR /&gt;After point 2 decide what is the best implementation to get the job done.&lt;BR /&gt;&lt;BR /&gt;In general the methods are the same for shell scripts as they are for traditional software languages.&lt;BR /&gt;&lt;BR /&gt;And finally test-debug-test and test again !</description>
      <pubDate>Wed, 21 Feb 2007 06:01:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948420#M95020</guid>
      <dc:creator>Peter Godron</dc:creator>
      <dc:date>2007-02-21T06:01:13Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948421#M95021</link>
      <description>Shalom,&lt;BR /&gt;&lt;BR /&gt;I created a stub years ago from which I started all shell scripts.&lt;BR /&gt;&lt;BR /&gt;It contained prompts for documentation and contained a change log a the top.&lt;BR /&gt;&lt;BR /&gt;It was very helpful when people used it.&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=51050" target="_blank"&gt;http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=51050&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;There are a lot of examples of best practices in this thread and it should be looked to for guidance.&lt;BR /&gt;&lt;BR /&gt;SEP</description>
      <pubDate>Wed, 21 Feb 2007 07:07:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948421#M95021</guid>
      <dc:creator>Steven E. Protter</dc:creator>
      <dc:date>2007-02-21T07:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948422#M95022</link>
      <description>The best practice is to practice a whole lot.&lt;BR /&gt;&lt;BR /&gt;1) Typeset everything.&lt;BR /&gt;2) Normal output goes to stdout; error/usage messages go to stderr.&lt;BR /&gt;3) Use {}'s around each and every variable.&lt;BR /&gt;4) Use X=$(commnd) rather than X=`command`.&lt;BR /&gt;5) No news is good news. Don't output stupid messages that say "all is well"; instead, produce output when things are bad.&lt;BR /&gt;6) Always return a valid exit status with 0 meaning OK.&lt;BR /&gt;&lt;BR /&gt;Nowadays, if I were starting over, I would learn only enough shell to get by and concentrate on Perl.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Feb 2007 08:43:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948422#M95022</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2007-02-21T08:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948423#M95023</link>
      <description>Hi Hunki:&lt;BR /&gt;&lt;BR /&gt;Be efficient!  Think about the number of processes and/or temporary files you are using!&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Don't use 'cat' to read a file and simply pipe the output to a 'read'.  Instead do:&lt;BR /&gt;&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;...&lt;BR /&gt;done &amp;lt; file&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Don't use 'grep' to find patterns and then pipe the output to 'awk' to extract a field.  Use awk's pattern matching and do the operation with one process!&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Use pipes to glue the output of one process to the input stream of another instead of using temporary files.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Use 'xargs' instead of '-exec' with 'find' *OR* use '-exec' with the "+" terminator instead of the ";" to achieve the same performance gain.  See the 'find' manpages.&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Learn to use traps (signal handlers).  This is a clean, handy way to automatically remove temporary files when a shell process terminates (abnormally or normally).&lt;BR /&gt;&lt;BR /&gt;&amp;gt; Use shell built-ins instead of spawning new external processes.  Use shell parameter substitution instead of 'basename' and 'dirname', for example.  &lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 21 Feb 2007 09:12:45 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948423#M95023</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-02-21T09:12:45Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948424#M95024</link>
      <description>My recommendations:&lt;BR /&gt;&lt;BR /&gt;1) ALWAYS use the she-bang syntax on the first line of your script to declare what interpreter (sh, ksh, perl, etc) the script is to use.&lt;BR /&gt;&lt;BR /&gt;2) Do NOT use back-ticks to execute commands.  Instead use $() syntax.  Like DATE=$(/usr/bin/date +%m%d%Y)&lt;BR /&gt;&lt;BR /&gt;3) Document your script.  Use lots of comments so whoever comes in later can figure out what you did.&lt;BR /&gt;&lt;BR /&gt;4) Use the FULL path to executables so you always know what you are running (/usr/bin/date rather than just date).&lt;BR /&gt;&lt;BR /&gt;5) Set your environment in the script.  You may not always run the script interactively.  You may run it via cron which has a sparse environment by default.&lt;BR /&gt;&lt;BR /&gt;6) Use 'set -u' as mentioned above.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Feb 2007 09:46:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948424#M95024</guid>
      <dc:creator>Patrick Wallek</dc:creator>
      <dc:date>2007-02-21T09:46:59Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948425#M95025</link>
      <description>Another very important shell technique:&lt;BR /&gt; &lt;BR /&gt;- NEVER use the existing $PATH value. Export PATH in your script so you know exactly where the shell will look for your commands. Start with:&lt;BR /&gt; &lt;BR /&gt;export PATH=/usr/bin&lt;BR /&gt; &lt;BR /&gt;and build on that only as necessary. This avoids a *LOT* of environment issues and improves security.&lt;BR /&gt; &lt;BR /&gt;Also make sure that common shell and Unix commands in your script are not aliased. It's common to have 'nice' options aliased (like rm -i) so rm in your script suddenly goes interactive with: (y/n). Use unalias to stabilize commands you are using.&lt;BR /&gt; &lt;BR /&gt;And a great shell scripting site:&lt;BR /&gt; &lt;BR /&gt;&lt;A href="http://www.shelldorado.com/" target="_blank"&gt;http://www.shelldorado.com/&lt;/A&gt;</description>
      <pubDate>Wed, 21 Feb 2007 10:44:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948425#M95025</guid>
      <dc:creator>Bill Hassell</dc:creator>
      <dc:date>2007-02-21T10:44:41Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948426#M95026</link>
      <description>Re-opened thru popular demand !</description>
      <pubDate>Wed, 21 Feb 2007 10:45:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948426#M95026</guid>
      <dc:creator>Hunki</dc:creator>
      <dc:date>2007-02-21T10:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948427#M95027</link>
      <description>My absolute most hated BAD practice in scripts is people doing:&lt;BR /&gt;&lt;BR /&gt;su - user -c cmd&lt;BR /&gt;&lt;BR /&gt;The use of 'su -' in scripts should be absolutely banned! This will always invoke /etc/profile and .profile and who knows what is in there that is inappropriate for a script. The least of it could be a bunch of terminal dependent code (result: you get lots of stty: not a typewriter messages), and the worst of it could be a DBA who implements a handy interactive menu in the .profile for the oracle user (result - none of your oracle scripts work or work on the wrong database).&lt;BR /&gt;&lt;BR /&gt;The correct way to do this is:&lt;BR /&gt;&lt;BR /&gt;export MY_ENV_VAR=xyz&lt;BR /&gt;su user -c cmd&lt;BR /&gt;&lt;BR /&gt;for example:&lt;BR /&gt;&lt;BR /&gt;export ORACLE_HOME=/oracle/10g&lt;BR /&gt;export ORACLE_SID=mydb&lt;BR /&gt;su oracle -c dbshut&lt;BR /&gt;&lt;BR /&gt;One gotcha to watch for though is that some env vars are removed by su (such as HOME SHLIB_PATH, LD_LIBRARY_PATH - see SU_KEEP_ENV_VARS in security(4) for details) and may need to be reset, e.g.&lt;BR /&gt;&lt;BR /&gt;export ORACLE_HOME=/oracle/10g&lt;BR /&gt;export ORACLE_SID=mydb&lt;BR /&gt;su oracle -c "export SHLIB_PATH=${ORACLE_HOME}/lib;dbshut"&lt;BR /&gt;&lt;BR /&gt;HTH&lt;BR /&gt;&lt;BR /&gt;Duncan&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Feb 2007 10:50:47 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948427#M95027</guid>
      <dc:creator>Duncan Edmonstone</dc:creator>
      <dc:date>2007-02-21T10:50:47Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948428#M95028</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;my additional recommendations:&lt;BR /&gt;&lt;BR /&gt;1) Use default values for your data, but setup an option scanner (via getopts !) do modify these defaults: so you do not have to change the code, but just supply an option for such changes.&lt;BR /&gt;&lt;BR /&gt;2) Implement a usage output for your scripts, which can be understood by its users.&lt;BR /&gt;I use a special comment tag for my usage output; here is my template for this:&lt;BR /&gt;&lt;BR /&gt;#!/usr/bin/ksh&lt;BR /&gt;#@@&lt;BR /&gt;#@@ start_snav&lt;BR /&gt;#@@ initializing and starting SourceNavigator &lt;BR /&gt;#@@&lt;BR /&gt;#@@ options:&lt;BR /&gt;#@@     -f      start with a really new configuration&lt;BR /&gt;#@@     -h      help&lt;BR /&gt;#@@     -i      don't start the sourcenavigator in the end&lt;BR /&gt;...snipped...&lt;BR /&gt;&lt;BR /&gt;usage () { sed -e '/^#@@/!d' -e 's/^#@@//' $0&lt;BR /&gt;exit ${1:-1}&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;while getopts :kKifp:hc c&lt;BR /&gt;do&lt;BR /&gt;    case $c in&lt;BR /&gt;    i) sn_start=n;;&lt;BR /&gt;    p) sn_proj=${OPTARG%%.proj} opt_p=y;;&lt;BR /&gt;...snipped...&lt;BR /&gt;    ?) usage;;&lt;BR /&gt;    esac&lt;BR /&gt;done&lt;BR /&gt;shift $((OPTIND-1))&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;The corresponding usage output would read as&lt;BR /&gt;&lt;BR /&gt; start_snav&lt;BR /&gt; initializing and starting SourceNavigator &lt;BR /&gt;&lt;BR /&gt; options:&lt;BR /&gt;        -f      start with a really new configuration&lt;BR /&gt;        -h      help&lt;BR /&gt;        -i      don't start the sourcenavigator in the end&lt;BR /&gt;        -k      keep_ref: keep all cross reference files&lt;BR /&gt;        -K      keep_all: don't check the environment and keep all refs&lt;BR /&gt;        -c      generate a current versioninfo in proj.ccs&lt;BR /&gt;        -p proj initialization with different project names&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Wed, 21 Feb 2007 10:55:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948428#M95028</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2007-02-21T10:55:07Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948429#M95029</link>
      <description>Hi Hunki:&lt;BR /&gt;&lt;BR /&gt;What a shame that you have closed this thread!  There are more contributions with excellent insights still coming!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 21 Feb 2007 11:00:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948429#M95029</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-02-21T11:00:40Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948430#M95030</link>
      <description>Its reopened on popular demand !</description>
      <pubDate>Wed, 21 Feb 2007 15:04:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948430#M95030</guid>
      <dc:creator>Hunki</dc:creator>
      <dc:date>2007-02-21T15:04:32Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948431#M95031</link>
      <description>&lt;!--!*#--&gt;Hi Hunki:&lt;BR /&gt;&lt;BR /&gt;To add to Duncan's comments, in my opinion, there are better ways to define common environmental variables than by directly declaring them in the user's profile.&lt;BR /&gt;&lt;BR /&gt;Create a separate file of application variables that you need and source (read) &lt;BR /&gt;that file when/whereever you need to do so.&lt;BR /&gt;&lt;BR /&gt;This isolates common variables and constants in one place; simplfies maintenance;  increases reusability and avoids replication errors if/as changes are made.&lt;BR /&gt;&lt;BR /&gt;For the purposes of this discussion, maintaining a discrete file devoted to&lt;BR /&gt;environmental variable declaration, eliminates explicitly sourcing login profiles (as commonly done in 'crontabs') or implicitly sourcing them when running 'su - &lt;USER&gt; -c &lt;CMD&gt;' merely to obtain a variables and their values.  &lt;BR /&gt;&lt;BR /&gt;To 'source' a file, specify a dot character; a space; and the name of the file to source.&lt;BR /&gt;&lt;BR /&gt;This technique can be used as the last operation in a login profile as well as within applicaton scripts during their startup logic.&lt;BR /&gt;&lt;BR /&gt;If you elect, instead, to maintain your application specific variable within the&lt;BR /&gt;standard login profile, you can avoid the "not a typewriter" error messages by&lt;BR /&gt;encapsulating terminal queries involving 'stty' or 'tset' with:&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;if [ -t 0 ]; then&lt;BR /&gt;   stty ...&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;This logic tests to see if STDIN (file descriptor 0) is associated with a terminal.  If it is, the process is interacive and can interact with a terminal.  If the test fails, there is no associated terminal (perhaps because you 'cron'ed a process and sourced the '.profile' to gain your environmental varibles) and the meaningless 'stty' and 'tset' operations are skipped.&lt;BR /&gt;&lt;BR /&gt;By the way, in the shell, if you want to declare a constant and keep it constant (!), declare it readonly:&lt;BR /&gt;&lt;BR /&gt;# readonly PI=3.14159&lt;BR /&gt;# export PI&lt;BR /&gt;&lt;BR /&gt;Within a POSIX shell script, you can use 'typeset -r' to accomplish the same task, too.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF... &lt;BR /&gt;&lt;/CMD&gt;&lt;/USER&gt;</description>
      <pubDate>Wed, 21 Feb 2007 15:27:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948431#M95031</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2007-02-21T15:27:29Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948432#M95032</link>
      <description>Hunki,&lt;BR /&gt;&lt;BR /&gt; My style of scripting&lt;BR /&gt;&lt;BR /&gt;* No Hard-coding !!, as much as possible&lt;BR /&gt;&lt;BR /&gt;* Repeatitive codes are to be converted in to function !&lt;BR /&gt;&lt;BR /&gt;* If the action is the same but the input differs, you know looping is the way to go ! I have seen people repeat the same code for different set of inputs over and over. This will stretch a 20 line script in to 100 line one &lt;BR /&gt;&lt;BR /&gt;* Think in long-term. This is especially important for scripts that deals with some dynamic paramters, like the filesystem/logical volume names, volume group names etc to name a few.&lt;BR /&gt;&lt;BR /&gt;  When new FSes or volume groups added, we expect the exisiting scripts to handle it just fine without having to rewrite them all over.&lt;BR /&gt;&lt;BR /&gt;* Run the scripts as a user with bare-minimum previleges. I have seen Oracle hot/cold backup scripts run as root, when I dont see any reason why it cannot be run as the oracle user. &lt;BR /&gt;  &lt;BR /&gt;  It may just work fine except when the output filesystem is unavailable. If run as root, it is going to fill up the parent filesystem but whereas the job will fail if it is run as a normal user.&lt;BR /&gt;&lt;BR /&gt;* Good revision control always helps. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 21 Feb 2007 16:30:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948432#M95032</guid>
      <dc:creator>Sundar_7</dc:creator>
      <dc:date>2007-02-21T16:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948433#M95033</link>
      <description>At a minimum, always set (or unset, as appropriate) your umask, PATH, SHLIB_PATH, and LD_LIBRARY_PATH.&lt;BR /&gt;&lt;BR /&gt;Don't use a fixed or predictable filename in /tmp or /var/tmp (or any other world-writable directories). Use mktemp, or at least create the filename with $$.&lt;BR /&gt;&lt;BR /&gt;For scripts that run as root, don't use /tmp or /var/tmp at all. root shouldn't write to files in directories that other users can write to.&lt;BR /&gt;&lt;BR /&gt;Use sudo, not su, even if you end up running "sudo -u somebody /usr/bin/sh -c 'blah'". sudo doesn't use the shell in /etc/passwd, so an app account doesn't even need a valid shell for the command to run.&lt;BR /&gt;&lt;BR /&gt;Quote everything. Use single and double quotes in the appropriate places to make sure you know what is and isn't subject to variable interpolation.&lt;BR /&gt;&lt;BR /&gt;Indent your code in a consistent manner. I won't argue styles because nobody can ever agree, just make sure you're consistent. You'll appreciate 4-8 space indents or tabs if you ever have to read it at 3:00am.&lt;BR /&gt;&lt;BR /&gt;Try to stick to POSIX-standard stuff. That will make it easier if you move to a new platform or OS release.&lt;BR /&gt;&lt;BR /&gt;If you start something that runs in the background (either a daemon or something you background with &amp;amp;) make sure you "cd /" first. If you background it with "&amp;amp;" you should probably nohup it too, and always redirect stdout and stderr to files or /dev/null.</description>
      <pubDate>Wed, 21 Feb 2007 19:35:23 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948433#M95033</guid>
      <dc:creator>Heironimus</dc:creator>
      <dc:date>2007-02-21T19:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948434#M95034</link>
      <description>for any language and shell scripting the rule is to check your input and error codes of commands before proceding to do something.&lt;BR /&gt;&lt;BR /&gt;granted, this will in some cases increase the size of your script to something much larger then you first though of, but it is worth it.&lt;BR /&gt;</description>
      <pubDate>Thu, 22 Feb 2007 01:58:59 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948434#M95034</guid>
      <dc:creator>dirk dierickx</dc:creator>
      <dc:date>2007-02-22T01:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948435#M95035</link>
      <description>So, along the lines of checking input and return codes, here's something that I've been struggling with for a long time.  What are recommendations on checking on the return codes of piped commands.&lt;BR /&gt;&lt;BR /&gt;E.G.&lt;BR /&gt;echo "hi there" | awk '{print $1}' | sed 's/h/H/g'&lt;BR /&gt;&lt;BR /&gt;What happens if awk craps out?  If you're going to some sort of check on the output of the pipe and it's now an empty string because awk didn't forward anything on, it could muck things up quite nicely, couldn't it?&lt;BR /&gt;&lt;BR /&gt;For the longest while, I've tried doing things like:&lt;BR /&gt;&lt;BR /&gt;( echo "hi there" ; echo $? &amp;gt; $ERRFILE ) |&lt;BR /&gt;( awk '{print $1}' ; echo $? &amp;gt;&amp;gt; $ERRFILE ) |&lt;BR /&gt;( sed 's/h/H/g' ; echo $? &amp;gt;&amp;gt; $ERRFILE )&lt;BR /&gt;&lt;BR /&gt;after which I check $ERRFILE for error return codes.  However, I've run into issues where $ERRFILE isn't necessarily being written to in the sequence of the pipes.</description>
      <pubDate>Fri, 09 Mar 2007 12:08:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948435#M95035</guid>
      <dc:creator>Kevin Nikiforuk</dc:creator>
      <dc:date>2007-03-09T12:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948436#M95036</link>
      <description>do a man on whichever shell your using&lt;BR /&gt;&lt;BR /&gt;in posix shell&lt;BR /&gt;&lt;BR /&gt;a || b &lt;BR /&gt;&lt;BR /&gt;executes b only if a returned zero.  there are other pipe operators depending upon what you want to accomplish</description>
      <pubDate>Fri, 09 Mar 2007 12:43:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948436#M95036</guid>
      <dc:creator>OldSchool</dc:creator>
      <dc:date>2007-03-09T12:43:19Z</dc:date>
    </item>
    <item>
      <title>Re: Unix Shell Scripting - Best Practises</title>
      <link>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948437#M95037</link>
      <description>But I want to pipe a's result into b.&lt;BR /&gt;&lt;BR /&gt;a || b will only execute a or b.</description>
      <pubDate>Fri, 09 Mar 2007 15:10:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/unix-shell-scripting-best-practises/m-p/3948437#M95037</guid>
      <dc:creator>Kevin Nikiforuk</dc:creator>
      <dc:date>2007-03-09T15:10:18Z</dc:date>
    </item>
  </channel>
</rss>

