Prevent keyword expansion in Script shell

Background

The shell script creates several Java source files. The source files have a special comment for the header, which includes Subversion keywords. The goal is to check the shell of the script without changing the headers of the source files buried inside.

Problem

The shell script contains its own header, which should have a keyword extension:

#!/bin/bash
#
#  Revision Control Information
#  File:                $Id:: autogenerate.sh 3142 2016-08-26 18:50:21Z USERNA#$
#  Date of Last Commit: $Date:: 2016-08-26 11:50:21 -0700 (Fri, 26 Aug 2016)   $
#  Revision Number:     $Rev:: 3142                                            $
#  Last Commit by:      $Author:: USERNAME                                     $

This part works. Invalid part when comment is included later in shell script:

cat <<EOT >> $FILENAME_IMPL
/*
 * *********************************************************************
 *  Revision Control Information
 *  File:                $Id::                                         $
 *  Date of Last Commit: $Date::                                       $
 *  Revision Number:     $Rev::                                        $
 *  Last Commit by:      $Author::                                     $
 *
 * **********************************************************************
 */
package com.company.pkg;
EOT

script , ; Java . , , . .

, Java:

cat <<EOT >> $FILENAME_IMPL
/*
 * *********************************************************************
 *  Revision Control Information
 *  File:                $Id:: autogenerate.sh                                 $
 *  Date of Last Commit: $Date:: 2016-08-26 11:50:21 -0700 (Fri, 26 Aug 2016)  $
 *  Revision Number:     $Rev:: 1234                                           $
 *  Last Commit by:      $Author:: USERNAME                                    $
 *
 * **********************************************************************
 */
package com.company.pkg;
EOT

Java "autogenerate.sh", "ClassName.java".

, script, autogenerate.sh:

#!/bin/bash
#  File:                $Id:: $

FILENAME_IMPL=ClassName.java

cat <<EOT >> $FILENAME_IMPL
/* File:                $Id:: $
 */
package com.company.pkg;
EOT

script , :

#!/bin/bash
#  File:                $Id:: autogenerate.sh $

FILENAME_IMPL=ClassName.java

cat <<EOT >> $FILENAME_IMPL
/* File:                $Id:: autogenerate.sh $
 */
package com.company.pkg;
EOT

$Id:: . $Id:: . , script , :

#!/bin/bash
#  File:                $Id:: autogenerate.sh $

FILENAME_IMPL=ClassName.java

cat <<EOT >> $FILENAME_IMPL
/* File:                $Id:: $
 */
package com.company.pkg;
EOT

. :

cat <<EOT >> $FILENAME_IMPL
/* File:                \$Id:: $
 */
package com.company.pkg;
EOT

, , Subversion?

+4
1

Try:

xId='$Id'; xDate='$Date'; xRev='$Rev'; xAuthor='$Author'
cat <<EOT >> "$FILENAME_IMPL"
/*
 * *********************************************************************
 *  Revision Control Information
 *  File:                $xId::                                         $
 *  Date of Last Commit: $xDate::                                       $
 *  Revision Number:     $xRev::                                        $
 *  Last Commit by:      $xAuthor::                                     $
 *
 * **********************************************************************
 */
package com.company.pkg;
EOT

subversion script, $xId::$, xId . script, $xId , $FILENAME_IMPL $Id::$.

, , : $Id, $Date, , $FILENAME_IMPL. $FILENAME_IMPL , .

+3

Source: https://habr.com/ru/post/1653025/


All Articles