Skip to content

Commit d514318

Browse files
committed
File comment and uncomment line with a specific string
1 parent b46d9b7 commit d514318

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#-----------------------------------------------------------------------
2+
#
3+
# Basescript function
4+
#
5+
# The basescript functions were designed to work as abstract function,
6+
# so it could be used in many different contexts executing specific job
7+
# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well"
8+
#
9+
# Developed by
10+
# Evert Ramos <evert.ramos@gmail.com>
11+
#
12+
# Copyright Evert Ramos
13+
#
14+
#-----------------------------------------------------------------------
15+
#
16+
# Be careful when editing this file, it is part of a bigger script!
17+
#
18+
# Basescript - https://github.com/evertramos/basescript
19+
#
20+
#-----------------------------------------------------------------------
21+
22+
#-----------------------------------------------------------------------
23+
# This function has one main objective:
24+
# 1. Comment a line in file
25+
#
26+
# You must/might inform the parameters below:
27+
# 1. Full file path
28+
# 2. String to find the correct line in file
29+
# 3. [optional] (default: '#') comment mark
30+
#
31+
#-----------------------------------------------------------------------
32+
33+
file_comment_line_with_string()
34+
{
35+
local LOCAL_FULL_FILE_PATH LOCAL_STRING LOCAL_COMMENT_MARK
36+
37+
LOCAL_FULL_FILE_PATH="${1:-null}"
38+
LOCAL_STRING="${2:-null}"
39+
LOCAL_COMMENT_MARK="${3:-#}"
40+
41+
# Check required
42+
[[ $LOCAL_FULL_FILE_PATH == "" || $LOCAL_FULL_FILE_PATH == null || $LOCAL_STRING == "" || $LOCAL_STRING == null ]] && \
43+
echoerr "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"
44+
45+
# Check if file exists | exit if 4th parameter is set to 'true'
46+
[[ ! -f $LOCAL_FULL_FILE_PATH ]] && echoerr "We could not find the file '$LOCAL_FULL_FILE_PATH' in your local folder. \nPlease inform the correct location and try again (function: ${FUNCNAME[0]})."
47+
48+
# Debug message
49+
[[ "$DEBUG" == true ]] && echo "Uncommenting '$LOCAL_STRING' in '$LOCAL_FULL_FILE_PATH' removing the '$LOCAL_COMMENT_MARK'."
50+
51+
sed -i '/'"$LOCAL_STRING"'/s/^/'"$LOCAL_COMMENT_MARK"'/g' "$LOCAL_FULL_FILE_PATH"
52+
53+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#-----------------------------------------------------------------------
2+
#
3+
# Basescript function
4+
#
5+
# The basescript functions were designed to work as abstract function,
6+
# so it could be used in many different contexts executing specific job
7+
# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well"
8+
#
9+
# Developed by
10+
# Evert Ramos <evert.ramos@gmail.com>
11+
#
12+
# Copyright Evert Ramos
13+
#
14+
#-----------------------------------------------------------------------
15+
#
16+
# Be careful when editing this file, it is part of a bigger script!
17+
#
18+
# Basescript - https://github.com/evertramos/basescript
19+
#
20+
#-----------------------------------------------------------------------
21+
22+
#-----------------------------------------------------------------------
23+
# This function has one main objective:
24+
# 1. Uncomment a line in file
25+
#
26+
# You must/might inform the parameters below:
27+
# 1. Full file path
28+
# 2. String to find the correct line in file
29+
# 3. [optional] (default: '#') comment mark
30+
#
31+
#-----------------------------------------------------------------------
32+
33+
file_uncomment_line_with_string()
34+
{
35+
local LOCAL_FULL_FILE_PATH LOCAL_STRING LOCAL_COMMENT_MARK
36+
37+
LOCAL_FULL_FILE_PATH="${1:-null}"
38+
LOCAL_STRING="${2:-null}"
39+
LOCAL_COMMENT_MARK="${3:-#}"
40+
41+
# Check required
42+
[[ $LOCAL_FULL_FILE_PATH == "" || $LOCAL_FULL_FILE_PATH == null || $LOCAL_STRING == "" || $LOCAL_STRING == null ]] && \
43+
echoerr "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'"
44+
45+
# Check if file exists | exit if 4th parameter is set to 'true'
46+
[[ ! -f $LOCAL_FULL_FILE_PATH ]] && echoerr "We could not find the file '$LOCAL_FULL_FILE_PATH' in your local folder. \nPlease inform the correct location and try again (function: ${FUNCNAME[0]})."
47+
48+
# Debug message
49+
[[ "$DEBUG" == true ]] && echo "Uncommenting '$LOCAL_STRING' in '$LOCAL_FULL_FILE_PATH' removing the '$LOCAL_COMMENT_MARK'."
50+
51+
sed -i '/'"$LOCAL_COMMENT_MARK"'/s/^'"$LOCAL_COMMENT_MARK"'//g' "$LOCAL_FULL_FILE_PATH"
52+
}

0 commit comments

Comments
 (0)