Writing if condition in shell script and failing to do requirement

Hi,

I am trying to edit the values in a file. For example i am trying to edit the value of "ABC" in a file by executing shell script.

Please Note that ABC value can be there mulitple times or it may not be there in the file
Conditions for it is

  1. If ABC is less than 123 then it should change the ABC value to 123
  2. If ABC is greater than 123 then it should leave the ABC value to existing value only as it is greater than 123
  3. If ABC is not there is file then it should add ABC=123 in file.

Code i am trying is not working. PFB


     if [ $ABC -lt 123 ]
     then
        sed -i "s|\("ABC" *= *\).*|\1123|" file.con
     fi

    if [ $ABC -gt 123 ]
     then

    "ABC=$ABC" >> file.con  

     else

     echo "kernel.shmall=" >> /mqseries1/sysctl.conf
     sed -i "s|\("kernel.shmall" *= *\).*|\1123|" file.con
     
     fi

Experts can you please help me over here?

Also, provide the contents of file.con

Content is

########################

ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

here you go

       flag=0
       for file in $(cat con);
        do
                field=`echo $file | awk 'BEGIN { FS = "=" } { print $1 }'`
                val=`echo $file | awk 'BEGIN { FS = "=" } { print $2 }'`
                        if [ $field = "ABC" ] && [ $val -gt 123 ]; then
                               echo $field"="$val >> con2
                               flag=1
                        elif [ $field = "ABC" ] && [ $val -lt 123 ]; then
                                echo $field"=123" >> con2
                                flag=1
                        elif [ $field != "ABC" ]; then
                                echo $field"="$val >> con2
                        fi
        done

                        if [ $flag -eq 0 ]; then
                                echo "ABC=123" >> con2
                        fi

---------- Post updated at 10:45 AM ---------- Previous update was at 10:41 AM ----------

contents of con file

ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

---------- Post updated at 10:47 AM ---------- Previous update was at 10:45 AM ----------

contents of con2 after executing script

ABC=342
#XYZ=892
CR=5678
ABC=123
XYZ=1892
ABC=2654

When i executed the script 3 times, i got output as below. Output is adding to exisiting one. but ideally it should alter the existing one.

ABC=342
#XYZ=892
CR=5678
ABC=123
XYZ=1892
ABC=2654
ABC=342
#XYZ=892
CR=5678
ABC=123
XYZ=1892
ABC=2654
ABC=342
#XYZ=892
CR=5678
ABC=123
XYZ=1892
ABC=2654

You didn't show us how the shell variable ABC was set in your script and there was no mention of updating kernel.shmall so I don't know what this code from your script is trying to do:

     echo "kernel.shmall=" >> /mqseries1/sysctl.conf
     sed -i "s|\("kernel.shmall" *= *\).*|\1123|" file.con

Other than that, you could try something like:

#!/bin/ksh
file=${1:-file.con}
cp "$file" "_$file" && awk '
BEGIN {	FS = OFS = "=" }
$1 == "ABC" {
	found++
	if($2 < 123) $2 = 123
}
1
END {	if(!found) print "ABC=123" }
' "_$file" > "$file"

Unlike your script, this will leave a backup of your input file using the input filename with a leading underscore as the backup file. Although tested with a Korn shell, this will work with any shell that recognizes POSIX shell variable expansions.

By default, this script works on a file named file.con (as in your example), but you can invoke this script with one operand that names any file you want it to process.

       flag=0
       for file in $(cat con);
        do
                field=`echo $file | awk 'BEGIN { FS = "=" } { print $1 }'`
                val=`echo $file | awk 'BEGIN { FS = "=" } { print $2 }'`
                        if [ $field = "ABC" ] && [ $val -ge 123 ]; then
                               echo $field"="$val >> con2
                               flag=1
                        elif [ $field = "ABC" ] && [ $val -lt 123 ]; then
                                echo $field"=123" >> con2
                                flag=1
                        elif [ $field != "ABC" ]; then
                                echo $field"="$val >> con2
                        fi
        done

                        if [ $flag -eq 0 ]; then
                                echo "ABC=123" >> con2
                        fi
                cat con2 > con
                rm con2

While copying "kernal.shmall" came it seems. There is no "kernal.shmall". you can ignore it. Script you gave is failing .

file.con has below values

ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

script is

#!/bin/ksh
file=${1:-file.con}
cp "$file" "_$file" && awk '
BEGIN { FS = OFS = "=" }
$1 == "ABC" {
        found++
        if($2 < 123) $2 = 123
}
1
END {   if(!found) print "ABC=123" }
' "_$file" > "$file"

error i got is

awk: syntax error near line 8
awk: bailing out near line 8

new file got are
_file.con has

ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

which is back up

original file became empty
file.con

Have you tried with updated script provided to you...

Yes, it seems it is working. I am checking in different conditions and i will update you

---------- Post updated at 04:41 AM ---------- Previous update was at 04:36 AM ----------

Yes it is working, if i want to inclued XYZ with 456 condition where should i add condition. Actually i am looking for multiple values to check like ABC=123,XYZ=456, VFR=789.

You need to change if condition to add multiple values

Yes you to change condition.

Where to change. Can you please let me know the condition. Script would be very helpful

---------- Post updated at 05:38 AM ---------- Previous update was at 05:23 AM ----------

I tried this, but it is printing only ABC

Script is

flag=0
       for file in $(cat sysctl.conf);
        do
                field=`echo $file | awk 'BEGIN { FS = "=" } { print $1 }'`
                val=`echo $file | awk 'BEGIN { FS = "=" } { print $2 }'`
                        if [ $field = "ABC" ] && [ $val -ge 123 ]; then
                               echo $field"="$val >> sysctl.conf2
                               flag=1
                        elif [ $field = "ABC" ] && [ $val -lt 123 ]; then
                                echo $field"=123" >> sysctl.conf2
                                flag=1
                        elif [ $field != "ABC" ]; then
                                echo $field"="$val >> sysctl.conf2
                        fi
                        
                        if [ $field = "XYZ" ] && [ $val -ge 456 ]; then
                               echo $field"="$val >> sysctl.conf3
                               flag=1
                        elif [ $field = "XYZ" ] && [ $val -lt 456 ]; then
                                echo $field"=456" >> sysctl.conf3
                                flag=1
                        elif [ $field != "XYZ" ]; then
                                echo $field"="$val >> sysctl.conf3
                        fi

        done

                        if [ $flag -eq 0 ]; then
                                echo "ABC=123" >> sysctl.conf2
                                echo "XYZ=456" >> sysctl.conf3
                        fi
                cat sysctl.conf2 > sysctl.conf
                cat sysctl.conf3 > sysctl.conf
                rm sysctl.conf2 sysctl.conf3

output :

ABC=123

This code is working perfectly on OS X.
What operating system (including version) and shell are you using?

In the awk code line 8 is

1

Not understood by old awk.
Replace the line with

{print}