I need to replace a line in from a file using a shell script.
The file is:
# cat /root/sge_scripts/night_sched_conf
algorithm default
schedule_interval 0:0:05
maxujobs 12
queue_sort_method seqno
I want to replace the third line (after maxujobs) by a value generated in the script.
Here is what I have done so far:
#!/bin/sh
export SGE_ROOT=/opt/sge
source=/opt/sge/defalt/common/settings.sh
delete=1
total=`qstat -u "*" | awk ' { print $4 } ' | sort | uniq | wc -l`
total_users=$((total - delete))
case $total_users in
1) range=18;;
2) range=9 ;;
3) range=7 ;;
4) range=6 ;;
5) range=5 ;;
6) range=4 ;;
7) range=3 ;;
8) range=3 ;;
9) range=2 ;;
10) range=2 ;;
11) range=2 ;;
12) range=2 ;;
13) range=1 ;;
14) range=1 ;;
15) range=1 ;;
16) range=1 ;;
18) range=1 ;;
19) range=1 ;;
20) range=1 ;;
21) range=1 ;;
22) range=1 ;;
23) range=1 ;;
24) range=1 ;;
25) range=1 ;;
26) range=1 ;;
27) range=1 ;;
28) range=1 ;;
29) range=1 ;;
30) range=1 ;;
31) range=1 ;;
32) range=1 ;;
33) range=1 ;;
34) range=1 ;;
*) echo "INVALID NUMBER!" ;;
esac
current=`cat /root/sge_scripts/night_sched_conf | awk ' /maxujobs/ { print $2 } '`
if [ "$range" != "$current" ]
then
sed -i '3s/.*/maxujobs "$range"/' /root/sge_scripts/night_sched_conf
fi
Copyright Notice:Content Author:「sagar」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30770618/replace-a-line-in-a-file-using-shell-script