LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   need help to replace line in shell script (https://www.linuxquestions.org/questions/linux-server-73/need-help-to-replace-line-in-shell-script-4175545034/)

sagar666 06-10-2015 08:43 PM

need help to replace line in shell script
 
Below is the file i need to replace third line with genarated output from script

My file:

[root@isge ~]# cat /root/sge_scripts/night_sched_conf
algorithm default
schedule_interval 0:0:05
maxujobs 12
queue_sort_method seqno

I want to replace in third line maxujobs number 12 to my script generated value (range value in below script)

My script:
#!/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

Please help me

goumba 06-10-2015 10:59 PM

What exactly is the problem? Is the substitution not being made?

sagar666 06-11-2015 12:24 AM

I cant able to pass range value in substitutions

sed -i '3s/.*/maxujobs "$range"/' /root/sge_scripts/night_sched_conf its not working instead value its its substitution $range itself

pan64 06-11-2015 12:33 AM

Try this:
Code:

sed -i '3s/.*/maxujobs '"$range/" /root/sge_scripts/night_sched_conf

goumba 06-11-2015 09:37 AM

Why do you have quotes around $range, when none is present in the original? Do you want the replacement to have quotes?

If not, and you only want the number, try this:

Code:

sed -i "3s/.*/maxujobs ${range}/" /root/sge_scripts/night_sched_conf
If you do indeed want the double quotes around the number, try this:

Code:

sed -i "3s/.*/maxujobs \"${range}\"/" /root/sge_scripts/night_sched_conf
Note:

I always use brackets for safety.

The type of quote - single or double - does matter to the shell (usually bash or dash by default) when doing substitutions. Using bash/dash, a variable name in single quotes will not be expanded to its value, whereas in a double quote the variable will be expanded.

sagar666 06-12-2015 08:58 PM

Thanks for your replies

goumba 06-13-2015 03:21 AM

If this solves your problem, please, mark the thread as solved.


All times are GMT -5. The time now is 05:29 PM.