<?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 String substitution is not working in shell script in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/string-substitution-is-not-working-in-shell-script/m-p/7127264#M495902</link>
    <description>&lt;P&gt;Dear concern,&lt;/P&gt;&lt;P&gt;I've write following script.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;bash-4.2$ cat truncate.sh
#!/bin/sh
LOGPATH="/wls_home/swig_atm/SwitchDbg"
ARCPATH="/wls_home/swig_atm/SwitchDbgArc"

for i in $LOGPATH/MSG*log
do
        if [ ! -e $i ]; then break ; fi
        #echo "For filename : $i"
        filename=`basename $i .log`
        dirname=`echo ${filename:4}`
        ofilename=`date +%H%M`
        #echo "target filename = $ARCPATH/$dirname/$dirname.$ofilename"
        if [ -e $ARCPATH/$dirname ];then
                mv $i "$ARCPATH/$dirname/$dirname.$ofilename"
        else
                mkdir -p $ARCPATH/$dirname
                mv $i "$ARCPATH/$dirname/$dirname.$ofilename"
        fi
done&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But&amp;nbsp;String substitution is not working in below arrow mentioned secton.&amp;nbsp; Please assist me,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;bash-4.2$ sh -x truncate.sh
+ LOGPATH=/wls_home/swig_atm/SwitchDbg
+ ARCPATH=/wls_home/swig_atm/SwitchDbgArc
+ [ ! -e /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log ]
+ + basename /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log .log
filename=MSG_23March2021
+ truncate.sh[10]: ${filename:4}: The specified substitution is not valid for this command.===========&amp;gt;String substitution is not working
dirname=
+ + date +%H%M
ofilename=1743
+ [ -e /wls_home/swig_atm/SwitchDbgArc/ ]
+ mv /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log /wls_home/swig_atm/SwitchDbgArc//.1743&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Mar 2021 12:07:37 GMT</pubDate>
    <dc:creator>Kauser</dc:creator>
    <dc:date>2021-03-25T12:07:37Z</dc:date>
    <item>
      <title>String substitution is not working in shell script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-substitution-is-not-working-in-shell-script/m-p/7127264#M495902</link>
      <description>&lt;P&gt;Dear concern,&lt;/P&gt;&lt;P&gt;I've write following script.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;bash-4.2$ cat truncate.sh
#!/bin/sh
LOGPATH="/wls_home/swig_atm/SwitchDbg"
ARCPATH="/wls_home/swig_atm/SwitchDbgArc"

for i in $LOGPATH/MSG*log
do
        if [ ! -e $i ]; then break ; fi
        #echo "For filename : $i"
        filename=`basename $i .log`
        dirname=`echo ${filename:4}`
        ofilename=`date +%H%M`
        #echo "target filename = $ARCPATH/$dirname/$dirname.$ofilename"
        if [ -e $ARCPATH/$dirname ];then
                mv $i "$ARCPATH/$dirname/$dirname.$ofilename"
        else
                mkdir -p $ARCPATH/$dirname
                mv $i "$ARCPATH/$dirname/$dirname.$ofilename"
        fi
done&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But&amp;nbsp;String substitution is not working in below arrow mentioned secton.&amp;nbsp; Please assist me,&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;bash-4.2$ sh -x truncate.sh
+ LOGPATH=/wls_home/swig_atm/SwitchDbg
+ ARCPATH=/wls_home/swig_atm/SwitchDbgArc
+ [ ! -e /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log ]
+ + basename /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log .log
filename=MSG_23March2021
+ truncate.sh[10]: ${filename:4}: The specified substitution is not valid for this command.===========&amp;gt;String substitution is not working
dirname=
+ + date +%H%M
ofilename=1743
+ [ -e /wls_home/swig_atm/SwitchDbgArc/ ]
+ mv /wls_home/swig_atm/SwitchDbg/MSG_23March2021.log /wls_home/swig_atm/SwitchDbgArc//.1743&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 12:07:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-substitution-is-not-working-in-shell-script/m-p/7127264#M495902</guid>
      <dc:creator>Kauser</dc:creator>
      <dc:date>2021-03-25T12:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: String substitution is not working in shell script</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/string-substitution-is-not-working-in-shell-script/m-p/7127313#M495903</link>
      <description>&lt;P&gt;&amp;gt; [...] String substitution is not working [...]&lt;/P&gt;&lt;P&gt;&amp;gt; bash-4.2$ [...]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; You may be using "bash" interactively, but your script uses a&lt;BR /&gt;different shell (with different features):&lt;/P&gt;&lt;P&gt;&amp;gt; #!/bin/sh&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; That kind of parameter substitution is not a feature of that shell.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; man sh-posix&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Look for "Parameter Substitution".&lt;/P&gt;&lt;P&gt;There are alternatives . For example:&lt;BR /&gt;&lt;BR /&gt;rux$ echo ${filename}&lt;BR /&gt;MSG_23March2021&lt;/P&gt;&lt;P&gt;rux$ echo ${filename#????}&lt;BR /&gt;23March2021&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Or, perhaps more generally:&lt;/P&gt;&lt;P&gt;rux$ echo ${filename#*_}&lt;BR /&gt;23March2021&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; As usual, many things are possible.&amp;nbsp; (But not everything works with&lt;BR /&gt;every shell.)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; Also, in:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dirname=`echo ${filename:4}`&lt;BR /&gt;what does&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; `echo `&lt;BR /&gt;buy you?&amp;nbsp; Why not?:&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; dirname=${filename#????}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; Alternatively, you could change your script to use "bash" (wherever&lt;BR /&gt;it is) instead of "/bin/sh", but I'd stick with "/bin/sh" for&lt;BR /&gt;portability/reliability.&lt;/P&gt;</description>
      <pubDate>Thu, 25 Mar 2021 21:57:24 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/string-substitution-is-not-working-in-shell-script/m-p/7127313#M495903</guid>
      <dc:creator>Steven Schweda</dc:creator>
      <dc:date>2021-03-25T21:57:24Z</dc:date>
    </item>
  </channel>
</rss>

