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
# 8  
Old 04-24-2014
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
Code:
ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

script is
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"

error i got is

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

new file got are
_file.con has
Code:
ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

which is back up

original file became empty
file.con
# 9  
Old 04-24-2014
Have you tried with updated script provided to you...
# 10  
Old 04-24-2014
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.
# 11  
Old 04-24-2014
You need to change if condition to add multiple values
# 12  
Old 04-24-2014
Yes you to change condition.
# 13  
Old 04-24-2014
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

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

Code:
ABC=123

# 14  
Old 04-24-2014
Quote:
Originally Posted by darling
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
Code:
ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

script is
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"

error i got is

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

new file got are
_file.con has
Code:
ABC=342
#XYZ=892


CR=5678
ABC=23
XYZ=1892

ABC=2654

which is back up

original file became empty
file.con
This code is working perfectly on OS X.
What operating system (including version) and shell are you using?
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