Writing if condition in shell script and failing to do requirement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Writing if condition in shell script and failing to do requirement
# 1  
Old 04-23-2014
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

Code:

     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?
# 2  
Old 04-23-2014
Also, provide the contents of file.con
# 3  
Old 04-23-2014
Content is

Code:
########################

ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654


Last edited by darling; 04-23-2014 at 12:15 PM..
# 4  
Old 04-23-2014
here you go

Code:
       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

Code:
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

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

# 5  
Old 04-23-2014
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.
Code:
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

# 6  
Old 04-24-2014
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:
Code:
     echo "kernel.shmall=" >> /mqseries1/sysctl.conf
     sed -i "s|\("kernel.shmall" *= *\).*|\1123|" file.con

Other than that, you could try something like:
Code:
#!/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.
# 7  
Old 04-24-2014
Code:
       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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sybase connection failing through shell script

Hi All, I'm trying to connect to Sybase via shell script. i'm getting the following error. Please let me know where i'm going wrong. Note that i'm not having this issue when connecting via terminal. #!/usr/bin/bash SYBASE=/usr/sybase ISQL=$SYBASE/bin/isql export SYBASE ISQL ... (6 Replies)
Discussion started by: Irishboy24
6 Replies

2. Shell Programming and Scripting

How to write bash shell script for mentioned requirement?

Hi All, I am unable to write the script for the below requirement. Requirement: Main table dir_ancillary table contain three column "dir_ancillary.table_name"," dir_ancillary.text_file_name ", "dir_ancillary.Include" . This dir_ancillary contain undefined tables in the column... (2 Replies)
Discussion started by: Vineeta Nigam
2 Replies

3. UNIX for Dummies Questions & Answers

Need help writing shell script!

Hi, I'm very new to this, so bear with me please. I want to write a sh script (or if there's a better format please let me know) that allows me to, when I run it, print the date to a file (1.out) take 2 arguments (files a.fa and b.fa), run them with another program, outputting to 2.out, and then... (4 Replies)
Discussion started by: ShiGua
4 Replies

4. Shell Programming and Scripting

Unix commands failing inside the shell script

When my script deals with large input files like 22Gb or 18 GB the basic commands like sort or join fails when run from inside the shell scripts. Can there be any specific reason for this? For e.g. sort -u -t "," -k1,1 a.csv > a.csv.uniq" sort -u -t "," -k1,1 b.csv > b.csv.uniq" The... (3 Replies)
Discussion started by: esha
3 Replies

5. Shell Programming and Scripting

Writing to file failing (maybe due to lock?)

Driver script kicks off supporter scripts in background. The supporter script writes it's own PID ($$) into a status file. When script completes, deletes the PID from the status file. The driver script constantly polls this status file for number of lines. As and when it's get lower than XX... (2 Replies)
Discussion started by: baivab
2 Replies

6. UNIX for Advanced & Expert Users

Shell script failing to read large Xml record-urgent critical help

Hi All, I have shell script running on AIX 5.3 box. It has 7 to 8 "sed" commands piped(|) together. It has a an Xml file as its input which has many records internally. There are certain record which which have more than hundered tags.The script is taking a huge amount of time more than 1.5 hrs... (10 Replies)
Discussion started by: aixjadoo
10 Replies

7. Shell Programming and Scripting

Required Shell script for My requirement

Hi All, I joined today in this forum to have all of your help. I have a Big requirement, pls. help me to resolve. I'm using HP-UX 11.23. I need a shell script for the following requirement. I have a file (nodes.txt) that contains 1000 nodes. I'm running the following command:... (3 Replies)
Discussion started by: ntgobinath
3 Replies

8. Shell Programming and Scripting

IF condition failing in a SSH script

Hi, I'm ssh-in to a remote machine (ubuntu) and trying to execute a little script in there.The script looks like this: ssh user@ubuntu <<EOF cd ~/test ls -l echo "Continue counting files starting with a`s ?" read answer if then ls -l a* else exit fi EOF Now everything works... (9 Replies)
Discussion started by: rubionis
9 Replies

9. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

10. Shell Programming and Scripting

Need help for writing shell script

Hello ALL, I am fresher in Unix . i need help to write small shell script . Please help me unix guru. I am developing the internal site in my office . the data files are generated in one directory everyday . I have to write shell script to sort those files and put it is internal site . ... (3 Replies)
Discussion started by: deepa20
3 Replies
Login or Register to Ask a Question