<?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: exit in script! in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624714#M104804</link>
    <description>set -x is used to enable shell tracing mode. In shell tracing mode each command is printed in the exact form that it is executed. &lt;BR /&gt;&lt;BR /&gt;This can be used to debug shell scripts.</description>
    <pubDate>Wed, 14 Sep 2005 08:36:54 GMT</pubDate>
    <dc:creator>Orhan Biyiklioglu</dc:creator>
    <dc:date>2005-09-14T08:36:54Z</dc:date>
    <item>
      <title>exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624704#M104794</link>
      <description>I have a question about the exit in the script.&lt;BR /&gt;The idea is that the script stop when the tar command fails. so that the files are not removed. did I do it correct?&lt;BR /&gt;&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;&lt;BR /&gt;set -v&lt;BR /&gt;cd /cdr1/pmc&lt;BR /&gt;#&lt;BR /&gt;filename=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.tar&lt;BR /&gt;indexfile=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.txt&lt;BR /&gt;&lt;BR /&gt;ls -l /cdr1/pmc &amp;gt;$indexfile&lt;BR /&gt;&lt;BR /&gt;mkdir -p /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;for file in $(ls *.BF); do&lt;BR /&gt;mv ${file} /cdr1/backup-tijd&lt;BR /&gt;done &lt;BR /&gt;&lt;BR /&gt;cd /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;tar cvf $filename .&lt;BR /&gt; if [ $? -ne 0 ];&lt;BR /&gt; then&lt;BR /&gt; echo "tar failed"&lt;BR /&gt; exit &lt;BR /&gt; else&lt;BR /&gt; rm /cdr1/backup-tijd/*&lt;BR /&gt; rmdir /cdr1/backup-tijd/&lt;BR /&gt;fi &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;        gzip $filename&lt;BR /&gt; if [ $? -ne 0 ];&lt;BR /&gt; then &lt;BR /&gt; echo  "gzip-failed"&lt;BR /&gt; exit &lt;BR /&gt; else&lt;BR /&gt; touch gzip-ok&lt;BR /&gt;fi&lt;BR /&gt;exit&lt;BR /&gt;</description>
      <pubDate>Tue, 13 Sep 2005 08:20:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624704#M104794</guid>
      <dc:creator>mick001</dc:creator>
      <dc:date>2005-09-13T08:20:50Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624705#M104795</link>
      <description>Looks OK to me.  You could also test it by inserting a simple command like "ll" in place of the tar.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Pete</description>
      <pubDate>Tue, 13 Sep 2005 08:29:15 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624705#M104795</guid>
      <dc:creator>Pete Randall</dc:creator>
      <dc:date>2005-09-13T08:29:15Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624706#M104796</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;seems good to me !&lt;BR /&gt;&lt;BR /&gt;regards&lt;BR /&gt;pg&lt;BR /&gt;</description>
      <pubDate>Tue, 13 Sep 2005 08:34:42 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624706#M104796</guid>
      <dc:creator>Piergiacomo Perini</dc:creator>
      <dc:date>2005-09-13T08:34:42Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624707#M104797</link>
      <description>You can also give you exit codes.&lt;BR /&gt;&lt;BR /&gt;Like exit 3, exit 4, etc.&lt;BR /&gt;This will help you trace later on, as to from where has the code exited.</description>
      <pubDate>Tue, 13 Sep 2005 08:36:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624707#M104797</guid>
      <dc:creator>Vibhor Kumar Agarwal</dc:creator>
      <dc:date>2005-09-13T08:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624708#M104798</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Your handling of the return status from 'tar' and 'gzip' are fine.&lt;BR /&gt;&lt;BR /&gt;I'll offer one unrelated suggestion.&lt;BR /&gt;&lt;BR /&gt;The remove of the contents of the directory and then the directory itself can be coded with a recursive remove to handle both.  More importantly, the use of the "*" wildcard which the shell will expand into a list of all of the files in the directory, could lead to an "argument list too long" error.  [Of course, I'll grant that that was more a problem on older OS versions than newer ones where much larger argument space can be configured; but nevertheless, there is better form].&lt;BR /&gt;&lt;BR /&gt;Thus, to remove your directory and its contents, simply do:&lt;BR /&gt;&lt;BR /&gt;# cd / &lt;BR /&gt;# rm -r rm /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 13 Sep 2005 09:00:46 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624708#M104798</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-09-13T09:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624709#M104799</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;Oops!!! That shoud read:&lt;BR /&gt;&lt;BR /&gt;# cd / &lt;BR /&gt;# rm -r /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 13 Sep 2005 09:02:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624709#M104799</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-09-13T09:02:52Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624710#M104800</link>
      <description>The most significant change I would suggest is to always save ${?} to another variable so that subsequent operations do not alter it. This becomes especially useful when printing error messages. Good practice requires that error messages generally be sent to stderr rather than stdout and that you exit with a non-zero status on failures and zero if okay.&lt;BR /&gt;&lt;BR /&gt;I would make changes similar to this:&lt;BR /&gt;&lt;BR /&gt;typeset -i STAT=0&lt;BR /&gt;gzip ${filename}&lt;BR /&gt;STAT=${?} # save status in another variable&lt;BR /&gt;if [[ ${STAT} -ne 0 ]]&lt;BR /&gt;then &lt;BR /&gt;echo "gzip-failed; status ${STAT}." &amp;gt;&amp;amp;2&lt;BR /&gt;exit ${STAT}&lt;BR /&gt;else&lt;BR /&gt;touch gzip-ok&lt;BR /&gt;fi&lt;BR /&gt;exit ${STAT} &lt;BR /&gt;</description>
      <pubDate>Tue, 13 Sep 2005 09:58:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624710#M104800</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2005-09-13T09:58:12Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624711#M104801</link>
      <description>Regarding deleting recursively, just a small enhancement:&lt;BR /&gt;&lt;BR /&gt;rm -rf /directory&lt;BR /&gt;&lt;BR /&gt;This will help if the file is read only.</description>
      <pubDate>Wed, 14 Sep 2005 05:45:41 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624711#M104801</guid>
      <dc:creator>Vibhor Kumar Agarwal</dc:creator>
      <dc:date>2005-09-14T05:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624712#M104802</link>
      <description>Use this script.&lt;BR /&gt;&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;# Exit Status&lt;BR /&gt;# 0 - Success; 1 - Tar failed;2 - gunzip failed&lt;BR /&gt;set -x&lt;BR /&gt;cd /cdr1/pmc&lt;BR /&gt;&lt;BR /&gt;filename=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.tar&lt;BR /&gt;indexfile=/cdr1/backup.cdr.`date '+%d.%m.%Y.%k.%M.%S'`.txt&lt;BR /&gt;&lt;BR /&gt;ls -l /cdr1/pmc &amp;gt;$indexfile&lt;BR /&gt;&lt;BR /&gt;mkdir -p /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;for file in $(ls *.BF); do&lt;BR /&gt;  mv -f ${file} /cdr1/backup-tijd&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;cd /cdr1/backup-tijd&lt;BR /&gt;&lt;BR /&gt;tar cvf $filename .&lt;BR /&gt;if [ $? -ne 0 ];&lt;BR /&gt;then&lt;BR /&gt; echo "tar failed"&lt;BR /&gt; exit 1&lt;BR /&gt;else&lt;BR /&gt; rm -rf /cdr1/backup-tijd/&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;gzip $filename&lt;BR /&gt;if [ $? -ne 0 ];&lt;BR /&gt;then&lt;BR /&gt;  echo "gzip-failed"&lt;BR /&gt;  exit 2&lt;BR /&gt;else&lt;BR /&gt;  echo "gunzip-ok"&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;exit 0&lt;BR /&gt;set +x&lt;BR /&gt;&lt;BR /&gt;hth.&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Sep 2005 05:53:16 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624712#M104802</guid>
      <dc:creator>Muthukumar_5</dc:creator>
      <dc:date>2005-09-14T05:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624713#M104803</link>
      <description>Hi &lt;BR /&gt;&lt;BR /&gt;Why do you use set -x ?&lt;BR /&gt;&lt;BR /&gt;Thanks</description>
      <pubDate>Wed, 14 Sep 2005 08:30:50 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624713#M104803</guid>
      <dc:creator>mick001</dc:creator>
      <dc:date>2005-09-14T08:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624714#M104804</link>
      <description>set -x is used to enable shell tracing mode. In shell tracing mode each command is printed in the exact form that it is executed. &lt;BR /&gt;&lt;BR /&gt;This can be used to debug shell scripts.</description>
      <pubDate>Wed, 14 Sep 2005 08:36:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624714#M104804</guid>
      <dc:creator>Orhan Biyiklioglu</dc:creator>
      <dc:date>2005-09-14T08:36:54Z</dc:date>
    </item>
    <item>
      <title>Re: exit in script!</title>
      <link>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624715#M104805</link>
      <description>Hi:&lt;BR /&gt;&lt;BR /&gt;'set -x' causes a script's commands and their arguments to be printed as they are executed.  This is useful to find what and where a script is doing; a useful debuging tool.&lt;BR /&gt;&lt;BR /&gt;Another one is '-n'.  This means syntax-check but don't actually execute.&lt;BR /&gt;&lt;BR /&gt;Consult the manpages for 'sh-posix' for a full list of the other very useful shell options.&lt;BR /&gt;&lt;BR /&gt;You can always do this:&lt;BR /&gt;&lt;BR /&gt;# sh -x yourscript&lt;BR /&gt;&lt;BR /&gt;That is, you do NOT need to actually modify a script to temporarily leverage a set option.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Sep 2005 08:45:00 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/exit-in-script/m-p/3624715#M104805</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2005-09-14T08:45:00Z</dc:date>
    </item>
  </channel>
</rss>

