<?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 While loop in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/while-loop/m-p/3332833#M13231</link>
    <description>To give a short description what i am doing in the script is reading from a regular text file for the directory structure and changing it to another format and creating the structure, but the problem is once it reads a line from the text file it fails on the second line, I am stuck with the while loop any suggestions please.&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;The REQFILE has&lt;BR /&gt;/RIP/module/resources/com/res/app/email/htmlConfirmationEmailHP.txt&lt;BR /&gt;RIP/module/resources/com/crm/client/UserMessages.properties&lt;BR /&gt;RIP/module/config/html/BrandDetectorServlet.properties&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Script:&lt;BR /&gt;#!/bin/ksh&lt;BR /&gt;STAGEDIR=/home/user/Patch/build_patch/lists&lt;BR /&gt;SCRIPTDIR=/home/user/Patch&lt;BR /&gt;&lt;BR /&gt;REQFILE=rip_files.txt&lt;BR /&gt;START=(What should I put)&lt;BR /&gt;END=`cat $REQFILE|wc -l`&lt;BR /&gt;echo $END&lt;BR /&gt;while [ $START -lt $END ]&lt;BR /&gt;do&lt;BR /&gt;for FLPATHNM in `cat $REQFILE`&lt;BR /&gt;do&lt;BR /&gt;cd $STAGEDIR&lt;BR /&gt;&lt;BR /&gt;       FSTCHR=`echo $FLPATHNM|awk '{print substr($0,1,1)}'`&lt;BR /&gt;       LSTCHR=`echo $FLPATHNM|awk '{print substr($0,length($0),1)}'`&lt;BR /&gt;       if [ $FSTCHR = "/" ]&lt;BR /&gt;       then FLPATHNM1=`echo $FLPATHNM|awk '{print substr($0,2,length($0))}'`&lt;BR /&gt;       else FLPATHNM1=`echo $FLPATHNM`&lt;BR /&gt;       fi&lt;BR /&gt;       if [ $LSTCHR = "/" ]&lt;BR /&gt;       then TFLPATHNM2=`echo $FLPATHNM1|awk '{print substr($0,1,length($0)-1)}'`&lt;BR /&gt;       else TFLPATHNM2=`echo $FLPATHNM1`&lt;BR /&gt;&lt;BR /&gt;       fi&lt;BR /&gt;        TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/resources/temp\/RIP\/lib\/resourcepatches/'`&lt;BR /&gt;TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/config/temp\/RIP\/lib\/configpatches/'`&lt;BR /&gt;&lt;BR /&gt;FLPATHNM2=$TFLPATHNM4&lt;BR /&gt; LENPATH=`echo $FLPATHNM2|awk '{print length()}'`&lt;BR /&gt;  SLSHFLG=0&lt;BR /&gt;  SLSHPOS=X&lt;BR /&gt;    while [ $SLSHFLG -ne 1 ] &amp;amp;&amp;amp; [ $LENPATH -gt 0 ]&lt;BR /&gt;    do&lt;BR /&gt;CHRLKCMP=`echo $FLPATHNM2|awk '{print substr($0,'$LENPATH',1)}'`&lt;BR /&gt;    if [ $CHRLKCMP = "/" ]&lt;BR /&gt;    then SLSHFLG=1&lt;BR /&gt;         SLSHPOS=$LENPATH&lt;BR /&gt;    fi&lt;BR /&gt;    ((LENPATH = LENPATH - 1))&lt;BR /&gt;    done&lt;BR /&gt;    ((SLSHPOS = SLSHPOS + 1))&lt;BR /&gt;    CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`&lt;BR /&gt;    FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`&lt;BR /&gt;  FSTCHR=`echo $FLPATHNM2|awk '{print substr($0,1,1)}'`&lt;BR /&gt;  if [ $FSTCHR = "/" ]&lt;BR /&gt;  then FILENM=`echo $FLPATHNM2|awk '{print substr($0,2)}'`&lt;BR /&gt;  else FILENM=$FLPATHNM2&lt;BR /&gt;  fi&lt;BR /&gt;  LENPATH=`echo $FILENM|awk '{print length()}'`&lt;BR /&gt;  SLSHFLG=0&lt;BR /&gt;  SLSHPOS=X&lt;BR /&gt;    while [ $SLSHFLG -ne 1 ] &amp;amp;&amp;amp; [ $LENPATH -gt 0 ]&lt;BR /&gt;    do&lt;BR /&gt;    CHRLKCMP=`echo $FILENM|awk '{print substr($0,'$LENPATH',1)}'`&lt;BR /&gt;    if [ $CHRLKCMP = "/" ]&lt;BR /&gt;    then SLSHFLG=1&lt;BR /&gt;         SLSHPOS=$LENPATH&lt;BR /&gt;    fi&lt;BR /&gt;    ((LENPATH = LENPATH - 1))&lt;BR /&gt;    done&lt;BR /&gt;    if [ $SLSHPOS = X ]&lt;BR /&gt;    then echo "ERROR: "$FILENM": Path Specified Is Incorrect!"&lt;BR /&gt;         continue&lt;BR /&gt;    fi&lt;BR /&gt;    ((SLSHPOS = SLSHPOS + 1))&lt;BR /&gt;cd /home/user/Patch&lt;BR /&gt;    CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`&lt;BR /&gt;echo $CDPATH&lt;BR /&gt;    FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`&lt;BR /&gt;  if [ -a $SCRIPTDIR/$CDPATH ]&lt;BR /&gt;  then cd $SCRIPTDIR/$CDPATH&lt;BR /&gt;       cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH&lt;BR /&gt;  else mkdir -p $CDPATH&lt;BR /&gt;       cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH&lt;BR /&gt;       continue&lt;BR /&gt;  fi&lt;BR /&gt;  echo "-&amp;gt; File: "$FILENM&lt;BR /&gt;done&lt;BR /&gt;done(end while loop)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Thu, 15 Jul 2004 15:12:58 GMT</pubDate>
    <dc:creator>Vanquish</dc:creator>
    <dc:date>2004-07-15T15:12:58Z</dc:date>
    <item>
      <title>While loop</title>
      <link>https://community.hpe.com/t5/operating-system-linux/while-loop/m-p/3332833#M13231</link>
      <description>To give a short description what i am doing in the script is reading from a regular text file for the directory structure and changing it to another format and creating the structure, but the problem is once it reads a line from the text file it fails on the second line, I am stuck with the while loop any suggestions please.&lt;BR /&gt;Thanks&lt;BR /&gt;&lt;BR /&gt;The REQFILE has&lt;BR /&gt;/RIP/module/resources/com/res/app/email/htmlConfirmationEmailHP.txt&lt;BR /&gt;RIP/module/resources/com/crm/client/UserMessages.properties&lt;BR /&gt;RIP/module/config/html/BrandDetectorServlet.properties&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Script:&lt;BR /&gt;#!/bin/ksh&lt;BR /&gt;STAGEDIR=/home/user/Patch/build_patch/lists&lt;BR /&gt;SCRIPTDIR=/home/user/Patch&lt;BR /&gt;&lt;BR /&gt;REQFILE=rip_files.txt&lt;BR /&gt;START=(What should I put)&lt;BR /&gt;END=`cat $REQFILE|wc -l`&lt;BR /&gt;echo $END&lt;BR /&gt;while [ $START -lt $END ]&lt;BR /&gt;do&lt;BR /&gt;for FLPATHNM in `cat $REQFILE`&lt;BR /&gt;do&lt;BR /&gt;cd $STAGEDIR&lt;BR /&gt;&lt;BR /&gt;       FSTCHR=`echo $FLPATHNM|awk '{print substr($0,1,1)}'`&lt;BR /&gt;       LSTCHR=`echo $FLPATHNM|awk '{print substr($0,length($0),1)}'`&lt;BR /&gt;       if [ $FSTCHR = "/" ]&lt;BR /&gt;       then FLPATHNM1=`echo $FLPATHNM|awk '{print substr($0,2,length($0))}'`&lt;BR /&gt;       else FLPATHNM1=`echo $FLPATHNM`&lt;BR /&gt;       fi&lt;BR /&gt;       if [ $LSTCHR = "/" ]&lt;BR /&gt;       then TFLPATHNM2=`echo $FLPATHNM1|awk '{print substr($0,1,length($0)-1)}'`&lt;BR /&gt;       else TFLPATHNM2=`echo $FLPATHNM1`&lt;BR /&gt;&lt;BR /&gt;       fi&lt;BR /&gt;        TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/resources/temp\/RIP\/lib\/resourcepatches/'`&lt;BR /&gt;TFLPATHNM4=`echo $TFLPATHNM2|sed 's/RIP\/module\/config/temp\/RIP\/lib\/configpatches/'`&lt;BR /&gt;&lt;BR /&gt;FLPATHNM2=$TFLPATHNM4&lt;BR /&gt; LENPATH=`echo $FLPATHNM2|awk '{print length()}'`&lt;BR /&gt;  SLSHFLG=0&lt;BR /&gt;  SLSHPOS=X&lt;BR /&gt;    while [ $SLSHFLG -ne 1 ] &amp;amp;&amp;amp; [ $LENPATH -gt 0 ]&lt;BR /&gt;    do&lt;BR /&gt;CHRLKCMP=`echo $FLPATHNM2|awk '{print substr($0,'$LENPATH',1)}'`&lt;BR /&gt;    if [ $CHRLKCMP = "/" ]&lt;BR /&gt;    then SLSHFLG=1&lt;BR /&gt;         SLSHPOS=$LENPATH&lt;BR /&gt;    fi&lt;BR /&gt;    ((LENPATH = LENPATH - 1))&lt;BR /&gt;    done&lt;BR /&gt;    ((SLSHPOS = SLSHPOS + 1))&lt;BR /&gt;    CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`&lt;BR /&gt;    FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`&lt;BR /&gt;  FSTCHR=`echo $FLPATHNM2|awk '{print substr($0,1,1)}'`&lt;BR /&gt;  if [ $FSTCHR = "/" ]&lt;BR /&gt;  then FILENM=`echo $FLPATHNM2|awk '{print substr($0,2)}'`&lt;BR /&gt;  else FILENM=$FLPATHNM2&lt;BR /&gt;  fi&lt;BR /&gt;  LENPATH=`echo $FILENM|awk '{print length()}'`&lt;BR /&gt;  SLSHFLG=0&lt;BR /&gt;  SLSHPOS=X&lt;BR /&gt;    while [ $SLSHFLG -ne 1 ] &amp;amp;&amp;amp; [ $LENPATH -gt 0 ]&lt;BR /&gt;    do&lt;BR /&gt;    CHRLKCMP=`echo $FILENM|awk '{print substr($0,'$LENPATH',1)}'`&lt;BR /&gt;    if [ $CHRLKCMP = "/" ]&lt;BR /&gt;    then SLSHFLG=1&lt;BR /&gt;         SLSHPOS=$LENPATH&lt;BR /&gt;    fi&lt;BR /&gt;    ((LENPATH = LENPATH - 1))&lt;BR /&gt;    done&lt;BR /&gt;    if [ $SLSHPOS = X ]&lt;BR /&gt;    then echo "ERROR: "$FILENM": Path Specified Is Incorrect!"&lt;BR /&gt;         continue&lt;BR /&gt;    fi&lt;BR /&gt;    ((SLSHPOS = SLSHPOS + 1))&lt;BR /&gt;cd /home/user/Patch&lt;BR /&gt;    CDPATH=`echo $FILENM|awk '{print substr($0,1,'$LENPATH')}'`&lt;BR /&gt;echo $CDPATH&lt;BR /&gt;    FILENM=`echo $FILENM|awk '{print substr($0,'$SLSHPOS',length())}'`&lt;BR /&gt;  if [ -a $SCRIPTDIR/$CDPATH ]&lt;BR /&gt;  then cd $SCRIPTDIR/$CDPATH&lt;BR /&gt;       cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH&lt;BR /&gt;  else mkdir -p $CDPATH&lt;BR /&gt;       cp $MODULEDIR/$TFLPATHNM2 $SCRIPTDIR/$CDPATH&lt;BR /&gt;       continue&lt;BR /&gt;  fi&lt;BR /&gt;  echo "-&amp;gt; File: "$FILENM&lt;BR /&gt;done&lt;BR /&gt;done(end while loop)&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Jul 2004 15:12:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/while-loop/m-p/3332833#M13231</guid>
      <dc:creator>Vanquish</dc:creator>
      <dc:date>2004-07-15T15:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: While loop</title>
      <link>https://community.hpe.com/t5/operating-system-linux/while-loop/m-p/3332834#M13232</link>
      <description>Ok, so let me get this straight.&lt;BR /&gt;&lt;BR /&gt;You've got a 'REQFILE' (rip_files.txt) with multiple lines in it, each with a file or directory name listed.&lt;BR /&gt;&lt;BR /&gt;You then want to create a structure somwehere which has all of these files and directories..&lt;BR /&gt;&lt;BR /&gt;You're over-complicating matters here, searching for /'s, instead of using shell commands (basename, dirname) to get this sort of info.&lt;BR /&gt;&lt;BR /&gt;You're also shelling off to awk whenever you want to do any text manipulation commands instead of using shell builtins (${VAR#..}, ${VAR%..}, ${#VAR}).&lt;BR /&gt;&lt;BR /&gt;Your core while loop currently doesn't break out *ever* (as you never increase or decrease either 'START' or 'END').&lt;BR /&gt;&lt;BR /&gt;Lets see if we can shorten this..&lt;BR /&gt;&lt;BR /&gt;#!/bin/ksh&lt;BR /&gt;STAGEDIR=/home/user/Patch/build_patch/lists&lt;BR /&gt;&lt;BR /&gt;SCRIPTDIR=/home/user/Patch&lt;BR /&gt;MODULEDIR=/some/path&lt;BR /&gt;&lt;BR /&gt;REQFILE=rip_files.txt&lt;BR /&gt;&lt;BR /&gt;while read FILE_PATH&lt;BR /&gt;do&lt;BR /&gt;FILE_PATH=$(echo ${FILE_PATH}|sed 's#RIP/module/resources#temp/RIP/lib/resourcepatches#')&lt;BR /&gt;  FILE_PATH=$(echo ${FILE_PATH}|sed 's#RIP/module/config#temp/RIP/lib/configpatches#')&lt;BR /&gt;&lt;BR /&gt; CDPATH=$(dirname ${FILE_PATH})&lt;BR /&gt; FILENM=$(basename ${FILE_PATH})&lt;BR /&gt;&lt;BR /&gt; if [ ! -a ${SCRIPTDIR}/${CDPATH} ]&lt;BR /&gt; then&lt;BR /&gt;  mkdir -p ${SCRIPTPATH}/${CDPATH}&lt;BR /&gt; fi&lt;BR /&gt; cp ${MODULEDIR}/${FILE_PATH} ${SCRIPTDIR}/${CDPATH}&lt;BR /&gt;done &amp;lt; ${REQFILE}&lt;BR /&gt;&lt;BR /&gt;Discussion:&lt;BR /&gt;&lt;BR /&gt;Shells's are very robust.  Doing things like:&lt;BR /&gt;&lt;BR /&gt;cp /file//to/path/name /another/path//with/slahes&lt;BR /&gt;&lt;BR /&gt;works just fine.  The shell understands that it's users are idiots and treats the //'s as a single /.&lt;BR /&gt;&lt;BR /&gt;That being said, the simple in-built:&lt;BR /&gt;&lt;BR /&gt;${CDPATH#/}&lt;BR /&gt;&lt;BR /&gt;would remove any leading slash from the directory names.&lt;BR /&gt;&lt;BR /&gt;The while loop taking input from the file (the 'done &amp;lt; ${REQFILE}') is a valid way to read a list of lines from a file.&lt;BR /&gt;&lt;BR /&gt;The work of finding out the directory-name and file-name can be left to the shell utilities 'dirname' and 'basename' respectivly.&lt;BR /&gt;&lt;BR /&gt;The sed commands.  Translating the path mid-stream is nice.  'sed' is a wonderful tool that allows you to replace the separator with a more natural character.  As seen above, the normal '/' character has been replaced with a '#' for the separator.  This makes it much easier to read and understand, causing less confusion when you come back to it.&lt;BR /&gt;&lt;BR /&gt;As you're going to be doing the copy regardless of whether the path exists or not, there's no point having the copy twice.  We do a negative-test for the path accessability, and create it if it's not.  You could do a check if the path failed to create at this point if you wanted by changing the 'mkdir -p' line to this:&lt;BR /&gt;&lt;BR /&gt;if ! mkdir -p ${SCRIPTPATH}/${CDPATH}&lt;BR /&gt;then&lt;BR /&gt;echo "Failed to create path for ${FILE_PATH}"&lt;BR /&gt;continue&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;Thus jumping out of this iteration of the loop.&lt;BR /&gt;&lt;BR /&gt;As you can do copies from one path to another, there's no need to change the directory at any point.  After defining 'MODULEDIR' and 'SCRIPTDIR', everything is ready to work regardless of your current-working-directory (or $PWD).&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 15 Jul 2004 19:11:58 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/while-loop/m-p/3332834#M13232</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2004-07-15T19:11:58Z</dc:date>
    </item>
  </channel>
</rss>

