-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsedtool.sh
More file actions
executable file
·84 lines (71 loc) · 1.86 KB
/
sedtool.sh
File metadata and controls
executable file
·84 lines (71 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash
SUCCESS=1
ERROR=0
ERROR_MSG=""
#******************************************************
#
#
#******************************************************
function split_string(){
local nargs="$#"
if (($nargs < 3)) ; then
ERROR_MSG="expecting two parameters but $nargs provided."
return $ERROR
fi
local value="$1"
local delimiter="$2"
local -n larray="${3}"
local counter=1
if [ ! -z "$value" ]; then
value=$(echo "$value" | tr "$delimiter" '\n')
while read -r line
do
larray[$counter]=$line
counter=$(($counter + 1))
done <<< "$value"
fi
return $SUCCESS
}
#***************************************************
#* @brief
#* The function is used to replace bash variables
#* in a given files.
#* @param filename the file to edit and replace its variables
#* @param the new values in "KEY=VALUE"
#***************************************************
function replace_bash_variables_infile(){
local nargs="$#"
if (($nargs < 0)); then
ERROR_MSG="$(echo "@replace_bash_variables: number of parameter provided did not match")"
return $ERROR
fi
local filename="$1"
local value="$2"
#check if the file exist
if [ ! -e "$filename" ] || [ -z "$filename" ]
then
ERROR_MSG="$(echo "@replace_bash_variables: filename '$filename' does not exist")"
return $ERROR
fi
if [ -z "$value" ] ; then
ERROR_MSG="$(echo "@replace_bash_variables: replace pair value '$value' is empty")"
return $ERROR
fi
#process the the files
while read -r line
do
if [ -z $line ]; then
continue;
fi
echo "$line" | sed -i "s/^$line" '$value'
done < "$filename"
return $SUCCESS
}
FILENAME="./bin/script.sh"
VALUES="PATH=~/bin"
replace_bash_variables_infile "$FILENAME" "$VALUES"
status="$?"
if (( $status <= 0 ))
then
echo "replace_bash_variables_infile failed: $ERROR_MSG, status= $status "
fi