0

Hi below is my bash script. which takes a source file and a token file, token file contains servicename:usage I have to find servicename in source file line by line if found then calculate memory usage then change -Xmxm with -Xmx\d{1,3}m. In below script bold line explain what to do much simple You can first under stand issue from below small part of script

  line="Superviser.childOpts:-Xmx128m" heapMB=750 line=($(echo $line|sed "s/${-Xmx\d{1,3}m}/$-Xmx{$heapMB}m/g"))  

So what is the wrong in above line

 #!/bin/bash
sourceFile=$1
tokenFile=$2
if [ -z $sourceFile ]
then
echo "Please provide a valid source file"
exit 0
fi
if [ -z $tokenFile ]
then
echo "Please provide a valid token file"
exit 0
fi
#read token file and tokenize with : to get service name at 0 index and percentage usages at 1
declare arr_token_name
declare arr_token_usage
count=0
while read line
do
#here line contain :percentage usages
OIFS="$IFS"
IFS=$':'
arr=($line)
IFS="$OIFS"
if [ ! -z $line ]
then
arr_token_name[$count]=${arr[0]}
arr_token_usage[$count]=${arr[1]}
count=`expr $count + 1`
fi
done # read source file line by line test with all the tokens
totalMemKB=$(awk '/MemTotal:/ { print $2 }' /proc/meminfo)
echo "total mem = $totalMemKB"
while read line
do
result_token_search=""
#for j in "${arr_token_name[@]}"
#do
# echo "index=$j"
#done
count2=0
for i in "${arr_token_name[@]}"
do
#here search token in line , if found
#calculate memory for this getting percent usage from arr_token_usage then use calculate frmula then device by 1024
#then replace -Xmx\d{1,5}m with -Xmx
echo "line1=$line"
result_token_search=$(echo $line|grep -P "$i")
if [ -n "$result_token_search" ]
then
percent_usage=${arr_token_usage[$count2]}
let heapKB=$totalMemKB*$percent_usage/100
let heapMB=$heapKB/1024
echo "before sed=$line"
line=($(echo $line|sed "s/${-Xmx\d{1,3}m}/$-Xmx{$heapMB}m/g"))
echo "new line=$line"
echo "token found in line $line , token = $i"
fi
result_token_search=""
count2=`expr $count2+1`
cat "$line" >> tmp.txt
done
done

1 Answer 1

1

try this line:

line=$( sed "s/-Xmx[0-9]\+/-Xmx$heapMB/" <<<$line ) 

test with your example:

kent$ line="Superviser.childOpts:-Xmx128m" kent$ heapMB=750 kent$ line=$( sed "s/-Xmx[0-9]\+/-Xmx$heapMB/" <<<$line ) kent$ echo $line Superviser.childOpts:-Xmx750m 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.