How to remove source code comments using Shell scripts in Linux

Source: Internet
Author: User
In Linux, use Shell scripts to remove source code comments-Linux general technology-Linux programming and kernel information. The following is a detailed description. This file is used to delete the comment. The operating system is limited to linux.

#! /Bin/bash
### The script use to delete comment

If [-z "$1"]; then # Check whether the source file is input
Echo "ipput the source file"
Exit 1
Fi

Postfix = 'echo $1 | cut-f 2-d'. ''# obtain the file suffix for different processing based on different files.

If [-n "$2"]; then # The file that stores the information produced by the source file.
Target_file = $2 # directly call
Touch $ {target_file}
Else
Prefix = 'echo $1 | cut-f 1-d'. ''# if not given, a file with the same name as the source file but suffix temp is generated.
Target_file =$ {prefix}. temp
Touch $ {target_file}
Fi

Case "postfix" in
Sh)
Echo "it is shell script! "
Sed's/[[: space:] #. * // G' $1 | sed '/^ #/d' | \
Sed '/^ [[: space:] * $/d' | sed'/^ $/d' >$ {target_file}
Echo "the source file is $1, target file is $ {target_file }"
;;
C | java)
Echo "c or java program"
Sed's /\/\*. * \ // G' $1 | sed '/\/\*/,/. * \ // d' | \
Sed's/\/. * // G' | \
Sed '/^ [[: space:] * $/d' | sed'/^ $/d' >$ {target_file}
Echo "the source file is $1, target file is $ {target_file }"
;;
*)
Echo "unknown file type! "
Rm-f $ {target_file}
;;
Esac


//-------------------------

#! /Bin/sh-f

Function del_comment_file ()
{
# Delete the C ++ mode comment.
# Delete the line begins //
# First Delete // The row at the beginning of the row
Sed-I '/^ [\ t] * \/\ // d' $ file

# Delete the line ends //
# Note that // is removed from the beginning of the line. The following match removes the URL // part, because some of the Code contains
# URL, such as fun ("ftp ://")
Sed-I's/\/[^ "] * // '$ file

# Delete lines that are annotated with only one row in C Language
Sed-I's/\ *. * \ // '$ file

# Delete the lines between/* and */
# Delete cross-row comments in C Language
Sed-I '/^ [\ t] * \/\ */,/. * \/d' $ file
}


Function del_comment ()
{
# $ Dir = 'pwd'

For file in 'LS'; do
Case $ file in
*. C)
Del_comment_file
;;
*. Cpp)
Del_comment_file
;;
*. H)
Del_comment_file
;;
*)
If [-d $ file]; then
Cd $ file
Del_comment
Cd ..
Fi
;;
Esac
Done
}

Dir = $1

If [! -E $ Dir]; then
Echo "The Directory isn' t exist ."
Exit
Fi

Cd $ Dir

Del_comment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.